diff --git a/src/quick-lint-js/i18n/locale.cpp b/src/quick-lint-js/i18n/locale.cpp index baa30252e9..a34d2f1419 100644 --- a/src/quick-lint-js/i18n/locale.cpp +++ b/src/quick-lint-js/i18n/locale.cpp @@ -70,10 +70,11 @@ Locale_Parts parse_locale(std::string_view locale_name) { } template -void locale_name_combinations(const char* locale_name, Func&& callback); +void locale_name_combinations(std::string_view locale_name, Func&& callback); } -std::optional find_locale(const char* locales, const char* locale_name) { +std::optional find_locale(const char* locales, + std::string_view locale_name) { // NOTE[locale-list-null-terminator]: The code generator guarantees that there // is an empty string at the end of the list (i.e. two null bytes at the end). C_String_List_View locales_list(locales); @@ -94,7 +95,7 @@ std::optional find_locale(const char* locales, const char* locale_name) { } void enumerate_locale_name_combinations( - const char* locale_name, + std::string_view locale_name, Temporary_Function_Ref callback) { return locale_name_combinations< Temporary_Function_Ref>( @@ -106,11 +107,11 @@ QLJS_WARNING_PUSH QLJS_WARNING_IGNORE_GCC("-Wzero-as-null-pointer-constant") template -void locale_name_combinations(const char* locale_name, Func&& callback) { +void locale_name_combinations(std::string_view locale_name, Func&& callback) { Locale_Parts parts = parse_locale(locale_name); std::vector locale; - std::size_t max_locale_size = std::strlen(locale_name); + std::size_t max_locale_size = locale_name.size(); locale.reserve(max_locale_size); locale.insert(locale.end(), parts.language().begin(), parts.language().end()); diff --git a/src/quick-lint-js/i18n/locale.h b/src/quick-lint-js/i18n/locale.h index 6ce1a3ceac..13e990b608 100644 --- a/src/quick-lint-js/i18n/locale.h +++ b/src/quick-lint-js/i18n/locale.h @@ -13,11 +13,12 @@ namespace quick_lint_js { // // If locales is "en_US\0fr_FR\0de_DE\0", and locale_name is "fr_FR", then the // result will be 1. -std::optional find_locale(const char* locales, const char* locale_name); +std::optional find_locale(const char* locales, + std::string_view locale_name); // For testing only. void enumerate_locale_name_combinations( - const char* locale_name, + std::string_view locale_name, Temporary_Function_Ref); } diff --git a/src/quick-lint-js/i18n/translation.cpp b/src/quick-lint-js/i18n/translation.cpp index 91741303a4..57caf17dc7 100644 --- a/src/quick-lint-js/i18n/translation.cpp +++ b/src/quick-lint-js/i18n/translation.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -22,12 +23,12 @@ namespace quick_lint_js { Translator qljs_messages; namespace { -std::vector split_on(const char* s, char separator) { - std::vector locales; +std::vector split_on(const char* s, char separator) { + std::vector locales; for (;;) { const char* sep = std::strchr(s, separator); if (sep) { - locales.emplace_back(s, sep); + locales.emplace_back(make_string_view(s, sep)); s = sep + 1; } else { locales.emplace_back(s); @@ -37,7 +38,7 @@ std::vector split_on(const char* s, char separator) { return locales; } -std::vector get_user_locale_preferences() { +std::vector get_user_locale_preferences() { // This lookup order roughly mimics GNU gettext. int category = @@ -74,12 +75,12 @@ void initialize_locale() { void initialize_translations_from_environment() { initialize_locale(); if (!qljs_messages.use_messages_from_locales( - Span(get_user_locale_preferences()))) { + Span(get_user_locale_preferences()))) { qljs_messages.use_messages_from_source_code(); } } -void initialize_translations_from_locale(const char* locale_name) { +void initialize_translations_from_locale(std::string_view locale_name) { initialize_locale(); if (!qljs_messages.use_messages_from_locale(locale_name)) { qljs_messages.use_messages_from_source_code(); @@ -91,7 +92,7 @@ void Translator::use_messages_from_source_code() { this->locale_index_ = translation_table_locale_count; } -bool Translator::use_messages_from_locale(const char* locale_name) { +bool Translator::use_messages_from_locale(std::string_view locale_name) { std::optional locale_index = find_locale(translation_data.locale_table, locale_name); if (locale_index.has_value()) { @@ -102,13 +103,13 @@ bool Translator::use_messages_from_locale(const char* locale_name) { } bool Translator::use_messages_from_locales( - Span locale_names) { - for (const std::string& locale : locale_names) { - if (locale == "C" || locale == "POSIX") { + Span locale_names) { + for (const std::string_view& locale : locale_names) { + if (locale == "C"sv || locale == "POSIX"sv) { // Stop seaching. C/POSIX locale takes priority. See GNU gettext. break; } - bool found_messages = this->use_messages_from_locale(locale.c_str()); + bool found_messages = this->use_messages_from_locale(locale); if (found_messages) { return true; } diff --git a/src/quick-lint-js/i18n/translation.h b/src/quick-lint-js/i18n/translation.h index 4bf0189f21..6b605138fc 100644 --- a/src/quick-lint-js/i18n/translation.h +++ b/src/quick-lint-js/i18n/translation.h @@ -19,7 +19,7 @@ namespace quick_lint_js { class Translatable_Message; void initialize_translations_from_environment(); -void initialize_translations_from_locale(const char* locale_name); +void initialize_translations_from_locale(std::string_view locale_name); class Translator { public: @@ -27,8 +27,8 @@ class Translator { explicit Translator() = default; void use_messages_from_source_code(); - bool use_messages_from_locale(const char* locale_name); - bool use_messages_from_locales(Span locale_names); + bool use_messages_from_locale(std::string_view locale_name); + bool use_messages_from_locales(Span locale_names); const Char8* translate(const Translatable_Message& message);