diff --git a/indexer/categories_holder.cpp b/indexer/categories_holder.cpp index 4cb7c0ab2c..01b54a3c4f 100644 --- a/indexer/categories_holder.cpp +++ b/indexer/categories_holder.cpp @@ -20,8 +20,40 @@ enum State } // unnamed namespace // static -size_t const CategoriesHolder::kNumLanguages = 30; -size_t const CategoriesHolder::kEnglishCode = 1; +int8_t const CategoriesHolder::kEnglishCode = 1; +int8_t const CategoriesHolder::kUnsupportedLocaleCode = -1; +// *NOTE* These constants should be updated when +// adding new translation to categories.txt. +vector const CategoriesHolder::kLocaleMapping = {{"en", 1}, + {"ru", 2}, + {"uk", 3}, + {"de", 4}, + {"fr", 5}, + {"it", 6}, + {"es", 7}, + {"ko", 8}, + {"ja", 9}, + {"cs", 10}, + {"nl", 11}, + {"zh-Hant", 12}, + {"pl", 13}, + {"pt", 14}, + {"hu", 15}, + {"th", 16}, + {"zh-Hans", 17}, + {"ar", 18}, + {"da", 19}, + {"tr", 20}, + {"sk", 21}, + {"sv", 22}, + {"vi", 23}, + {"id", 24}, + {"ro", 25}, + {"nb", 26}, + {"fi", 27}, + {"el", 28}, + {"he", 29}, + {"sw", 30}}; CategoriesHolder::CategoriesHolder(unique_ptr && reader) { @@ -277,27 +309,14 @@ bool CategoriesHolder::IsTypeExist(uint32_t type) const int8_t CategoriesHolder::MapLocaleToInteger(string const & locale) { - struct Mapping + ASSERT(!kLocaleMapping.empty(), ()); + ASSERT_EQUAL(string(kLocaleMapping[0].m_name), "en", ()); + ASSERT_EQUAL(kLocaleMapping[0].m_code, kEnglishCode, ()); + + for (auto const & entry : kLocaleMapping) { - char const * m_name; - int8_t m_code; - }; - // TODO(AlexZ): These constants should be updated when adding new - // translation into categories.txt - static const Mapping mapping[] = { - {"en", 1}, {"ru", 2}, {"uk", 3}, {"de", 4}, {"fr", 5}, {"it", 6}, - {"es", 7}, {"ko", 8}, {"ja", 9}, {"cs", 10}, {"nl", 11}, {"zh-Hant", 12}, - {"pl", 13}, {"pt", 14}, {"hu", 15}, {"th", 16}, {"zh-Hans", 17}, {"ar", 18}, - {"da", 19}, {"tr", 20}, {"sk", 21}, {"sv", 22}, {"vi", 23}, {"id", 24}, - {"ro", 25}, {"nb", 26}, {"fi", 27}, {"el", 28}, {"he", 29}, {"sw", 30}}; - static_assert(ARRAY_SIZE(mapping) == kNumLanguages, ""); - static_assert(CategoriesHolder::kEnglishCode == 1, ""); - ASSERT_EQUAL(string(mapping[0].m_name), "en", ()); - ASSERT_EQUAL(mapping[0].m_code, CategoriesHolder::kEnglishCode, ()); - for (size_t i = 0; i < kNumLanguages; ++i) - { - if (locale.find(mapping[i].m_name) == 0) - return mapping[i].m_code; + if (locale.find(entry.m_name) == 0) + return entry.m_code; } // Special cases for different Chinese variations diff --git a/indexer/categories_holder.hpp b/indexer/categories_holder.hpp index 2677a7e718..14e7f97053 100644 --- a/indexer/categories_holder.hpp +++ b/indexer/categories_holder.hpp @@ -35,6 +35,12 @@ public: } }; + struct Mapping + { + char const * m_name; + int8_t m_code; + }; + private: typedef strings::UniString StringT; typedef multimap > Type2CategoryContT; @@ -45,8 +51,9 @@ private: Name2CatContT m_name2type; public: - static size_t const kNumLanguages; - static size_t const kEnglishCode; + static int8_t const kEnglishCode; + static int8_t const kUnsupportedLocaleCode; + static vector const kLocaleMapping; explicit CategoriesHolder(unique_ptr && reader); void LoadFromStream(istream & s); @@ -114,7 +121,6 @@ public: /// Converts any language locale from UI to internal integer code static int8_t MapLocaleToInteger(string const & locale); - static constexpr int8_t kUnsupportedLocaleCode = -1; private: void AddCategory(Category & cat, vector & types); diff --git a/indexer/categories_index.cpp b/indexer/categories_index.cpp index 3582e6e074..b57559a723 100644 --- a/indexer/categories_index.cpp +++ b/indexer/categories_index.cpp @@ -52,7 +52,8 @@ namespace indexer { void CategoriesIndex::AddCategoryByTypeAndLang(uint32_t type, int8_t lang) { - ASSERT(lang >= 1 && lang <= CategoriesHolder::kNumLanguages, ("Invalid lang code:", lang)); + ASSERT(lang >= 1 && lang <= CategoriesHolder::kLocaleMapping.size(), + ("Invalid lang code:", lang)); m_catHolder->ForEachNameByType(type, [&](TCategory::Name const & name) { if (name.m_locale == lang) @@ -62,13 +63,14 @@ void CategoriesIndex::AddCategoryByTypeAndLang(uint32_t type, int8_t lang) void CategoriesIndex::AddCategoryByTypeAllLangs(uint32_t type) { - for (size_t i = 1; i <= CategoriesHolder::kNumLanguages; ++i) + for (size_t i = 1; i <= CategoriesHolder::kLocaleMapping.size(); ++i) AddCategoryByTypeAndLang(type, i); } void CategoriesIndex::AddAllCategoriesInLang(int8_t lang) { - ASSERT(lang >= 1 && lang <= CategoriesHolder::kNumLanguages, ("Invalid lang code:", lang)); + ASSERT(lang >= 1 && lang <= CategoriesHolder::kLocaleMapping.size(), + ("Invalid lang code:", lang)); m_catHolder->ForEachTypeAndCategory([&](uint32_t type, TCategory const & cat) { for (auto const & name : cat.m_synonyms) diff --git a/indexer/indexer_tests/categories_test.cpp b/indexer/indexer_tests/categories_test.cpp index 2efd94b1b8..5bb18117b8 100644 --- a/indexer/indexer_tests/categories_test.cpp +++ b/indexer/indexer_tests/categories_test.cpp @@ -269,8 +269,10 @@ UNIT_TEST(CategoriesIndex_UniqueNames) editor::EditorConfig config; osm::NewFeatureCategories categories(config); - categories.ForEachLanguage([&](string const & lang) + bool noDuplicates = true; + for (auto const & locale : CategoriesHolder::kLocaleMapping) { + string const lang(locale.m_name); categories.AddLanguage(lang); auto const & names = categories.GetAllCategoryNames(lang); @@ -286,6 +288,7 @@ UNIT_TEST(CategoriesIndex_UniqueNames) { if (names[i - 1].first == names[i].first) { + noDuplicates = false; LOG(LWARNING, (names[i].first, cl.GetReadableObjectName(names[i].second), cl.GetReadableObjectName(names[i - 1].second))); @@ -294,5 +297,7 @@ UNIT_TEST(CategoriesIndex_UniqueNames) LOG(LWARNING, ("+++++++++++++++++++++++++++++++++++++")); } - }); + }; + + TEST(noDuplicates, ()); } diff --git a/indexer/new_feature_categories.hpp b/indexer/new_feature_categories.hpp index 4148b87854..f236e0802f 100644 --- a/indexer/new_feature_categories.hpp +++ b/indexer/new_feature_categories.hpp @@ -48,12 +48,6 @@ public: // The returned list is sorted. TNames const & GetAllCategoryNames(string const & lang) const; - template void ForEachLanguage(TFn && fn) const - { - for (auto const & e : m_categoriesByLang) - fn(e.first); - } - private: indexer::CategoriesIndex m_index; vector m_types;