Skip to content

Commit

Permalink
Merge branch 'nx' of https://github.com/neverxie/dstr into nx
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	dstr/examples/examples_dstr.c
#	dstr/inc/dstr.h
#	dstr/src/dstr.c
  • Loading branch information
neverxie committed Jun 7, 2018
2 parents e128401 + 8b57021 commit a34de51
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# dstr

## 1、介绍
这是一个在 RT-Thread 上,基于ANSI/ISO C(C89)实现的动态字符串软件包
这是一个在 RT-Thread 上,基于ANSI/ISO C(C89)实现的动态字符串软件包

### 1.1 目录结构

Expand All @@ -17,7 +17,7 @@ dstr package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。

### 1.3 依赖

- RT-Thread 3.0+
RT-Thread 无依赖,也可用于裸机。

## 2、如何打开 dstr

Expand Down
43 changes: 23 additions & 20 deletions dstr/examples/examples_dstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* Change Logs:
* Date Author Notes
* 2018-06-06 never the first version
* 2018-06-07 never the first version
*/

#include <rtthread.h>
Expand All @@ -28,14 +28,14 @@

void rt_dstr_printf(rt_dstr_t *thiz)
{
if (thiz == RT_NULL)
if (thiz == NULL)
return;
rt_kprintf("%s\n", thiz->str);
}

void dstr_test_new(void)
{
rt_dstr_t *p = RT_NULL;
rt_dstr_t *p = NULL;

p = rt_dstr_new("new dstr");
rt_dstr_printf(p);
Expand All @@ -44,7 +44,7 @@ void dstr_test_new(void)

void dstr_test_cat(void)
{
rt_dstr_t *p = RT_NULL;
rt_dstr_t *p = NULL;

p = rt_dstr_new("cat");

Expand All @@ -57,21 +57,24 @@ void dstr_test_cat(void)

void dstr_test_ncat(void)
{
rt_dstr_t *p = RT_NULL;
rt_dstr_t *p1 = NULL;

p = rt_dstr_new("ncat");
p1 = rt_dstr_new("ncat");

rt_dstr_ncat(p, " dstrnnn", 5);
rt_dstr_ncat(p1, " dstrnnn", 5);

rt_dstr_printf(p);
rt_dstr_ncat(p1, "1234", 3);

rt_dstr_del(p);
rt_dstr_printf(p1);
rt_kprintf("p2 str:%s\n",p1->str);

rt_dstr_del(p1);
}

void dstr_test_cmp(void)
{
rt_dstr_t *p1 = RT_NULL;
rt_dstr_t *p2 = RT_NULL;
rt_dstr_t *p1 = NULL;
rt_dstr_t *p2 = NULL;
int res = 0;

p1 = rt_dstr_new("helle");
Expand Down Expand Up @@ -100,9 +103,9 @@ void dstr_test_cmp(void)

void dstr_test_ncmp(void)
{
rt_dstr_t *p1 = RT_NULL;
rt_dstr_t *p2 = RT_NULL;
rt_int8_t res = 0;
rt_dstr_t *p1 = NULL;
rt_dstr_t *p2 = NULL;
int res = 0;

p1 = rt_dstr_new("hello");
p2 = rt_dstr_new("hella");
Expand Down Expand Up @@ -130,9 +133,9 @@ void dstr_test_ncmp(void)

void dstr_test_casecmp(void)
{
rt_dstr_t *p1 = RT_NULL;
rt_dstr_t *p2 = RT_NULL;
rt_int8_t res = 0;
rt_dstr_t *p1 = NULL;
rt_dstr_t *p2 = NULL;
int res = 0;

p1 = rt_dstr_new("hello");
p2 = rt_dstr_new("HELLO");
Expand Down Expand Up @@ -160,7 +163,7 @@ void dstr_test_casecmp(void)

void dstr_test_strlen(void)
{
rt_dstr_t *p1 = RT_NULL;
rt_dstr_t *p1 = NULL;
int res = 0;

p1 = rt_dstr_new("hello strlen");
Expand All @@ -178,8 +181,8 @@ void dstr_test_strlen(void)
void dstr_test_sprintf(void)
{
const char *src = "hello dstr sprintf";
rt_dstr_t *p1 = RT_NULL;
rt_dstr_t *p2 = RT_NULL;
rt_dstr_t *p1 = NULL;
rt_dstr_t *p2 = NULL;

// string format
p1 = rt_dstr_new("");
Expand Down
4 changes: 2 additions & 2 deletions dstr/inc/dstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* Change Logs:
* Date Author Notes
* 2018-06-06 never the first version
* 2018-06-07 never the first version
*/

#ifndef __DSTR_H__
Expand All @@ -30,7 +30,7 @@
struct rt_dstr
{
char *str;
unsigned int length;
size_t length; // allocated space
};
typedef struct rt_dstr rt_dstr_t;

Expand Down
51 changes: 27 additions & 24 deletions dstr/src/dstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* Change Logs:
* Date Author Notes
* 2018-06-06 never the first version
* 2018-06-07 never the first version
*/

#include <rtthread.h>
Expand All @@ -32,7 +32,7 @@
#define DBG_ENABLE
#undef DBG_ENABLE
#define DBG_SECTION_NAME "[RTDSTR]"
#define DBG_LEVEL DBG_INFO
#define DBG_LEVEL DBG_ERROR
#define DBG_COLOR
#include <rtdbg.h>

Expand Down Expand Up @@ -60,16 +60,16 @@ rt_dstr_t *rt_dstr_new(const char *str)
return NULL;
}

thiz->length = strlen(str);
thiz->str = (char *)rt_malloc(sizeof(char) * (thiz->length + 1));
thiz->length = strlen(str) + 1; // allocated space
thiz->str = (char *)malloc(sizeof(char) * thiz->length + 1);

if (thiz->str == NULL)
{
rt_free(thiz);
free(thiz);
return NULL;
}

memcpy(thiz->str, str, thiz->length + 1);
memcpy(thiz->str, str, thiz->length);

return thiz;
}
Expand All @@ -88,16 +88,16 @@ void rt_dstr_del(rt_dstr_t *thiz)

if (thiz->str == NULL)
{
rt_free(thiz);
free(thiz);
return;
}

rt_free(thiz->str);
free(thiz->str);

rt_free(thiz);
free(thiz);
}

static int rt_dstr_resize(rt_dstr_t *const thiz, size_t new_length)
static int rt_dstr_resize(rt_dstr_t *const thiz, size_t new_spacesize)
{
char *p = NULL;

Expand All @@ -106,8 +106,8 @@ static int rt_dstr_resize(rt_dstr_t *const thiz, size_t new_length)
dbg_log(DBG_ERROR, "resize.thiz param error\n");
return NULL;
}

p = (char *)rt_realloc(thiz->str, new_length);
p = (char *)realloc(thiz->str, new_spacesize);

if (p == NULL)
{
Expand All @@ -116,6 +116,8 @@ static int rt_dstr_resize(rt_dstr_t *const thiz, size_t new_length)
}
else
{
thiz->length = new_spacesize;
rt_kprintf("new_spacesize:%d\n", thiz->length);
thiz->str = p;
return 0;
}
Expand All @@ -137,7 +139,7 @@ rt_dstr_t *rt_dstr_cat(rt_dstr_t *const thiz, const char *src)
}

/**
* This function is similar, except that it will use at most n bytes from src;
* This function is similar, except that it will use at most n bytes from src,
* and src does not need to be null-terminated if it contains n or more bytes.
*
* @param thiz the dstr(dynamic string) thiz
Expand All @@ -149,22 +151,22 @@ rt_dstr_t *rt_dstr_cat(rt_dstr_t *const thiz, const char *src)
rt_dstr_t *rt_dstr_ncat(rt_dstr_t *const thiz, const char *src, size_t n)
{
int res = 0;
rt_uint32_t new_length = 0, src_length = 0, old_length = 0;
size_t new_spacesize = 0, old_spacesize = 0, src_length = 0;

src_length = strlen(src);
old_length = thiz->length;
new_length = src_length + old_length + 1; // e.g.: abc + efg\0 = abcefg\0
old_spacesize = thiz->length; // allocated space

new_spacesize = n + old_spacesize; // allocated space

res = rt_dstr_resize(thiz, new_length);
res = rt_dstr_resize(thiz, new_spacesize);

if (res == -1)
{
dbg_log(DBG_ERROR, "nacat.resize error\n");
return NULL;
}

memcpy(thiz->str + old_length, src, n); //
*(thiz->str + old_length + n) = '\0';
memcpy(thiz->str + (old_spacesize - 1), src, n);
*(thiz->str + (old_spacesize - 1) + n) = '\0';

return thiz;
}
Expand Down Expand Up @@ -291,7 +293,7 @@ int rt_dstr_strlen(rt_dstr_t *const thiz)
if (thiz == NULL)
return -1;

return rt_strlen(thiz->str);
return strlen(thiz->str);
}

/**
Expand All @@ -307,9 +309,9 @@ int rt_dstr_sprintf(rt_dstr_t *const thiz, const char *fmt, ...)
va_list arg_ptr;
va_list tmp;
int status = 0;
size_t new_length = 0, old_length = 0, res = 0;
size_t new_length = 0, old_spacesize = 0, res = 0;

old_length = thiz->length;
old_spacesize = thiz->length;

va_start(arg_ptr, fmt);

Expand All @@ -319,7 +321,7 @@ int rt_dstr_sprintf(rt_dstr_t *const thiz, const char *fmt, ...)

va_end(tmp);

status = rt_dstr_resize(thiz, new_length + old_length);
status = rt_dstr_resize(thiz, new_length + old_spacesize);

if (status == -1)
{
Expand All @@ -328,6 +330,7 @@ int rt_dstr_sprintf(rt_dstr_t *const thiz, const char *fmt, ...)
}

res = vsnprintf(thiz->str, new_length + 1, fmt, arg_ptr);

va_end(arg_ptr);

return res;
Expand Down

0 comments on commit a34de51

Please sign in to comment.