Commit e7f50c96 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Remove SIs in rules_data.cc.

The global |kRawData| std::map constant have to be initialized when the
program starts. Instead, make it a static local by wrapping it in a
GetRawData() function so the map is initialized on first use instead.
This remove 14 static initializers.

Along the way, make some globals fully const, use |kKeyMapCount| in more
places instead of hardcoding its value, simplify some code, and make the
modified files free of lint errors.

Bug: 537099
Change-Id: I1b6d059f608db108bb4ecb672119a5f43634bef5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1824067Reviewed-by: default avatarShu Chen <shuchen@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706278}
parent a991bcfd
...@@ -69,113 +69,83 @@ struct RawDataEntry { ...@@ -69,113 +69,83 @@ struct RawDataEntry {
history_prune(prune) {} history_prune(prune) {}
}; };
static const std::map<std::string, RawDataEntry> kRawData = { const std::map<std::string, RawDataEntry>& GetRawData() {
static const std::map<std::string, RawDataEntry> kRawData = {
{ar::kId, RawDataEntry(ar::kKeyMap, ar::kIs102)}, {ar::kId, RawDataEntry(ar::kKeyMap, ar::kIs102)},
{bn_phone::kId, RawDataEntry(bn_phone::kKeyMap, {bn_phone::kId,
bn_phone::kIs102, RawDataEntry(bn_phone::kKeyMap, bn_phone::kIs102, bn_phone::kTransforms,
bn_phone::kTransforms, bn_phone::kTransformsLen, bn_phone::kHistoryPrune)},
bn_phone::kTransformsLen,
bn_phone::kHistoryPrune)},
{ckb_ar::kId, RawDataEntry(ckb_ar::kKeyMap, ckb_ar::kIs102)}, {ckb_ar::kId, RawDataEntry(ckb_ar::kKeyMap, ckb_ar::kIs102)},
{ckb_en::kId, RawDataEntry(ckb_en::kKeyMap, ckb_en::kIs102)}, {ckb_en::kId, RawDataEntry(ckb_en::kKeyMap, ckb_en::kIs102)},
{deva_phone::kId, RawDataEntry(us::kKeyMap, {deva_phone::kId,
us::kIs102, RawDataEntry(us::kKeyMap, us::kIs102, deva_phone::kTransforms,
deva_phone::kTransforms, deva_phone::kTransformsLen, deva_phone::kHistoryPrune)},
deva_phone::kTransformsLen, {ethi::kId, RawDataEntry(ethi::kKeyMap, ethi::kIs102, ethi::kTransforms,
deva_phone::kHistoryPrune)}, ethi::kTransformsLen, ethi::kHistoryPrune)},
{ethi::kId, RawDataEntry(ethi::kKeyMap,
ethi::kIs102,
ethi::kTransforms,
ethi::kTransformsLen,
ethi::kHistoryPrune)},
{fa::kId, RawDataEntry(fa::kKeyMap, fa::kIs102)}, {fa::kId, RawDataEntry(fa::kKeyMap, fa::kIs102)},
{gu_phone::kId, RawDataEntry(gu_phone::kKeyMap, {gu_phone::kId,
gu_phone::kIs102, RawDataEntry(gu_phone::kKeyMap, gu_phone::kIs102, gu_phone::kTransforms,
gu_phone::kTransforms, gu_phone::kTransformsLen, gu_phone::kHistoryPrune)},
gu_phone::kTransformsLen,
gu_phone::kHistoryPrune)},
{km::kId, RawDataEntry(km::kKeyMap, km::kIs102)}, {km::kId, RawDataEntry(km::kKeyMap, km::kIs102)},
{kn_phone::kId, RawDataEntry(us::kKeyMap, {kn_phone::kId,
us::kIs102, RawDataEntry(us::kKeyMap, us::kIs102, kn_phone::kTransforms,
kn_phone::kTransforms, kn_phone::kTransformsLen, kn_phone::kHistoryPrune)},
kn_phone::kTransformsLen,
kn_phone::kHistoryPrune)},
{lo::kId, RawDataEntry(lo::kKeyMap, lo::kIs102)}, {lo::kId, RawDataEntry(lo::kKeyMap, lo::kIs102)},
{ml_phone::kId, RawDataEntry(us::kKeyMap, {ml_phone::kId,
us::kIs102, RawDataEntry(us::kKeyMap, us::kIs102, ml_phone::kTransforms,
ml_phone::kTransforms, ml_phone::kTransformsLen, ml_phone::kHistoryPrune)},
ml_phone::kTransformsLen, {my::kId, RawDataEntry(my::kKeyMap, my::kIs102, my::kTransforms,
ml_phone::kHistoryPrune)}, my::kTransformsLen, my::kHistoryPrune)},
{my::kId, RawDataEntry(my::kKeyMap, {my_myansan::kId,
my::kIs102, RawDataEntry(my_myansan::kKeyMap, my_myansan::kIs102,
my::kTransforms, my_myansan::kTransforms, my_myansan::kTransformsLen,
my::kTransformsLen,
my::kHistoryPrune)},
{my_myansan::kId, RawDataEntry(my_myansan::kKeyMap,
my_myansan::kIs102,
my_myansan::kTransforms,
my_myansan::kTransformsLen,
my_myansan::kHistoryPrune)}, my_myansan::kHistoryPrune)},
{ne_inscript::kId, RawDataEntry(ne_inscript::kKeyMap, ne_inscript::kIs102)}, {ne_inscript::kId,
RawDataEntry(ne_inscript::kKeyMap, ne_inscript::kIs102)},
{ne_phone::kId, RawDataEntry(ne_phone::kKeyMap, ne_phone::kIs102)}, {ne_phone::kId, RawDataEntry(ne_phone::kKeyMap, ne_phone::kIs102)},
{ru_phone_aatseel::kId, {ru_phone_aatseel::kId,
RawDataEntry(ru_phone_aatseel::kKeyMap, ru_phone_aatseel::kIs102)}, RawDataEntry(ru_phone_aatseel::kKeyMap, ru_phone_aatseel::kIs102)},
{ru_phone_yazhert::kId, {ru_phone_yazhert::kId,
RawDataEntry(ru_phone_yazhert::kKeyMap, ru_phone_yazhert::kIs102)}, RawDataEntry(ru_phone_yazhert::kKeyMap, ru_phone_yazhert::kIs102)},
{si::kId, RawDataEntry(si::kKeyMap, {si::kId, RawDataEntry(si::kKeyMap, si::kIs102, si::kTransforms,
si::kIs102, si::kTransformsLen, si::kHistoryPrune)},
si::kTransforms, {ta_inscript::kId,
si::kTransformsLen, RawDataEntry(ta_inscript::kKeyMap, ta_inscript::kIs102)},
si::kHistoryPrune)}, {ta_itrans::kId,
{ta_inscript::kId, RawDataEntry(ta_inscript::kKeyMap, ta_inscript::kIs102)}, RawDataEntry(us::kKeyMap, us::kIs102, ta_itrans::kTransforms,
{ta_itrans::kId, RawDataEntry(us::kKeyMap, ta_itrans::kTransformsLen, ta_itrans::kHistoryPrune)},
us::kIs102, {ta_phone::kId,
ta_itrans::kTransforms, RawDataEntry(ta_phone::kKeyMap, ta_phone::kIs102, ta_phone::kTransforms,
ta_itrans::kTransformsLen, ta_phone::kTransformsLen, ta_phone::kHistoryPrune)},
ta_itrans::kHistoryPrune)}, {ta_tamil99::kId,
{ta_phone::kId, RawDataEntry(ta_phone::kKeyMap, RawDataEntry(ta_tamil99::kKeyMap, ta_tamil99::kIs102,
ta_phone::kIs102, ta_tamil99::kTransforms, ta_tamil99::kTransformsLen,
ta_phone::kTransforms,
ta_phone::kTransformsLen,
ta_phone::kHistoryPrune)},
{ta_tamil99::kId, RawDataEntry(ta_tamil99::kKeyMap,
ta_tamil99::kIs102,
ta_tamil99::kTransforms,
ta_tamil99::kTransformsLen,
ta_tamil99::kHistoryPrune)}, ta_tamil99::kHistoryPrune)},
{ta_typewriter::kId, {ta_typewriter::kId,
RawDataEntry(ta_typewriter::kKeyMap, ta_typewriter::kIs102)}, RawDataEntry(ta_typewriter::kKeyMap, ta_typewriter::kIs102)},
{te_phone::kId, RawDataEntry(us::kKeyMap, {te_phone::kId,
us::kIs102, RawDataEntry(us::kKeyMap, us::kIs102, te_phone::kTransforms,
te_phone::kTransforms, te_phone::kTransformsLen, te_phone::kHistoryPrune)},
te_phone::kTransformsLen,
te_phone::kHistoryPrune)},
{th::kId, RawDataEntry(th::kKeyMap, th::kIs102)}, {th::kId, RawDataEntry(th::kKeyMap, th::kIs102)},
{th_pattajoti::kId, {th_pattajoti::kId,
RawDataEntry(th_pattajoti::kKeyMap, th_pattajoti::kIs102)}, RawDataEntry(th_pattajoti::kKeyMap, th_pattajoti::kIs102)},
{th_tis::kId, RawDataEntry(th_tis::kKeyMap, th_tis::kIs102)}, {th_tis::kId, RawDataEntry(th_tis::kKeyMap, th_tis::kIs102)},
{vi_tcvn::kId, RawDataEntry(us::kKeyMap, {vi_tcvn::kId,
us::kIs102, RawDataEntry(us::kKeyMap, us::kIs102, vi_tcvn::kTransforms,
vi_tcvn::kTransforms, vi_tcvn::kTransformsLen, vi_tcvn::kHistoryPrune)},
vi_tcvn::kTransformsLen, {vi_telex::kId,
vi_tcvn::kHistoryPrune)}, RawDataEntry(us::kKeyMap, us::kIs102, vi_telex::kTransforms,
{vi_telex::kId, RawDataEntry(us::kKeyMap, vi_telex::kTransformsLen, vi_telex::kHistoryPrune)},
us::kIs102, {vi_viqr::kId,
vi_telex::kTransforms, RawDataEntry(us::kKeyMap, us::kIs102, vi_viqr::kTransforms,
vi_telex::kTransformsLen, vi_viqr::kTransformsLen, vi_viqr::kHistoryPrune)},
vi_telex::kHistoryPrune)}, {vi_vni::kId,
{vi_viqr::kId, RawDataEntry(us::kKeyMap, RawDataEntry(us::kKeyMap, us::kIs102, vi_vni::kTransforms,
us::kIs102, vi_vni::kTransformsLen, vi_vni::kHistoryPrune)}};
vi_viqr::kTransforms, return kRawData;
vi_viqr::kTransformsLen, }
vi_viqr::kHistoryPrune)},
{vi_vni::kId, RawDataEntry(us::kKeyMap, const char* const k101Keys[] = {
us::kIs102,
vi_vni::kTransforms,
vi_vni::kTransformsLen,
vi_vni::kHistoryPrune)}};
static const char* k101Keys[] = {
// Row #1 // Row #1
"Backquote", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Backquote", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6",
"Digit7", "Digit8", "Digit9", "Digit0", "Minus", "Equal", "Digit7", "Digit8", "Digit9", "Digit0", "Minus", "Equal",
...@@ -191,7 +161,7 @@ static const char* k101Keys[] = { ...@@ -191,7 +161,7 @@ static const char* k101Keys[] = {
// Row #5 // Row #5
"Space"}; "Space"};
static const char* k102Keys[] = { const char* const k102Keys[] = {
// Row #1 // Row #1
"Backquote", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Backquote", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6",
"Digit7", "Digit8", "Digit9", "Digit0", "Minus", "Equal", "Digit7", "Digit8", "Digit9", "Digit0", "Minus", "Equal",
...@@ -207,11 +177,9 @@ static const char* k102Keys[] = { ...@@ -207,11 +177,9 @@ static const char* k102Keys[] = {
// Row #5 // Row #5
"Space"}; "Space"};
const static size_t kKeyMapCount = 8;
// Parses the raw key mappings and generate a KeyMap instance. // Parses the raw key mappings and generate a KeyMap instance.
KeyMap ParseKeyMap(const char** raw_key_map, bool is_102) { KeyMap ParseKeyMap(const char** raw_key_map, bool is_102) {
const char** std_keys = is_102 ? k102Keys : k101Keys; const char* const* std_keys = is_102 ? k102Keys : k101Keys;
size_t nkeys = is_102 ? base::size(k102Keys) : base::size(k101Keys); size_t nkeys = is_102 ? base::size(k102Keys) : base::size(k101Keys);
KeyMap key_map; KeyMap key_map;
for (size_t i = 0; i < nkeys; ++i) for (size_t i = 0; i < nkeys; ++i)
...@@ -286,7 +254,7 @@ std::string Prefixalize(const std::string& re_str) { ...@@ -286,7 +254,7 @@ std::string Prefixalize(const std::string& re_str) {
std::pair<std::unique_ptr<re2::RE2>, std::unique_ptr<re2::RE2>> ParseTransforms( std::pair<std::unique_ptr<re2::RE2>, std::unique_ptr<re2::RE2>> ParseTransforms(
const char** raw_transforms, const char** raw_transforms,
uint16_t trans_count, uint16_t trans_count,
std::map<uint16_t, TransformRule>& re_map) { std::map<uint16_t, TransformRule>* re_map) {
if (!trans_count) if (!trans_count)
return std::make_pair(nullptr, nullptr); return std::make_pair(nullptr, nullptr);
...@@ -304,7 +272,7 @@ std::pair<std::unique_ptr<re2::RE2>, std::unique_ptr<re2::RE2>> ParseTransforms( ...@@ -304,7 +272,7 @@ std::pair<std::unique_ptr<re2::RE2>, std::unique_ptr<re2::RE2>> ParseTransforms(
all_trans += "(" + from + "$)|"; all_trans += "(" + from + "$)|";
all_prefixes += Prefixalize(from) + "|"; all_prefixes += Prefixalize(from) + "|";
re_map[sum_of_groups] = std::make_pair(std::move(from_re), to); (*re_map)[sum_of_groups] = std::make_pair(std::move(from_re), to);
sum_of_groups += group_count + 1; sum_of_groups += group_count + 1;
} }
return std::make_pair( return std::make_pair(
...@@ -342,7 +310,7 @@ std::unique_ptr<RulesData> RulesData::Create(const char*** key_map, ...@@ -342,7 +310,7 @@ std::unique_ptr<RulesData> RulesData::Create(const char*** key_map,
data->key_maps_[i] = ParseKeyMap(key_map[i], is_102_keyboard); data->key_maps_[i] = ParseKeyMap(key_map[i], is_102_keyboard);
} }
auto regexes = auto regexes =
ParseTransforms(transforms, transforms_count, data->transform_rules_); ParseTransforms(transforms, transforms_count, &data->transform_rules_);
data->transform_re_merged_ = std::move(regexes.first); data->transform_re_merged_ = std::move(regexes.first);
data->prefix_re_ = std::move(regexes.second); data->prefix_re_ = std::move(regexes.second);
data->history_prune_re_ = ParseHistoryPrune(history_prune); data->history_prune_re_ = ParseHistoryPrune(history_prune);
...@@ -351,8 +319,9 @@ std::unique_ptr<RulesData> RulesData::Create(const char*** key_map, ...@@ -351,8 +319,9 @@ std::unique_ptr<RulesData> RulesData::Create(const char*** key_map,
// static // static
std::unique_ptr<RulesData> RulesData::GetById(const std::string& id) { std::unique_ptr<RulesData> RulesData::GetById(const std::string& id) {
auto it = kRawData.find(id); const std::map<std::string, RawDataEntry>& raw_data = GetRawData();
if (it == kRawData.end()) auto it = raw_data.find(id);
if (it == raw_data.end())
return nullptr; return nullptr;
const RawDataEntry& entry = it->second; const RawDataEntry& entry = it->second;
...@@ -362,11 +331,11 @@ std::unique_ptr<RulesData> RulesData::GetById(const std::string& id) { ...@@ -362,11 +331,11 @@ std::unique_ptr<RulesData> RulesData::GetById(const std::string& id) {
// static // static
bool RulesData::IsIdSupported(const std::string& id) { bool RulesData::IsIdSupported(const std::string& id) {
return kRawData.find(id) != kRawData.end(); return base::Contains(GetRawData(), id);
} }
const KeyMap* RulesData::GetKeyMapByModifiers(uint8_t modifiers) const { const KeyMap* RulesData::GetKeyMapByModifiers(uint8_t modifiers) const {
return modifiers < 8 ? &key_maps_[modifiers] : nullptr; return modifiers < kKeyMapCount ? &key_maps_[modifiers] : nullptr;
} }
bool RulesData::Transform(const std::string& context, bool RulesData::Transform(const std::string& context,
...@@ -420,9 +389,7 @@ bool RulesData::Transform(const std::string& context, ...@@ -420,9 +389,7 @@ bool RulesData::Transform(const std::string& context,
} }
bool RulesData::MatchHistoryPrune(const std::string& str) const { bool RulesData::MatchHistoryPrune(const std::string& str) const {
if (!history_prune_re_) return history_prune_re_ && re2::RE2::FullMatch(str, *history_prune_re_);
return false;
return re2::RE2::FullMatch(str, *history_prune_re_);
} }
bool RulesData::PredictTransform(const std::string& str, int transat) const { bool RulesData::PredictTransform(const std::string& str, int transat) const {
......
...@@ -6,8 +6,9 @@ ...@@ -6,8 +6,9 @@
#define CHROMEOS_SERVICES_IME_PUBLIC_CPP_RULEBASED_RULES_DATA_H_ #define CHROMEOS_SERVICES_IME_PUBLIC_CPP_RULEBASED_RULES_DATA_H_
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <vector> #include <utility>
#include "base/macros.h" #include "base/macros.h"
...@@ -66,7 +67,8 @@ class RulesData { ...@@ -66,7 +67,8 @@ class RulesData {
private: private:
// The KeyMap instances under all the modifier states. // The KeyMap instances under all the modifier states.
KeyMap key_maps_[8]; static constexpr size_t kKeyMapCount = 8;
KeyMap key_maps_[kKeyMapCount];
// The map from the sub group match index (of the merged regexp) to the // The map from the sub group match index (of the merged regexp) to the
// transform rule (which is a pair of matching regexp, and replace string). // transform rule (which is a pair of matching regexp, and replace string).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment