Quellcode durchsuchen

Merge branch 'nx' of https://github.com/neverxie/dstr into nx

# Conflicts:
#	README.md
#	dstr/examples/examples_dstr.c
#	dstr/inc/dstr.h
#	dstr/src/dstr.c
neverxie vor 7 Jahren
Ursprung
Commit
a34de5124c
4 geänderte Dateien mit 54 neuen und 48 gelöschten Zeilen
  1. 2 2
      README.md
  2. 23 20
      dstr/examples/examples_dstr.c
  3. 2 2
      dstr/inc/dstr.h
  4. 27 24
      dstr/src/dstr.c

+ 2 - 2
README.md

@@ -1,7 +1,7 @@
 # dstr
 
 ## 1、介绍
-这是一个在 RT-Thread 上,基于ANSI/ISO C(C89)实现的动态字符串软件包
+这是一个在 RT-Thread 上,基于ANSI/ISO C(C89)实现的动态字符串软件包
 
 ### 1.1 目录结构
 
@@ -17,7 +17,7 @@ dstr package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。
 
 ### 1.3 依赖
 
-- RT-Thread 3.0+
+对 RT-Thread 无依赖,也可用于裸机。
 
 ## 2、如何打开 dstr
 

+ 23 - 20
dstr/examples/examples_dstr.c

@@ -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>
@@ -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);
@@ -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");
     
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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("");

+ 2 - 2
dstr/inc/dstr.h

@@ -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__
@@ -30,7 +30,7 @@
 struct rt_dstr
 {
     char *str;
-    unsigned int length;
+    size_t length;  //  allocated space
 };
 typedef struct rt_dstr rt_dstr_t;
 

+ 27 - 24
dstr/src/dstr.c

@@ -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>
@@ -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>
 
@@ -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;
 }
@@ -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;
 
@@ -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)
     {
@@ -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;
     }
@@ -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
@@ -149,13 +151,13 @@ 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)
     {
@@ -163,8 +165,8 @@ rt_dstr_t *rt_dstr_ncat(rt_dstr_t *const thiz, const char *src, size_t 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;
 }
@@ -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);
 }
 
 /**
@@ -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);
    
@@ -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)
     {
@@ -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;