diff --git a/src/menu/menu.c b/src/menu/menu.c index ded574ed..2505317d 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -4,14 +4,12 @@ #include #include #include -#include #include +#include #include #include #include -#include #include -#include #include #include #include @@ -133,25 +131,28 @@ validate(void) } } -/* Extract a single Unicode codepoint and convert it to lowercase */ +/* Read a single Unicode codepoint and convert it to lowercase */ static uint32_t -get_unicode_char_lowercase(const char *first_byte, size_t *out_bytes) +read_unicode_char_lowercase(const char *first_byte, size_t *bytes_read) { - if (!first_byte || first_byte[0] == '\0' || !out_bytes) { - if (out_bytes) { - *out_bytes = 0; - } + assert(bytes_read); + + if (string_null_or_empty(first_byte)) { + *bytes_read = 0; return 0; } gunichar codepoint = g_utf8_get_char_validated(first_byte, -1); - if (codepoint == (gunichar)-1 || codepoint == (gunichar)-2) { - *out_bytes = 1; + bool partial_read = (codepoint == (gunichar)-2); + bool failed_read = (codepoint == (gunichar)-1); + if (partial_read || failed_read) { + /* Read only the first byte */ + *bytes_read = 1; return (uint32_t)(unsigned char)first_byte[0]; } - *out_bytes = (size_t)(g_utf8_next_char(first_byte) - first_byte); + *bytes_read = (size_t)(g_utf8_next_char(first_byte) - first_byte); return (uint32_t)g_unichar_tolower(codepoint); } @@ -176,22 +177,22 @@ item_parse_accelerator(struct menuitem *item, const char *text) } } - size_t bytes = 0; + size_t bytes_read = 0; if (!accel_ptr) { /* Default to the first char */ item->text = xstrdup(text); - item->accelerator = get_unicode_char_lowercase(text, &bytes); + item->accelerator = read_unicode_char_lowercase(text, &bytes_read); } else { /* Set the accelerator and remove the preceding underscore */ item->use_markup = true; - item->accelerator = get_unicode_char_lowercase(accel_ptr, &bytes); + item->accelerator = read_unicode_char_lowercase(accel_ptr, &bytes_read); item->text = strdup_printf("%.*s%.*s%s", /* Prefix length + prefix */ (int)(accel_ptr - 1 - text), text, /* Accelerator (utf-8 byte) length + accelerator */ - (int)bytes, accel_ptr, + (int)bytes_read, accel_ptr, /* Remainder */ - accel_ptr + bytes); + accel_ptr + bytes_read); } }