Skip to content

Commit

Permalink
refactor(i18n): char* -> std::string_view
Browse files Browse the repository at this point in the history
Prefer std::string_view over char* so make it clear that null
terminators are not required.
  • Loading branch information
strager committed Oct 28, 2023
1 parent f74d834 commit 8064a3c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/quick-lint-js/i18n/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ Locale_Parts parse_locale(std::string_view locale_name) {
}

template <class Func>
void locale_name_combinations(const char* locale_name, Func&& callback);
void locale_name_combinations(std::string_view locale_name, Func&& callback);
}

std::optional<int> find_locale(const char* locales, const char* locale_name) {
std::optional<int> 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);
Expand All @@ -94,7 +95,7 @@ std::optional<int> 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<bool(std::string_view locale)> callback) {
return locale_name_combinations<
Temporary_Function_Ref<bool(std::string_view locale)>>(
Expand All @@ -106,11 +107,11 @@ QLJS_WARNING_PUSH
QLJS_WARNING_IGNORE_GCC("-Wzero-as-null-pointer-constant")

template <class Func>
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<char> 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());

Expand Down
5 changes: 3 additions & 2 deletions src/quick-lint-js/i18n/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> find_locale(const char* locales, const char* locale_name);
std::optional<int> 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<bool(std::string_view locale)>);
}

Expand Down
18 changes: 9 additions & 9 deletions src/quick-lint-js/i18n/translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace quick_lint_js {
Translator qljs_messages;

namespace {
std::vector<std::string> split_on(const char* s, char separator) {
std::vector<std::string> locales;
std::vector<std::string_view> split_on(const char* s, char separator) {
std::vector<std::string_view> locales;
for (;;) {
const char* sep = std::strchr(s, separator);
if (sep) {
Expand All @@ -37,7 +37,7 @@ std::vector<std::string> split_on(const char* s, char separator) {
return locales;
}

std::vector<std::string> get_user_locale_preferences() {
std::vector<std::string_view> get_user_locale_preferences() {
// This lookup order roughly mimics GNU gettext.

int category =
Expand Down Expand Up @@ -74,7 +74,7 @@ void initialize_locale() {
void initialize_translations_from_environment() {
initialize_locale();
if (!qljs_messages.use_messages_from_locales(
Span<const std::string>(get_user_locale_preferences()))) {
Span<const std::string_view>(get_user_locale_preferences()))) {
qljs_messages.use_messages_from_source_code();
}
}
Expand All @@ -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<int> locale_index =
find_locale(translation_data.locale_table, locale_name);
if (locale_index.has_value()) {
Expand All @@ -102,13 +102,13 @@ bool Translator::use_messages_from_locale(const char* locale_name) {
}

bool Translator::use_messages_from_locales(
Span<const std::string> locale_names) {
for (const std::string& locale : locale_names) {
if (locale == "C" || locale == "POSIX") {
Span<const std::string_view> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/quick-lint-js/i18n/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::string> locale_names);
bool use_messages_from_locale(std::string_view locale_name);
bool use_messages_from_locales(Span<const std::string_view> locale_names);

const Char8* translate(const Translatable_Message& message);

Expand Down

0 comments on commit 8064a3c

Please sign in to comment.