diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 26fb1d7..ed1083b 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2842,7 +2842,7 @@ sw buf_inspect_str (s_buf *buf, const s_str *str)
if (! str_has_reserved_characters(str, &b))
return -1;
if (b)
- return buf_inspect_str_reserved(buf, str);
+ return buf_inspect_str_reserved(buf, str, true);
buf_save_init(buf, &save);
if ((r = buf_write_character_utf8(buf, '"')) <= 0)
goto clean;
@@ -3023,29 +3023,36 @@ sw buf_inspect_str_eval (s_buf *buf, const s_list *list)
sw r;
sw result = 0;
const s_tag *tag;
- if ((r = buf_write_1(buf, "\"")) < 0)
+ if ((r = buf_write_1(buf, "\"")) <= 0)
return r;
+ result += r;
l = list;
while (l) {
if (l->tag.type == TAG_STR) {
- if ((r = buf_write_str_without_indent(buf, &l->tag.data.str)) < 0)
+ if ((r = buf_inspect_str_reserved(buf, &l->tag.data.str,
+ false)) <= 0)
return r;
+ result += r;
}
else {
- if ((r = buf_write_1(buf, "#{")) < 0)
+ if ((r = buf_write_1(buf, "#{")) <= 0)
return r;
+ result += r;
tag = &l->tag;
if (tag_is_cast(tag, &g_sym_Str))
tag = &list_next(tag->data.call.arguments)->tag;
- if ((r = buf_inspect_tag(buf, tag)) < 0)
+ if ((r = buf_inspect_tag(buf, tag)) <= 0)
return r;
- if ((r = buf_write_1(buf, "}")) < 0)
+ result += r;
+ if ((r = buf_write_1(buf, "}")) <= 0)
return r;
+ result += r;
}
l = list_next(l);
}
- if ((r = buf_write_1(buf, "\"")) < 0)
+ if ((r = buf_write_1(buf, "\"")) <= 0)
return r;
+ result += r;
return result;
}
@@ -3055,49 +3062,54 @@ sw buf_inspect_str_eval_size (s_pretty *pretty, const s_list *list)
sw r;
sw result = 0;
const s_tag *tag;
- if ((r = buf_write_1_size(pretty, "\"")) < 0)
+ if ((r = buf_write_1_size(pretty, "\"")) <= 0)
return r;
+ result += r;
l = list;
while (l) {
if (l->tag.type == TAG_STR) {
- if ((r = buf_write_str_without_indent_size(pretty,
- &l->tag.data.str)) < 0)
+ if ((r = buf_inspect_str_reserved_size(pretty, &l->tag.data.str,
+ false)) <= 0)
return r;
+ result += r;
}
else {
- if ((r = buf_write_1_size(pretty, "#{")) < 0)
+ if ((r = buf_write_1_size(pretty, "#{")) <= 0)
return r;
+ result += r;
tag = &l->tag;
if (tag_is_cast(tag, &g_sym_Str))
tag = &list_next(tag->data.call.arguments)->tag;
- if ((r = buf_inspect_tag_size(pretty, tag)) < 0)
+ if ((r = buf_inspect_tag_size(pretty, tag)) <= 0)
return r;
- if ((r = buf_write_1_size(pretty, "}")) < 0)
+ result += r;
+ if ((r = buf_write_1_size(pretty, "}")) <= 0)
return r;
+ result += r;
}
l = list_next(l);
}
- if ((r = buf_write_1_size(pretty, "\"")) < 0)
+ if ((r = buf_write_1_size(pretty, "\"")) <= 0)
return r;
+ result += r;
return result;
}
/* keep in sync with buf_inspect_str_reserved_size */
-sw buf_inspect_str_reserved (s_buf *buf, const s_str *str)
+sw buf_inspect_str_reserved (s_buf *buf, const s_str *str, bool quotes)
{
u8 byte;
character c;
- sw r;
+ sw r = 1;
sw result = 0;
s_str s;
s_buf_save save;
buf_save_init(buf, &save);
- if ((r = buf_write_1(buf, "\"")) <= 0) {
- if (! r)
- r = -1;
- goto clean;
+ if (quotes) {
+ if ((r = buf_write_1(buf, "\"")) <= 0)
+ goto clean;
+ result += r;
}
- result += r;
s = *str;
while (r) {
if ((r = str_read_character_utf8(&s, &c)) < 0)
@@ -3115,9 +3127,11 @@ sw buf_inspect_str_reserved (s_buf *buf, const s_str *str)
result += r;
}
}
- if ((r = buf_write_character_utf8(buf, '"')) <= 0)
- goto restore;
- result += r;
+ if (quotes) {
+ if ((r = buf_write_1(buf, "\"")) <= 0)
+ goto restore;
+ result += r;
+ }
r = result;
goto clean;
restore:
@@ -3130,16 +3144,19 @@ sw buf_inspect_str_reserved (s_buf *buf, const s_str *str)
}
/* keep in sync with buf_inspect_str_reserved */
-sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str)
+sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str,
+ bool quotes)
{
u8 byte;
character c;
- sw r;
+ sw r = 1;
sw result = 0;
s_str s;
- if ((r = buf_write_1_size(pretty, "\"")) < 0)
- return r;
- result += r;
+ if (quotes) {
+ if ((r = buf_write_1_size(pretty, "\"")) <= 0)
+ return r;
+ result += r;
+ }
s = *str;
while (r) {
if ((r = str_read_character_utf8(&s, &c)) < 0)
@@ -3156,9 +3173,11 @@ sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str)
result += r;
}
}
- if ((r = buf_write_1_size(pretty, "\"")) < 0)
- return r;
- result += r;
+ if (quotes) {
+ if ((r = buf_write_1_size(pretty, "\"")) <= 0)
+ return r;
+ result += r;
+ }
return result;
restore:
return -1;
@@ -3172,7 +3191,7 @@ sw buf_inspect_str_size (s_pretty *pretty, const s_str *str)
if (! str_has_reserved_characters(str, &b))
return -1;
if (b)
- return buf_inspect_str_reserved_size(pretty, str);
+ return buf_inspect_str_reserved_size(pretty, str, true);
if ((r = buf_write_1_size(pretty, "\"")) < 0)
return r;
result += r;
@@ -3782,18 +3801,47 @@ sw buf_inspect_time_size (s_pretty *pretty, const s_time *time)
{
sw r;
sw result = 0;
- if ((r = buf_write_1_size(pretty, "%Time{tv_sec: ")) < 0)
- return r;
- result += r;
- if ((r = buf_inspect_sw_size(pretty, &time->tv_sec)) < 0)
- return r;
- result += r;
- if ((r = buf_write_1_size(pretty, ", tv_nsec: ")) < 0)
- return r;
- result += r;
- if ((r = buf_inspect_sw_size(pretty, &time->tv_nsec)) < 0)
+ bool sec;
+ if ((r = buf_write_1_size(pretty, "%Time{")) < 0)
return r;
result += r;
+ if ((sec = time->tag ?
+ time->tag->type != TAG_SW || time->tag->data.sw :
+ time->tv_sec)) {
+ if ((r = buf_write_1_size(pretty, "tv_sec: ")) < 0)
+ return r;
+ result += r;
+ if (time->tag) {
+ if ((r = buf_inspect_tag_size(pretty, time->tag)) < 0)
+ return r;
+ }
+ else {
+ if ((r = buf_inspect_sw_decimal_size(pretty, &time->tv_sec)) < 0)
+ return r;
+ }
+ result += r;
+ }
+ if (time->tag ?
+ time->tag[1].type != TAG_SW || time->tag[1].data.sw :
+ time->tv_nsec) {
+ if (sec) {
+ if ((r = buf_write_1_size(pretty, ", ")) < 0)
+ return r;
+ result += r;
+ }
+ if ((r = buf_write_1_size(pretty, "tv_nsec: ")) < 0)
+ return r;
+ result += r;
+ if (time->tag) {
+ if ((r = buf_inspect_tag_size(pretty, time->tag + 1)) < 0)
+ return r;
+ }
+ else {
+ if ((r = buf_inspect_sw_decimal_size(pretty, &time->tv_nsec)) < 0)
+ return r;
+ }
+ result += r;
+ }
if ((r = buf_write_1_size(pretty, "}")) < 0)
return r;
result += r;
diff --git a/libkc3/buf_inspect.h b/libkc3/buf_inspect.h
index 8cf06b0..3427111 100644
--- a/libkc3/buf_inspect.h
+++ b/libkc3/buf_inspect.h
@@ -176,8 +176,9 @@ sw buf_inspect_str_character_size (s_pretty *pretty,
sw buf_inspect_str_eval (s_buf *buf, const s_list *list);
sw buf_inspect_str_eval_size (s_pretty *pretty, const s_list *list);
sw buf_inspect_str (s_buf *buf, const s_str *str);
-sw buf_inspect_str_reserved (s_buf *buf, const s_str *str);
-sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str);
+sw buf_inspect_str_reserved (s_buf *buf, const s_str *str, bool quotes);
+sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str,
+ bool quotes);
sw buf_inspect_str_size (s_pretty *pretty, const s_str *str);
sw buf_inspect_struct (s_buf *buf, const s_struct *s);
sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s);
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 7a968cc..e0cf4d1 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -33,6 +33,7 @@
#include "fn_clause.h"
#include "frame.h"
#include "ident.h"
+#include "inspect.h"
#include "integer.h"
#include "list.h"
#include "map.h"
@@ -1641,7 +1642,7 @@ sw buf_parse_fact (s_buf *buf, s_fact_w *dest)
if ((r = buf_ignore_spaces(buf)) < 0)
goto restore;
result += r;
- if ((r = buf_parse_tag(buf, &tmp.subject)) <= 0)
+ if ((r = buf_parse_static_tag(buf, &tmp.subject)) <= 0)
goto restore;
result += r;
if ((r = buf_parse_comments(buf)) < 0)
@@ -1659,7 +1660,7 @@ sw buf_parse_fact (s_buf *buf, s_fact_w *dest)
if ((r = buf_ignore_spaces(buf)) < 0)
goto restore;
result += r;
- if ((r = buf_parse_tag(buf, &tmp.predicate)) <= 0)
+ if ((r = buf_parse_static_tag(buf, &tmp.predicate)) <= 0)
goto restore;
result += r;
if ((r = buf_parse_comments(buf)) < 0)
@@ -1677,7 +1678,7 @@ sw buf_parse_fact (s_buf *buf, s_fact_w *dest)
if ((r = buf_ignore_spaces(buf)) < 0)
goto restore;
result += r;
- if ((r = buf_parse_tag(buf, &tmp.object)) <= 0)
+ if ((r = buf_parse_static_tag(buf, &tmp.object)) <= 0)
goto restore;
result += r;
if ((r = buf_parse_comments(buf)) < 0)
@@ -3098,6 +3099,37 @@ sw buf_parse_quote (s_buf *buf, s_quote *dest)
return r;
}
+sw buf_parse_ratio (s_buf *buf, s_ratio *dest)
+{
+ sw r;
+ sw result = 0;
+ s_buf_save save;
+ s_ratio tmp = {0};
+ buf_save_init(buf, &save);
+ if ((r = buf_parse_integer(buf, &tmp.numerator)) <= 0)
+ goto clean;
+ result += r;
+ if ((r = buf_read_1(buf, "/")) <= 0)
+ goto restore;
+ result += r;
+ if ((r = buf_parse_integer(buf, &tmp.denominator)) <= 0)
+ goto restore;
+ result += r;
+ if (! integer_is_positive(&tmp.denominator)) {
+ r = -1;
+ goto restore;
+ }
+ *dest = tmp;
+ r = result;
+ goto clean;
+ restore:
+ ratio_clean(&tmp);
+ buf_save_restore_rpos(buf, &save);
+ clean:
+ buf_save_clean(buf, &save);
+ return r;
+}
+
sw buf_parse_special_operator (s_buf *buf, s_call *dest)
{
bool b;
@@ -3185,37 +3217,36 @@ sw buf_parse_special_operator (s_buf *buf, s_call *dest)
return r;
}
-sw buf_parse_ratio (s_buf *buf, s_ratio *dest)
+sw buf_parse_static_tag (s_buf *buf, s_tag *tag)
{
sw r;
- sw result = 0;
s_buf_save save;
- s_ratio tmp = {0};
+ s_str str;
+ s_tag tmp;
+ assert(buf);
+ assert(tag);
buf_save_init(buf, &save);
- if ((r = buf_parse_integer(buf, &tmp.numerator)) <= 0)
- goto clean;
- result += r;
- if ((r = buf_read_1(buf, "/")) <= 0)
- goto restore;
- result += r;
- if ((r = buf_parse_integer(buf, &tmp.denominator)) <= 0)
- goto restore;
- result += r;
- if (! integer_is_positive(&tmp.denominator)) {
- r = -1;
- goto restore;
+ if ((r = buf_parse_tag(buf, &tmp)) <= 0)
+ return r;
+ if (tmp.type == TAG_CALL &&
+ (! tmp.data.call.ident.module ||
+ tmp.data.call.ident.module == &g_sym_KC3) &&
+ tmp.data.call.ident.sym == &g_sym_str &&
+ tmp.data.call.arguments &&
+ tmp.data.call.arguments->tag.type == TAG_LIST) {
+ tag_void(&tmp);
+ buf_save_restore_rpos(buf, &save);
+ if ((r = buf_parse_str(buf, &str)) <= 0)
+ goto clean;
+ tmp.type = TAG_STR;
+ tmp.data.str = str;
}
- *dest = tmp;
- r = result;
- goto clean;
- restore:
- ratio_clean(&tmp);
- buf_save_restore_rpos(buf, &save);
+ *tag = tmp;
clean:
buf_save_clean(buf, &save);
return r;
}
-
+
sw buf_parse_str (s_buf *buf, s_str *dest)
{
u8 b;
diff --git a/libkc3/buf_parse.h b/libkc3/buf_parse.h
index eb95f1d..43994db 100644
--- a/libkc3/buf_parse.h
+++ b/libkc3/buf_parse.h
@@ -97,6 +97,7 @@ sw buf_parse_ptr_free (s_buf *buf, u_ptr_w *dest);
sw buf_parse_quote (s_buf *buf, s_quote *dest);
sw buf_parse_ratio (s_buf *buf, s_ratio *dest);
sw buf_parse_special_operator (s_buf *buf, s_call *dest);
+sw buf_parse_static_tag (s_buf *buf, s_tag *tag);
sw buf_parse_str (s_buf *buf, s_str *dest);
sw buf_parse_str_character (s_buf *buf, character *dest);
sw buf_parse_str_character_unicode (s_buf *buf, character *dest);
diff --git a/libkc3/inspect.c b/libkc3/inspect.c
index 49f09b2..3712b47 100644
--- a/libkc3/inspect.c
+++ b/libkc3/inspect.c
@@ -222,6 +222,7 @@ s_str * inspect_str (const s_str *str, s_str *dest)
{
s_buf buf;
s_pretty pretty = {0};
+ sw r;
sw size;
size = buf_inspect_str_size(&pretty, str);
if (size < 0) {
@@ -229,10 +230,10 @@ s_str * inspect_str (const s_str *str, s_str *dest)
assert(! "inspect_str: buf_inspect_str_size");
return NULL;
}
- buf_init_alloc(&buf, size);
- buf_inspect_str(&buf, str);
- assert(buf.wpos == buf.size);
- if (buf.wpos != buf.size) {
+ if (! buf_init_alloc(&buf, size))
+ return NULL;
+ r = buf_inspect_str(&buf, str);
+ if (r != size) {
buf_clean(&buf);
err_puts("inspect_str: buf_inspect_str");
assert(! "inspect_str: buf_inspect_str");
@@ -241,6 +242,35 @@ s_str * inspect_str (const s_str *str, s_str *dest)
return buf_to_str(&buf, dest);
}
+s_str * inspect_str_eval (const s_list *list, s_str *dest)
+{
+ s_buf buf;
+ s_pretty pretty = {0};
+ sw r;
+ sw size;
+ size = buf_inspect_str_eval_size(&pretty, list);
+ if (size < 0) {
+ err_puts("inspect_str: buf_inspect_str_size");
+ assert(! "inspect_str: buf_inspect_str_size");
+ return NULL;
+ }
+ if (! buf_init_alloc(&buf, size))
+ return NULL;
+ if ((r = buf_inspect_str_eval(&buf, list)) <= 0) {
+ buf_clean(&buf);
+ err_puts("inspect_str: buf_inspect_str_eval <= 0");
+ assert(! "inspect_str: buf_inspect_str_eval <= 0");
+ return NULL;
+ }
+ if (r != size) {
+ buf_clean(&buf);
+ err_puts("inspect_str: buf_inspect_str_eval != size");
+ assert(! "inspect_str: buf_inspect_str_eval != size");
+ return NULL;
+ }
+ return buf_to_str(&buf, dest);
+}
+
s_str * inspect_struct (const s_struct *s, s_str *dest)
{
s_pretty pretty = {0};
diff --git a/libkc3/inspect.h b/libkc3/inspect.h
index 1ee693a..88e062c 100644
--- a/libkc3/inspect.h
+++ b/libkc3/inspect.h
@@ -30,6 +30,7 @@ s_str * inspect_ident (const s_ident *ident, s_str *dest);
s_str * inspect_list (const s_list *list, s_str *dest);
s_str * inspect_ratio (const s_ratio *src, s_str *dest);
s_str * inspect_str (const s_str *str, s_str *dest);
+s_str * inspect_str_eval (const s_list *list, s_str *dest);
s_str * inspect_struct (const s_struct *s, s_str *dest);
s_str * inspect_sym (const s_sym *sym, s_str *dest);
s_str * inspect_tag (const s_tag *tag, s_str *dest);
diff --git a/libkc3/str.c b/libkc3/str.c
index 4788a9c..07e00ca 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -254,16 +254,18 @@ bool * str_has_reserved_characters (const s_str *src, bool *dest)
sw r;
s_str str;
str_init(&str, NULL, src->size, src->ptr.p);
- while ((r = str_read_character_utf8(&str, &c)) > 0) {
+ while (str.size > 0) {
+ if ((r = str_read_character_utf8(&str, &c)) < 0) {
+ *dest = true;
+ return dest;
+ }
+ if (! r)
+ break;
if (str_character_is_reserved(c)) {
*dest = true;
return dest;
}
}
- if (r < 0) {
- *dest = true;
- return dest;
- }
*dest = false;
return dest;
}