menu: cleanup

Remove unused headers, rename `get_unicode_char_lowercase()` to
`read_unicode_char_lowercase()`, explain the meaning of `(gunichar)-1`
and `(gunichar)-2`.
This commit is contained in:
Alex Chernika 2026-05-08 13:10:29 +02:00
parent e3ee330f5f
commit 375ba0f0ff
No known key found for this signature in database
GPG key ID: 6029FAD8ABFB076A

View file

@ -4,14 +4,12 @@
#include <assert.h>
#include <ctype.h>
#include <libxml/parser.h>
#include <locale.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <uchar.h>
#include <unistd.h>
#include <wctype.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
@ -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<u>%.*s</u>%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);
}
}