utf8: automatically determine length of string if SIZE_MAX is specified

Let's make utf8_to_utf16() and utf16_to_utf8() a bit nicer to use by
adding shortcuts for common cases.

This is particularly relevant for utf16_to_utf8() since the
multiplication with 2 is easy to forget.
This commit is contained in:
Lennart Poettering
2023-08-24 13:19:12 +02:00
parent b56ee69233
commit ba091282e5
6 changed files with 30 additions and 9 deletions
+2 -2
View File
@@ -34,7 +34,7 @@ static int parse_timeout(const char *arg1, char16_t **ret_timeout, size_t *ret_t
xsprintf(utf8, USEC_FMT, MIN(timeout / USEC_PER_SEC, UINT32_MAX));
encoded = utf8_to_utf16(utf8, strlen(utf8));
encoded = utf8_to_utf16(utf8, SIZE_MAX);
if (!encoded)
return log_oom();
@@ -69,7 +69,7 @@ static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target
} else if (arg1[0] == '@' && !streq(arg1, "@saved"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unsupported special entry identifier: %s", arg1);
else {
encoded = utf8_to_utf16(arg1, strlen(arg1));
encoded = utf8_to_utf16(arg1, SIZE_MAX);
if (!encoded)
return log_oom();
+1 -1
View File
@@ -152,7 +152,7 @@ TEST(argv_bcds) {
char16_t *title = get_bcd_title(bcd, len);
if (title) {
_cleanup_free_ char *title_utf8 = utf16_to_utf8(title, char16_strlen(title) * 2);
_cleanup_free_ char *title_utf8 = utf16_to_utf8(title, SIZE_MAX);
log_info("%s: \"%s\"", saved_argv[i], title_utf8);
} else
log_info("%s: Bad BCD", saved_argv[i]);