Kaynağa Gözat

ncd/NCDValGenerator.c, examples/ncdval_test.c: don't use NCDVal_StringGetPtr(), use cstring interface instead

ambrop7 13 yıl önce
ebeveyn
işleme
f8faed215b
2 değiştirilmiş dosya ile 8 ekleme ve 12 silme
  1. 5 2
      examples/ncdval_test.c
  2. 3 10
      ncd/NCDValGenerator.c

+ 5 - 2
examples/ncdval_test.c

@@ -142,15 +142,18 @@ static void test_string (NCDValRef str, const char *data, size_t length)
     FORCE( NCDVal_IsStringNoNulls(str) == !memchr(data, '\0', length) )
     FORCE( NCDVal_StringRegionEquals(str, 0, length, data) )
     
+    b_cstring cstr = NCDVal_StringCstring(str);
+    
     for (size_t i = 0; i < length; i++) {
-        const char *chunk_data;
         size_t chunk_length;
-        NCDVal_StringGetPtr(str, i, length - i, &chunk_data, &chunk_length);
+        const char *chunk_data = b_cstring_get(cstr, i, length - i, &chunk_length);
         
         FORCE( chunk_length > 0 )
         FORCE( chunk_length <= length - i )
         FORCE( !memcmp(chunk_data, data + i, chunk_length) )
         FORCE( NCDVal_StringRegionEquals(str, i, chunk_length, data + i) )
+        FORCE( b_cstring_memcmp(cstr, b_cstring_make_buf(data, length), i, i, chunk_length) == 0 )
+        FORCE( b_cstring_memcmp(cstr, b_cstring_make_buf(data + i, length - i), i, 0, chunk_length) == 0 )
     }
 }
 

+ 3 - 10
ncd/NCDValGenerator.c

@@ -45,19 +45,14 @@ static int generate_val (NCDValRef value, ExpString *out_str)
     
     switch (NCDVal_Type(value)) {
         case NCDVAL_STRING: {
-            size_t len = NCDVal_StringLength(value);
+            b_cstring cstr = NCDVal_StringCstring(value);
             
             if (!ExpString_AppendChar(out_str, '"')) {
                 BLog(BLOG_ERROR, "ExpString_AppendChar failed");
                 goto fail;
             }
             
-            size_t pos = 0;
-            while (pos < len) {
-                const char *chunk_data;
-                size_t chunk_len;
-                NCDVal_StringGetPtr(value, pos, len - pos, &chunk_data, &chunk_len);
-                
+            B_CSTRING_LOOP(cstr, pos, chunk_data, chunk_len, {
                 for (size_t i = 0; i < chunk_len; i++) {
                     if (chunk_data[i] == '\0') {
                         char buf[5];
@@ -83,9 +78,7 @@ static int generate_val (NCDValRef value, ExpString *out_str)
                         goto fail;
                     }
                 }
-                
-                pos += chunk_len;
-            }
+            })
             
             if (!ExpString_AppendChar(out_str, '"')) {
                 BLog(BLOG_ERROR, "ExpString_AppendChar failed");