From 8064a3cbdb859b294438593ce25e06f1abecc8ec Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Sat, 28 Oct 2023 18:16:13 -0400 Subject: [PATCH] refactor(i18n): char* -> std::string_view Prefer std::string_view over char* so make it clear that null terminators are not required. --- src/quick-lint-js/i18n/locale.cpp | 11 ++++++----- src/quick-lint-js/i18n/locale.h | 5 +++-- src/quick-lint-js/i18n/translation.cpp | 18 +++++++++--------- src/quick-lint-js/i18n/translation.h | 4 ++-- 4 files changed, 20 insertions(+), 18 deletions(-) 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..1eb55ce1b0 100644 --- a/src/quick-lint-js/i18n/translation.cpp +++ b/src/quick-lint-js/i18n/translation.cpp @@ -22,8 +22,8 @@ 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) { @@ -37,7 +37,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,7 +74,7 @@ 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(); } } @@ -91,7 +91,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 +102,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..55cd5ae223 100644 --- a/src/quick-lint-js/i18n/translation.h +++ b/src/quick-lint-js/i18n/translation.h @@ -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);