From c2925f5f11158d9892f78c4316c79e6e1ac1fc32 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 26 Aug 2023 23:19:11 -0700 Subject: [PATCH] propername: tune single-byte code * lib/propername.c (mbsstr_trimmed_wordbounded): Cache MB_CUR_MAX. Simplify word boundary detection in single-byte code. --- ChangeLog | 6 ++++++ lib/propername.c | 27 +++++++++------------------ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index de52b57755..64db236853 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2023-08-26 Paul Eggert + + propername: tune single-byte code + * lib/propername.c (mbsstr_trimmed_wordbounded): Cache MB_CUR_MAX. + Simplify word boundary detection in single-byte code. + 2023-08-26 Paul Eggert Tune single-byte code involving tolower diff --git a/lib/propername.c b/lib/propername.c index f9eae490db..dbd96b8772 100644 --- a/lib/propername.c +++ b/lib/propername.c @@ -55,15 +55,19 @@ mbsstr_trimmed_wordbounded (const char *string, const char *sub) { char *tsub = trim (sub); bool found = false; + bool multibyte_locale = MB_CUR_MAX > 1; + size_t tsublen; + if (! multibyte_locale) + tsublen = strlen (tsub); - for (; *string != '\0';) + while (*string != '\0') { const char *tsub_in_string = mbsstr (string, tsub); if (tsub_in_string == NULL) break; else { - if (MB_CUR_MAX > 1) + if (multibyte_locale) { mbui_iterator_t string_iter; bool word_boundary_before; @@ -120,22 +124,9 @@ mbsstr_trimmed_wordbounded (const char *string, const char *sub) } else { - bool word_boundary_before; - const char *p; - bool word_boundary_after; - - word_boundary_before = true; - if (string < tsub_in_string) - if (isalnum ((unsigned char) tsub_in_string[-1])) - word_boundary_before = false; - - p = tsub_in_string + strlen (tsub); - word_boundary_after = true; - if (*p != '\0') - if (isalnum ((unsigned char) *p)) - word_boundary_after = false; - - if (word_boundary_before && word_boundary_after) + if ((string == tsub_in_string + || !isalnum ((unsigned char) tsub_in_string[-1])) + && !isalnum ((unsigned char) tsub_in_string[tsublen])) { found = true; break; -- 2.39.5