Commit 1d64d11c786e5f863827157753c970b60c77a65a

Thomas de Grivel 2024-08-16T13:55:14

str_starts_with, str_ends_with

diff --git a/lib/kc3/0.1/str.facts b/lib/kc3/0.1/str.facts
index 85cf414..5edba7d 100644
--- a/lib/kc3/0.1/str.facts
+++ b/lib/kc3/0.1/str.facts
@@ -6,7 +6,7 @@ replace {Str.cast, :symbol_value,
          cfn Str "str_init_cast" (Result, Sym, Tag)}
 add {Str, :symbol, Str.ends_with?}
 replace {Str.ends_with?, :symbol_value,
-         cfn Bool "str_ends_with" (Str, Str)}
+         cfn Bool "str_ends_with" (Str, Str, Result)}
 add {Str, :symbol, Str.rindex_character}
 replace {Str.rindex_character, :symbol_value,
          cfn Sw "str_rindex_character" (Str, Character)}
@@ -15,4 +15,4 @@ replace {Str.slice, :symbol_value,
          cfn Str "str_init_slice" (Result, Str, Sw, Sw)}
 add {Str, :symbol, Str.starts_with?}
 replace {Str.starts_with?, :symbol_value,
-         cfn Bool "str_starts_with" (Str, Str)}
+         cfn Bool "str_starts_with" (Str, Str, Result)}
diff --git a/libkc3/str.c b/libkc3/str.c
index 49df097..0b02a46 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -227,17 +227,22 @@ void str_delete (s_str *str)
   free(str);
 }
 
-bool str_ends_with (const s_str *str, const s_str *end)
+bool * str_ends_with (const s_str *str, const s_str *end, bool *dest)
 {
   uw i;
   assert(str);
   assert(end);
-  if (! end->size)
-    return true;
-  if (str->size < end->size)
-    return false;
+  if (! end->size) {
+    *dest = true;
+    return dest;
+  }
+  if (str->size < end->size) {
+    *dest = false;
+    return dest;
+  }
   i = str->size - end->size;
-  return ! memcmp(str->ptr.pchar + i, end->ptr.p, end->size);
+  *dest = ! memcmp(str->ptr.pchar + i, end->ptr.p, end->size);
+  return dest;
 }
 
 bool * str_has_reserved_characters (const s_str *src, bool *dest)
@@ -969,15 +974,21 @@ sw str_rindex_character (const s_str *str, character c)
   return result;
 }
 
-bool str_starts_with (const s_str *str, const s_str *start)
+bool * str_starts_with (const s_str *str, const s_str *start,
+                        bool *dest)
 {
   assert(str);
   assert(start);
-  if (! start->size)
-    return true;
-  if (str->size < start->size)
-    return false;
-  return ! memcmp(str->ptr.p, start->ptr.p, start->size);
+  if (! start->size) {
+    *dest = true;
+    return dest;
+  }
+  if (str->size < start->size) {
+    *dest = false;
+    return dest;
+  }
+  *dest = ! memcmp(str->ptr.p, start->ptr.p, start->size);
+  return dest;
 }
 
 uw * str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest)
diff --git a/libkc3/str.h b/libkc3/str.h
index 589affc..027c6c0 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -97,7 +97,8 @@ sw            str_character (const s_str *str, uw position,
 character     str_character_escape (character c);
 bool          str_character_is_reserved (character c);
 sw            str_character_position (const s_str *str, character c);
-bool          str_ends_with (const s_str *str, const s_str *end);
+bool *        str_ends_with (const s_str *str, const s_str *end,
+                             bool *dest);
 bool *        str_has_reserved_characters (const s_str *src,
                                            bool *dest);
 sw            str_length_utf8 (const s_str *str);
@@ -118,7 +119,8 @@ sw            str_peek_u32 (const s_str *str, u32 *dest);
 sw            str_peek_u64 (const s_str *str, u64 *dest);
 sw            str_position_1 (const s_str *str, const char *token);
 sw            str_rindex_character (const s_str *str, character c);
-bool          str_starts_with (const s_str *str, const s_str *start);
+bool *        str_starts_with (const s_str *str, const s_str *start,
+                               bool *dest);
 uw *          str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest);
 s_str *       str_to_hex (const s_str *str, s_str *dest);
 s_ident *     str_to_ident (const s_str *str, s_ident *dest);