diff --git a/test/component_test.cc b/test/component_test.cc index da1f701137..5462c18451 100644 --- a/test/component_test.cc +++ b/test/component_test.cc @@ -10,7 +10,7 @@ using namespace rime; -class Greeting : public Class { +class Greeting : public Class { public: virtual string Say() = 0; virtual ~Greeting() = default; @@ -31,10 +31,10 @@ class Hello : public Greeting { // customize a hello component with parameters class HelloComponent : public Hello::Component { public: - HelloComponent(const string& word) : word_(word) {} + HelloComponent(string_view word) : word_(word) {} // define a custom creator to provide an additional argument - Hello* Create(const string& name) { - return new Hello(make_pair(word_, name)); + Hello* Create(string_view name) { + return new Hello(make_pair(word_, string{name})); } private: diff --git a/test/config_compiler_test.cc b/test/config_compiler_test.cc index 18e62945b7..a61b6247aa 100644 --- a/test/config_compiler_test.cc +++ b/test/config_compiler_test.cc @@ -7,6 +7,7 @@ #include #include #include +#include using namespace rime; @@ -35,73 +36,90 @@ TEST_F(RimeConfigCompilerTest, IncludeLocalReference) { std::ostringstream oss; oss << "include_local_reference/@" << i << "/"; const auto& prefix(oss.str()); - EXPECT_TRUE(config_->IsNull(prefix + "__include")); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include"))); string actual; - EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &actual)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/player"), &actual)); EXPECT_EQ("slayers_boxer", actual); - EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &actual)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/player"), &actual)); EXPECT_EQ("grrrr", actual); - EXPECT_TRUE(config_->GetString(prefix + "zerg/player", &actual)); + EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "zerg/player"), + &actual)); EXPECT_EQ("yellow", actual); } } TEST_F(RimeConfigCompilerTest, IncludeExternalReference) { - const string& prefix = "include_external_reference/"; - EXPECT_TRUE(config_->IsNull(prefix + "terrans/__include")); + string_view prefix = "include_external_reference/"; + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "terrans/__include"))); int mineral = 0; - EXPECT_TRUE(config_->GetInt(prefix + "terrans/tank/cost/mineral", &mineral)); + EXPECT_TRUE(config_->GetInt( + rime::strings::concat(prefix, "terrans/tank/cost/mineral"), &mineral)); EXPECT_EQ(150, mineral); int gas = 0; - EXPECT_TRUE(config_->GetInt(prefix + "terrans/tank/cost/gas", &gas)); + EXPECT_TRUE(config_->GetInt( + rime::strings::concat(prefix, "terrans/tank/cost/gas"), &gas)); EXPECT_EQ(100, gas); - EXPECT_TRUE(config_->IsNull(prefix + "protoss")); - EXPECT_TRUE(config_->IsNull(prefix + "zerg")); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "protoss"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "zerg"))); } TEST_F(RimeConfigCompilerTest, IncludeExternalFile) { - const string& prefix = "include_external_file/"; - EXPECT_TRUE(config_->IsNull(prefix + "__include")); - EXPECT_TRUE(config_->IsMap(prefix + "terrans")); - EXPECT_TRUE(config_->IsMap(prefix + "protoss")); - EXPECT_TRUE(config_->IsMap(prefix + "zerg")); + string_view prefix = "include_external_file/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include"))); + EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "terrans"))); + EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "protoss"))); + EXPECT_TRUE(config_->IsMap(rime::strings::concat(prefix, "zerg"))); int number = 0; - EXPECT_TRUE(config_->GetInt(prefix + "terrans/supply/produced", &number)); + EXPECT_TRUE(config_->GetInt( + rime::strings::concat(prefix, "terrans/supply/produced"), &number)); EXPECT_EQ(28, number); - EXPECT_TRUE(config_->IsList(prefix + "protoss/air_force")); + EXPECT_TRUE( + config_->IsList(rime::strings::concat(prefix, "protoss/air_force"))); string name; - EXPECT_TRUE(config_->GetString(prefix + "zerg/queen", &name)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "zerg/queen"), &name)); EXPECT_EQ("Kerrigan", name); } TEST_F(RimeConfigCompilerTest, PatchReference) { - const string& prefix = "patch_reference/"; - EXPECT_TRUE(config_->IsNull(prefix + "__patch")); - EXPECT_EQ(4, config_->GetListSize(prefix + "battlefields")); + string_view prefix = "patch_reference/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch"))); + EXPECT_EQ( + 4, config_->GetListSize(rime::strings::concat(prefix, "battlefields"))); string map; - EXPECT_TRUE(config_->GetString(prefix + "battlefields/@3", &map)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "battlefields/@3"), &map)); EXPECT_EQ("match point", map); } TEST_F(RimeConfigCompilerTest, PatchLiteral) { - const string& prefix = "patch_literal/"; - EXPECT_TRUE(config_->IsNull(prefix + "__patch")); - EXPECT_EQ(6, config_->GetListSize(prefix + "zerg/ground_units")); + string_view prefix = "patch_literal/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch"))); + EXPECT_EQ(6, config_->GetListSize( + rime::strings::concat(prefix, "zerg/ground_units"))); string unit; - EXPECT_TRUE(config_->GetString(prefix + "zerg/ground_units/@5", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "zerg/ground_units/@5"), &unit)); EXPECT_EQ("lurker", unit); // assert that we patched on a copy of the included node EXPECT_EQ(5, config_->GetListSize("/starcraft/zerg/ground_units")); } TEST_F(RimeConfigCompilerTest, PatchList) { - const string& prefix = "patch_list/"; - EXPECT_TRUE(config_->IsNull(prefix + "protoss/__patch")); - EXPECT_EQ(8, config_->GetListSize(prefix + "protoss/ground_units")); + string_view prefix = "patch_list/"; + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "protoss/__patch"))); + EXPECT_EQ(8, config_->GetListSize( + rime::strings::concat(prefix, "protoss/ground_units"))); string unit; - EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@6", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/ground_units/@6"), &unit)); EXPECT_EQ("dark templar", unit); - EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@7", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/ground_units/@7"), &unit)); EXPECT_EQ("dark archon", unit); // assert that we patched on a copy of the included node EXPECT_EQ(6, config_->GetListSize("/starcraft/protoss/ground_units")); @@ -113,30 +131,41 @@ class RimeConfigDependencyTest : public RimeConfigCompilerTestBase { }; TEST_F(RimeConfigDependencyTest, DependencyChaining) { - const string& prefix = "dependency_chaining/"; - EXPECT_TRUE(config_->IsNull(prefix + "alpha/__include")); - EXPECT_TRUE(config_->IsNull(prefix + "beta/__include")); - EXPECT_TRUE(config_->IsNull(prefix + "delta/__include")); + string_view prefix = "dependency_chaining/"; + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "alpha/__include"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "beta/__include"))); + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "delta/__include"))); string value; - EXPECT_TRUE(config_->GetString(prefix + "alpha", &value)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "alpha"), &value)); EXPECT_EQ("success", value); - EXPECT_TRUE(config_->GetString(prefix + "beta", &value)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "beta"), &value)); EXPECT_EQ("success", value); - EXPECT_TRUE(config_->GetString(prefix + "delta", &value)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "delta"), &value)); EXPECT_EQ("success", value); } // Unit test for https://github.com/rime/librime/issues/141 TEST_F(RimeConfigDependencyTest, DependencyPriorities) { - const string& prefix = "dependency_priorities/"; - EXPECT_TRUE(config_->IsNull(prefix + "terrans/__include")); - EXPECT_TRUE(config_->IsNull(prefix + "terrans/__patch")); - EXPECT_TRUE(config_->IsNull(prefix + "protoss/__include")); - EXPECT_TRUE(config_->IsNull(prefix + "protoss/__patch")); + string_view prefix = "dependency_priorities/"; + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "terrans/__include"))); + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "terrans/__patch"))); + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "protoss/__include"))); + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "protoss/__patch"))); string player; - EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/player"), &player)); EXPECT_EQ("nada", player); - EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &player)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/player"), &player)); EXPECT_EQ("bisu", player); } @@ -148,11 +177,12 @@ class RimeConfigOptionalReferenceTest : public RimeConfigCompilerTestBase { }; TEST_F(RimeConfigOptionalReferenceTest, OptionalReference) { - const string& prefix = "/"; - EXPECT_TRUE(config_->IsNull(prefix + "__include")); - EXPECT_TRUE(config_->IsNull(prefix + "__patch")); + string_view prefix = "/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch"))); bool untouched; - EXPECT_TRUE(config_->GetBool(prefix + "untouched", &untouched)); + EXPECT_TRUE( + config_->GetBool(rime::strings::concat(prefix, "untouched"), &untouched)); EXPECT_TRUE(untouched); } @@ -162,16 +192,18 @@ class RimeConfigMergeTest : public RimeConfigCompilerTestBase { }; TEST_F(RimeConfigMergeTest, AppendWithInclude) { - const string& prefix = "append_with_include/"; - EXPECT_TRUE(config_->IsNull(prefix + "list/__include")); - EXPECT_TRUE(config_->IsNull(prefix + "list/__append")); + string_view prefix = "append_with_include/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "list/__include"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "list/__append"))); - EXPECT_TRUE(config_->IsList(prefix + "list")); - EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "list")); + EXPECT_TRUE(config_->IsList(rime::strings::concat(prefix, "list"))); + EXPECT_EQ(6 + 2, config_->GetListSize(rime::strings::concat(prefix, "list"))); string unit; - EXPECT_TRUE(config_->GetString(prefix + "list/@6", &unit)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "list/@6"), &unit)); EXPECT_EQ("dark templar", unit); - EXPECT_TRUE(config_->GetString(prefix + "list/@7", &unit)); + EXPECT_TRUE( + config_->GetString(rime::strings::concat(prefix, "list/@7"), &unit)); EXPECT_EQ("dark archon", unit); // verify that we append to a copy and the original list is untouched @@ -179,22 +211,27 @@ TEST_F(RimeConfigMergeTest, AppendWithInclude) { } TEST_F(RimeConfigMergeTest, AppendWithPatch) { - const string& prefix = "append_with_patch/"; - EXPECT_TRUE(config_->IsNull(prefix + "__include")); - EXPECT_TRUE(config_->IsNull(prefix + "__patch")); + string_view prefix = "append_with_patch/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch"))); string player; - EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/player"), &player)); EXPECT_EQ("slayers_boxer, nada", player); - EXPECT_EQ(2, config_->GetListSize(prefix + "terrans/air_units")); + EXPECT_EQ(2, config_->GetListSize( + rime::strings::concat(prefix, "terrans/air_units"))); string unit; - EXPECT_TRUE(config_->GetString(prefix + "terrans/air_units/@0", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/air_units/@0"), &unit)); EXPECT_EQ("wraith", unit); - EXPECT_TRUE(config_->GetString(prefix + "terrans/air_units/@1", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/air_units/@1"), &unit)); EXPECT_EQ("battlecruiser", unit); - EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "protoss/ground_units")); + EXPECT_EQ(6 + 2, config_->GetListSize( + rime::strings::concat(prefix, "protoss/ground_units"))); // verify that we append to a copy and the original list is untouched EXPECT_TRUE(config_->GetString("/starcraft/terrans/player", &player)); @@ -204,35 +241,50 @@ TEST_F(RimeConfigMergeTest, AppendWithPatch) { } TEST_F(RimeConfigMergeTest, MergeTree) { - const string& prefix = "merge_tree/"; - EXPECT_TRUE(config_->IsNull(prefix + "__include")); + string_view prefix = "merge_tree/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__include"))); string player; - EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &player)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/player"), &player)); EXPECT_EQ("slayers_boxer", player); - EXPECT_TRUE(config_->IsNull(prefix + "terrans/air_units")); - EXPECT_TRUE(config_->IsList(prefix + "terrans/ground_units")); - EXPECT_EQ(5 + 2, config_->GetListSize(prefix + "terrans/ground_units")); + EXPECT_TRUE( + config_->IsNull(rime::strings::concat(prefix, "terrans/air_units"))); + EXPECT_TRUE( + config_->IsList(rime::strings::concat(prefix, "terrans/ground_units"))); + EXPECT_EQ(5 + 2, config_->GetListSize( + rime::strings::concat(prefix, "terrans/ground_units"))); string unit; - EXPECT_TRUE(config_->GetString(prefix + "terrans/ground_units/@5", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/ground_units/@5"), &unit)); EXPECT_EQ("medic", unit); - EXPECT_TRUE(config_->GetString(prefix + "terrans/ground_units/@6", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "terrans/ground_units/@6"), &unit)); EXPECT_EQ("goliath", unit); - EXPECT_TRUE(config_->GetString(prefix + "protoss/player", &player)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/player"), &player)); EXPECT_EQ("grrrr", player); - EXPECT_TRUE(config_->IsNull(prefix + "protoss/ground_units/__append")); - EXPECT_TRUE(config_->IsList(prefix + "protoss/ground_units")); - EXPECT_EQ(6 + 2, config_->GetListSize(prefix + "protoss/ground_units")); - EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@6", &unit)); + EXPECT_TRUE(config_->IsNull( + rime::strings::concat(prefix, "protoss/ground_units/__append"))); + EXPECT_TRUE( + config_->IsList(rime::strings::concat(prefix, "protoss/ground_units"))); + EXPECT_EQ(6 + 2, config_->GetListSize( + rime::strings::concat(prefix, "protoss/ground_units"))); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/ground_units/@6"), &unit)); EXPECT_EQ("dark templar", unit); - EXPECT_TRUE(config_->GetString(prefix + "protoss/ground_units/@7", &unit)); + EXPECT_TRUE(config_->GetString( + rime::strings::concat(prefix, "protoss/ground_units/@7"), &unit)); EXPECT_EQ("dark archon", unit); - EXPECT_TRUE(config_->GetString(prefix + "zerg/player", &player)); + EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "zerg/player"), + &player)); EXPECT_EQ("yellow", player); - EXPECT_TRUE(config_->IsList(prefix + "zerg/ground_units")); - EXPECT_EQ(0, config_->GetListSize(prefix + "zerg/ground_units")); + EXPECT_TRUE( + config_->IsList(rime::strings::concat(prefix, "zerg/ground_units"))); + EXPECT_EQ(0, config_->GetListSize( + rime::strings::concat(prefix, "zerg/ground_units"))); // verify that we merge to a copy and the original list is untouched EXPECT_TRUE(config_->IsNull("/starcraft/terrans/ground_units")); @@ -248,19 +300,21 @@ class RimeConfigCircularDependencyTest : public RimeConfigCompilerTestBase { }; TEST_F(RimeConfigMergeTest, CreateListWithInplacePatch) { - const string& prefix = "create_list_with_inplace_patch/"; - EXPECT_TRUE(config_->IsList(prefix + "all_ground_units")); - EXPECT_EQ(16, config_->GetListSize(prefix + "all_ground_units")); + string_view prefix = "create_list_with_inplace_patch/"; + EXPECT_TRUE( + config_->IsList(rime::strings::concat(prefix, "all_ground_units"))); + EXPECT_EQ(16, config_->GetListSize( + rime::strings::concat(prefix, "all_ground_units"))); } TEST_F(RimeConfigCircularDependencyTest, BestEffortResolution) { - const string& prefix = "test/"; - EXPECT_TRUE(config_->IsNull(prefix + "__patch")); - EXPECT_TRUE(config_->IsNull(prefix + "work/__include")); + string_view prefix = "test/"; + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "__patch"))); + EXPECT_TRUE(config_->IsNull(rime::strings::concat(prefix, "work/__include"))); string home; - EXPECT_TRUE(config_->GetString(prefix + "home", &home)); + EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "home"), &home)); EXPECT_EQ("naive", home); string work; - EXPECT_TRUE(config_->GetString(prefix + "work", &work)); + EXPECT_TRUE(config_->GetString(rime::strings::concat(prefix, "work"), &work)); EXPECT_EQ("excited", work); } diff --git a/tools/rime_console.cc b/tools/rime_console.cc index 6907351933..07092b0162 100644 --- a/tools/rime_console.cc +++ b/tools/rime_console.cc @@ -68,7 +68,7 @@ class RimeConsole { } } - void ProcessLine(const string& line) { + void ProcessLine(string_view line) { KeySequence keys; if (!keys.Parse(line)) { LOG(ERROR) << "error parsing input: '" << line << "'"; diff --git a/tools/rime_deployer.cc b/tools/rime_deployer.cc index 8360ac5b90..e030ddcfb9 100644 --- a/tools/rime_deployer.cc +++ b/tools/rime_deployer.cc @@ -46,7 +46,7 @@ int add_schema(int count, char* schemas[]) { return 0; } -int set_active_schema(const string& schema_id) { +int set_active_schema(string_view schema_id) { Config config; if (!config.LoadFromFile(path{"user.yaml"})) { LOG(INFO) << "creating new file 'user.yaml'."; diff --git a/tools/rime_patch.cc b/tools/rime_patch.cc index 652a1c3216..c12a03e7ed 100644 --- a/tools/rime_patch.cc +++ b/tools/rime_patch.cc @@ -17,9 +17,7 @@ using namespace rime; -int apply_patch(const string& config_id, - const string& key, - const string& yaml) { +int apply_patch(string_view config_id, string_view key, string_view yaml) { RimeApi* rime = rime_get_api(); RimeModule* module = rime->find_module("levers"); if (!module) { @@ -31,12 +29,12 @@ int apply_patch(const string& config_id, int ret = 1; RimeConfig value = {0}; // should be zero-initialized - if (rime->config_load_string(&value, yaml.c_str())) { + if (rime->config_load_string(&value, yaml.data())) { RimeCustomSettings* settings = - levers->custom_settings_init(config_id.c_str(), "rime_patch"); + levers->custom_settings_init(config_id.data(), "rime_patch"); levers->load_settings(settings); - if (levers->customize_item(settings, key.c_str(), &value)) { + if (levers->customize_item(settings, key.data(), &value)) { levers->save_settings(settings); std::cerr << "patch applied." << std::endl; ret = 0;