Commit ed3678fc59c06340b7dd687d78baf94b37321230

Thomas de Grivel 2023-06-11T11:27:47

fix buf_inspect(\n)

diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 3d7a94d..c1224ee 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -1145,7 +1145,7 @@ sw buf_inspect_str_reserved (s_buf *buf, const s_str *str)
   s_str s;
   s_buf_save save;
   buf_save_init(buf, &save);
-  if ((r = buf_write_u8(buf, '"')) <= 0) {
+  if ((r = buf_write_1(buf, "\"")) <= 0) {
     if (! r)
       r = -1;
     goto clean;
@@ -1190,27 +1190,29 @@ sw buf_inspect_str_reserved_size (const s_str *str)
   sw r;
   sw result = 0;
   s_str s;
-  result += sizeof('"');
+  r = strlen("\"");
+  result += r;
   str_init_str(&s, str);
-  r = 1;
   while (r) {
     if ((r = str_read_character_utf8(&s, &c)) < 0)
-      return r;
+      goto restore;
     if (r) {
-      if ((r = buf_inspect_str_character_size(&c)) <= 0) {
-        if (! r)
-          r = -1;
-        return r;
-      }
+      if ((r = buf_inspect_str_character_size(&c)) <= 0)
+        goto restore;
       result += r;
     }
     else if ((r = str_read_u8(&s, &byte)) < 0)
-      return r;
-    else if (r)
-      result += buf_inspect_str_byte_size;
+      goto restore;
+    else if (r) {
+      r = buf_inspect_str_byte_size;
+      result += r;
+    }
   }
-  result += sizeof('"');
+  r = strlen("\"");
+  result += r;
   return result;
+ restore:
+  return -1;
 }
 
 sw buf_inspect_str_size (const s_str *str)
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index 6a06c29..5f710ae 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -99,7 +99,7 @@ BUF_INSPECT_S_PROTOTYPES(64);
 BUF_INSPECT_S_PROTOTYPES(w);
 sw buf_inspect_str (s_buf *buf, const s_str *str);
 sw buf_inspect_str_byte (s_buf *buf, const u8 *byte);
-static const sw buf_inspect_str_byte_size = 4;
+sw buf_inspect_str_byte_size (const u8 *byte);
 sw buf_inspect_str_character (s_buf *buf, const character *c);
 sw buf_inspect_str_character_size (const character *c);
 sw buf_inspect_str_reserved (s_buf *buf, const s_str *str);
diff --git a/test/buf_inspect_test.c b/test/buf_inspect_test.c
index 73d07a7..569f3c1 100644
--- a/test/buf_inspect_test.c
+++ b/test/buf_inspect_test.c
@@ -23,7 +23,7 @@
     test_context("buf_inspect_bool(" # test ") -> " # expected);       \
     buf_init(&buf, false, sizeof(b), b);                               \
     tmp = (test);                                                      \
-    TEST_EQ(buf_inspect_bool_size(&tmp), strlen(expected));      \
+    TEST_EQ(buf_inspect_bool_size(&tmp), strlen(expected));            \
     TEST_EQ(buf_inspect_bool(&buf, &tmp), strlen(expected));           \
     TEST_STRNCMP(buf.ptr.p, (expected), buf.wpos);                     \
     test_context(NULL);                                                \
@@ -122,9 +122,9 @@
     test_context("buf_inspect_str(" # test ") -> " # expected);        \
     str_init_1(&str, NULL, (test));                                    \
     buf_init(&buf, false, sizeof(b), b);                               \
-    TEST_STRNCMP(buf.ptr.p, (expected), buf.wpos);                     \
-    TEST_EQ(buf_inspect_str(&buf, &str), strlen(expected));            \
     TEST_EQ(buf_inspect_str_size(&str), strlen(expected));             \
+    TEST_EQ(buf_inspect_str(&buf, &str), strlen(expected));            \
+    TEST_STRNCMP(buf.ptr.p, (expected), buf.wpos);                     \
     test_context(NULL);                                                \
   } while (0)
 
@@ -314,6 +314,7 @@ TEST_CASE(buf_inspect_str_character)
   BUF_INSPECT_TEST_STR_CHARACTER(0, "\\0");
   BUF_INSPECT_TEST_STR_CHARACTER(1, "\\x01");
   BUF_INSPECT_TEST_STR_CHARACTER(2, "\\x02");
+  BUF_INSPECT_TEST_STR_CHARACTER(10, "\\n");
   BUF_INSPECT_TEST_STR_CHARACTER('0', "0");
   BUF_INSPECT_TEST_STR_CHARACTER('9', "9");
   BUF_INSPECT_TEST_STR_CHARACTER('A', "A");