From c1496ab6c055974ac401865bdca87a4fc0a76ea3 Mon Sep 17 00:00:00 2001 From: bobsayshilol Date: Sun, 27 Oct 2024 20:11:42 +0000 Subject: Fix passing nullptr to args marked as non-null libstdc++'s |memcpy| and |memset| both state that their inputs should never be a nullptr since this matches the C spec. Some compilers act on these hints, so we shouldn't unconditionally call these as it would signal to the compiler that they can't be nullptrs. As an example, the following code will always call |do_something()| when compiled with optimisations since GCC version 4.9: ``` void clear(void *ptr, int size) { memset(ptr, 0, size); } void example(void *ptr, int size) { clear(ptr, size); if (ptr != nullptr) do_something(); } ``` --- src/string.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/string.cpp') diff --git a/src/string.cpp b/src/string.cpp index 3c7d96934..190b69041 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -156,6 +156,7 @@ gb_internal isize string_index_byte(String const &s, u8 x) { gb_internal gb_inline bool str_eq(String const &a, String const &b) { if (a.len != b.len) return false; + if (a.len == 0) return true; return memcmp(a.text, b.text, a.len) == 0; } gb_internal gb_inline bool str_ne(String const &a, String const &b) { return !str_eq(a, b); } -- cgit v1.2.3 From 6f966f30aab6f739dd65301500a6a0aff67da1d5 Mon Sep 17 00:00:00 2001 From: Dominik Pötzschke <16013351+dpoetzschke@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:24:50 +0100 Subject: fix: fix windows params bug --- src/string.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/string.cpp') diff --git a/src/string.cpp b/src/string.cpp index 3c7d96934..14f56e4f0 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -411,6 +411,25 @@ gb_internal String concatenate4_strings(gbAllocator a, String const &x, String c return make_string(data, len); } +#if defined(GB_SYSTEM_WINDOWS) +gb_internal String escape_char(gbAllocator a, String s, char cte) { + isize buf_len = s.len; + u8 *buf = gb_alloc_array(a, u8, buf_len); + isize i = 0; + for (isize j = 0; j < s.len; j++) { + u8 c = s.text[j]; + + if (c == cte) { + buf[i++] = '\\'; + buf[i++] = c; + } else { + buf[i++] = c; + } + } + return make_string(buf, i); +} +#endif + gb_internal String string_join_and_quote(gbAllocator a, Array strings) { if (!strings.count) { return make_string(nullptr, 0); @@ -426,7 +445,11 @@ gb_internal String string_join_and_quote(gbAllocator a, Array strings) { if (i > 0) { s = gb_string_append_fmt(s, " "); } +#if defined(GB_SYSTEM_WINDOWS) + s = gb_string_append_fmt(s, "\"%.*s\" ", LIT(escape_char(a, strings[i], '\\'))); +#else s = gb_string_append_fmt(s, "\"%.*s\" ", LIT(strings[i])); +#endif } return make_string(cast(u8 *) s, gb_string_length(s)); -- cgit v1.2.3 From d74f2154900c5d0d1335294265c65e01e44ccf51 Mon Sep 17 00:00:00 2001 From: Dominik Pötzschke <16013351+dpoetzschke@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:30:56 +0100 Subject: adjust memory allocation --- src/string.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/string.cpp') diff --git a/src/string.cpp b/src/string.cpp index 14f56e4f0..ff20fb11c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -414,7 +414,12 @@ gb_internal String concatenate4_strings(gbAllocator a, String const &x, String c #if defined(GB_SYSTEM_WINDOWS) gb_internal String escape_char(gbAllocator a, String s, char cte) { isize buf_len = s.len; - u8 *buf = gb_alloc_array(a, u8, buf_len); + isize cte_count = 0; + for (isize j = 0; j < s.len; j++) + if (s.text[j] == cte) + cte_count++; + + u8 *buf = gb_alloc_array(a, u8, buf_len+cte_count); isize i = 0; for (isize j = 0; j < s.len; j++) { u8 c = s.text[j]; -- cgit v1.2.3 From f1de4804a5d816d2a65f6e1d3d4faf7d3f340f64 Mon Sep 17 00:00:00 2001 From: Dominik Pötzschke <16013351+dpoetzschke@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:18:12 +0100 Subject: added braces --- src/string.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/string.cpp') diff --git a/src/string.cpp b/src/string.cpp index ff20fb11c..c461602a7 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -415,9 +415,11 @@ gb_internal String concatenate4_strings(gbAllocator a, String const &x, String c gb_internal String escape_char(gbAllocator a, String s, char cte) { isize buf_len = s.len; isize cte_count = 0; - for (isize j = 0; j < s.len; j++) - if (s.text[j] == cte) + for (isize j = 0; j < s.len; j++) { + if (s.text[j] == cte) { cte_count++; + } + } u8 *buf = gb_alloc_array(a, u8, buf_len+cte_count); isize i = 0; -- cgit v1.2.3