Skip to content

Commit

Permalink
refactor(i18n): remove std::vector from public API
Browse files Browse the repository at this point in the history
Reduce our use of std::vector in non-test code to reduce binary bloat:
#689
  • Loading branch information
strager committed Oct 28, 2023
1 parent 0e74e44 commit 9ca55dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/quick-lint-js/i18n/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ std::optional<int> find_locale(const char* locales, const char* locale_name) {
return found_entry;
}

std::vector<std::string> locale_name_combinations(const char* locale_name) {
std::vector<std::string> locale_names;
locale_name_combinations(locale_name,
[&](std::string_view current_locale) -> bool {
locale_names.emplace_back(current_locale);
return true;
});
return locale_names;
void enumerate_locale_name_combinations(
const char* locale_name,
Temporary_Function_Ref<bool(std::string_view locale)> callback) {
return locale_name_combinations<
Temporary_Function_Ref<bool(std::string_view locale)>>(
locale_name, std::move(callback));
}

namespace {
Expand Down
9 changes: 6 additions & 3 deletions src/quick-lint-js/i18n/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <cstring>
#include <optional>
#include <string>
#include <vector>
#include <quick-lint-js/port/function-ref.h>
#include <string_view>

namespace quick_lint_js {
// Returns the index of the matching locale.
Expand All @@ -15,7 +15,10 @@ namespace quick_lint_js {
// result will be 1.
std::optional<int> find_locale(const char* locales, const char* locale_name);

std::vector<std::string> locale_name_combinations(const char* locale_name);
// For testing only.
void enumerate_locale_name_combinations(
const char* locale_name,
Temporary_Function_Ref<bool(std::string_view locale)>);
}

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
10 changes: 10 additions & 0 deletions test/test-locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ using namespace std::literals::string_view_literals;

namespace quick_lint_js {
namespace {
std::vector<std::string> locale_name_combinations(const char* locale_name) {
std::vector<std::string> locale_names;
enumerate_locale_name_combinations(
locale_name, [&](std::string_view current_locale) -> bool {
locale_names.emplace_back(current_locale);
return true;
});
return locale_names;
}

TEST(Test_Locale, combinations_for_language) {
EXPECT_THAT(locale_name_combinations("en"), ElementsAreArray({"en"}));
}
Expand Down

0 comments on commit 9ca55dc

Please sign in to comment.