Provide g_strnlen to replace strnlen for portability to older systems. (mono/mono...
authorJay Krell <jaykrell@microsoft.com>
Wed, 10 Jul 2019 03:43:13 +0000 (20:43 -0700)
committerMartin Baulig <mabaul@microsoft.com>
Wed, 10 Jul 2019 03:43:12 +0000 (23:43 -0400)
See https://github.com/mistydemeo/tigerbrew/issues/605.
See https://www.gnu.org/software/gnulib/manual/html_node/strnlen.html.

Commit migrated from https://github.com/mono/mono/commit/77c269721ed2f6b510af94a30be9b43f183f0eb7

src/mono/mono/eglib/eglib-remap.h
src/mono/mono/eglib/glib.h
src/mono/mono/eglib/gstr.c
src/mono/mono/eglib/test/string.c
src/mono/mono/mini/cfgdump.c

index 1a01348..43d6543 100644 (file)
 #define g_strlcpy monoeg_g_strlcpy
 #define g_strndup monoeg_g_strndup
 #define g_strnfill monoeg_g_strnfill
+#define g_strnlen monoeg_g_strnlen
 #define g_strreverse monoeg_g_strreverse
 #define g_strsplit monoeg_g_strsplit
 #define g_strsplit_set monoeg_g_strsplit_set
index 82a1ad6..c4b54ee 100644 (file)
@@ -375,6 +375,7 @@ gchar       *g_strchug        (gchar *str);
 gchar       *g_strchomp       (gchar *str);
 void         g_strdown        (gchar *string);
 gchar       *g_strnfill       (gsize length, gchar fill_char);
+gsize        g_strnlen        (const char*, gsize);
 
 void        g_strdelimit     (char *string, char delimiter, char new_delimiter);
 gchar       *g_strescape      (const gchar *source, const gchar *exceptions);
index e7cfb4c..b43f3d9 100644 (file)
@@ -1069,3 +1069,11 @@ g_utf16_len (const gunichar2 *a)
        return length;
 #endif
 }
+
+gsize
+g_strnlen (const char* s, gsize n)
+{
+       gsize i;
+       for (i = 0; i < n && s [i]; ++i) ;
+       return i;
+}
index ab73d25..c2cc5f3 100644 (file)
@@ -190,6 +190,18 @@ test_macros (void)
        return NULL;
 }
 
+static RESULT
+test_strnlen (void)
+{
+       g_assert (g_strnlen ("abc", 0) == 0);
+       g_assert (g_strnlen ("abc", 1) == 1);
+       g_assert (g_strnlen ("abc", 2) == 2);
+       g_assert (g_strnlen ("abc", 3) == 3);
+       g_assert (g_strnlen ("abc", 4) == 3);
+       g_assert (g_strnlen ("abc", 5) == 3);
+       return NULL;
+}
+
 static Test string_tests [] = {
        {"append-speed", test_append_speed},
        {"append_c-speed", test_append_c_speed},
@@ -198,6 +210,7 @@ static Test string_tests [] = {
        {"truncate", test_truncate },
        {"append_len", test_appendlen },
        {"macros", test_macros },
+       {"strnlen", test_strnlen },
        {NULL, NULL}
 };
 
index 3cd62c5..9f89aef 100644 (file)
@@ -103,7 +103,7 @@ write_int (MonoCompile *cfg, int v)
 static void
 write_string (MonoCompile *cfg, const char *str)
 {
-       size_t len = strnlen (str, 0x2000);
+       const size_t len = g_strnlen (str, 0x2000);
        write_int (cfg, (int) len);
 
        gunichar2 *u = u8to16 (str);
@@ -163,7 +163,7 @@ add_pool_entry (MonoCompile *cfg, ConstantPoolEntry *entry)
 
                        write_string (cfg, mono_inst_name (insn->opcode));
                        GString *insndesc = mono_print_ins_index_strbuf (-1, insn);
-                       int len = strnlen (insndesc->str, 0x2000);
+                       const int len = g_strnlen (insndesc->str, 0x2000);
 #define CUTOFF 40
                        if (len > CUTOFF) {
                                insndesc->str[CUTOFF] = '\0';