diff --git a/src/normalize.c b/src/normalize.c index f8f241e3..4eeda0f9 100644 --- a/src/normalize.c +++ b/src/normalize.c @@ -10,10 +10,13 @@ char *normalize_string_utf8(char *str, uint64_t options) { bool have_utf8proc_options = false; + char *normalized = NULL; + if (options & NORMALIZE_STRING_TRIM) { char *trimmed = string_trim(str); if (trimmed != NULL) { - str = trimmed; + normalized = trimmed; + str = normalized; } } @@ -37,8 +40,6 @@ char *normalize_string_utf8(char *str, uint64_t options) { utf8proc_options |= UTF8PROC_OPTIONS_LOWERCASE; } - char *normalized = NULL; - if (have_utf8proc_options) { utf8proc_map((uint8_t *)str, 0, &utf8proc_normalized, utf8proc_options); @@ -46,9 +47,11 @@ char *normalize_string_utf8(char *str, uint64_t options) { str = normalized; } - if (options & NORMALIZE_STRING_REPLACE_HYPHENS) { - string_replace(str, '-', ' '); - normalized = str; + if (options & NORMALIZE_STRING_REPLACE_HYPHENS && strchr(str, '-') != NULL) { + char *replaced = string_replace(str, '-', ' '); + if (replaced != NULL) { + normalized = replaced; + } } return normalized; diff --git a/src/string_utils.c b/src/string_utils.c index 12f483bf..ce5ea30f 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -80,10 +80,14 @@ inline void string_upper(char *s) { for (; *s; ++s) *s = toupper(*s); } -inline void string_replace(char *s, char c1, char c2) { - for (; *s; ++s) { - if (*s == c1) *s = c2; +inline char *string_replace(char *s, char c1, char c2) { + char *repl = strdup(s); + if (repl == NULL) return NULL; + char *ptr = repl; + for (; *ptr; ++ptr) { + if (*ptr == c1) *ptr = c2; } + return repl; } inline bool string_is_upper(char *s) { diff --git a/src/string_utils.h b/src/string_utils.h index 2295316c..67a65e72 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -55,7 +55,7 @@ void string_lower(char *s); bool string_is_upper(char *s); void string_upper(char *s); -void string_replace(char *s, char c1, char c2); +char *string_replace(char *s, char c1, char c2); bool string_starts_with(const char *str, const char *start); bool string_ends_with(const char *str, const char *ending);