From b55a65feaac5eb1df6e7798e3fe3b5b98d3db841 Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Fri, 27 Oct 2023 14:00:32 -0400 Subject: [PATCH] refactor(i18n): remove std::vector from public API Reduce our use of std::vector in non-test code to reduce binary bloat: https://github.com/quick-lint/quick-lint-js/issues/689 --- src/quick-lint-js/i18n/locale.cpp | 14 ++++++-------- src/quick-lint-js/i18n/locale.h | 9 ++++++--- test/test-locale.cpp | 10 ++++++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/quick-lint-js/i18n/locale.cpp b/src/quick-lint-js/i18n/locale.cpp index 2ecd1a2692..baa30252e9 100644 --- a/src/quick-lint-js/i18n/locale.cpp +++ b/src/quick-lint-js/i18n/locale.cpp @@ -93,14 +93,12 @@ std::optional find_locale(const char* locales, const char* locale_name) { return found_entry; } -std::vector locale_name_combinations(const char* locale_name) { - std::vector 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 callback) { + return locale_name_combinations< + Temporary_Function_Ref>( + locale_name, std::move(callback)); } namespace { diff --git a/src/quick-lint-js/i18n/locale.h b/src/quick-lint-js/i18n/locale.h index 28d5445245..6ce1a3ceac 100644 --- a/src/quick-lint-js/i18n/locale.h +++ b/src/quick-lint-js/i18n/locale.h @@ -5,8 +5,8 @@ #include #include -#include -#include +#include +#include namespace quick_lint_js { // Returns the index of the matching locale. @@ -15,7 +15,10 @@ namespace quick_lint_js { // result will be 1. std::optional find_locale(const char* locales, const char* locale_name); -std::vector locale_name_combinations(const char* locale_name); +// For testing only. +void enumerate_locale_name_combinations( + const char* locale_name, + Temporary_Function_Ref); } // quick-lint-js finds bugs in JavaScript programs. diff --git a/test/test-locale.cpp b/test/test-locale.cpp index 028b7676ba..9f94283ef6 100644 --- a/test/test-locale.cpp +++ b/test/test-locale.cpp @@ -11,6 +11,16 @@ using namespace std::literals::string_view_literals; namespace quick_lint_js { namespace { +std::vector locale_name_combinations(const char* locale_name) { + std::vector 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"})); }