From 0c9711efbab65e7dde7aa9cc102a3a2aa55c5ebc Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 13 Nov 2021 10:03:17 -0500 Subject: [PATCH] LibUnicode: Handle all space code points when creating currency patterns Previously, we were checking if the code point immediately before/after the {currency} key was U+00A0 (non-breaking space). Instead, to handle other spacing code points, we must check if the surrounding code point has the separator general category. --- Userland/Libraries/LibUnicode/Locale.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp index b09783126d..411fe1d0ca 100644 --- a/Userland/Libraries/LibUnicode/Locale.cpp +++ b/Userland/Libraries/LibUnicode/Locale.cpp @@ -1013,18 +1013,30 @@ String create_currency_format_pattern(StringView currency_display, StringView ba Utf8View utf8_currency_display { currency_display }; Optional currency_display_with_spacing; + auto last_code_point = [](StringView string) { + Utf8View utf8_string { string }; + u32 code_point = 0; + + for (auto it = utf8_string.begin(); it != utf8_string.end(); ++it) + code_point = *it; + + return code_point; + }; + if (*number_index < *currency_index) { - if (!base_pattern.substring_view(0, *currency_index).ends_with(spacing)) { + u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *currency_index)); + + if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) { u32 first_currency_code_point = *utf8_currency_display.begin(); if (!code_point_has_general_category(first_currency_code_point, GeneralCategory::Symbol)) currency_display_with_spacing = String::formatted("{}{}", spacing, currency_display); } } else { - if (!base_pattern.substring_view(0, *number_index).ends_with(spacing)) { - u32 last_currency_code_point = 0; - for (auto it = utf8_currency_display.begin(); it != utf8_currency_display.end(); ++it) - last_currency_code_point = *it; + u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index)); + + if (!code_point_has_general_category(last_pattern_code_point, GeneralCategory::Separator)) { + u32 last_currency_code_point = last_code_point(currency_display); if (!code_point_has_general_category(last_currency_code_point, GeneralCategory::Symbol)) currency_display_with_spacing = String::formatted("{}{}", currency_display, spacing);