diff --git a/CMakeLists.txt b/CMakeLists.txt index 010d6ed8e7..c8759052af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,7 @@ if(MSVC) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi") set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF") add_definitions("/wd4244 /wd4996") + add_compile_options("/Zc:__cplusplus") # large address aware set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE") diff --git a/include/darts.h b/include/darts.h index 18bf988bab..632b3cbda7 100644 --- a/include/darts.h +++ b/include/darts.h @@ -1429,9 +1429,9 @@ class DoubleArrayBuilder { void clear(); private: - enum { BLOCK_SIZE = 256 }; - enum { NUM_EXTRA_BLOCKS = 16 }; - enum { NUM_EXTRAS = BLOCK_SIZE * NUM_EXTRA_BLOCKS }; + static const std::size_t BLOCK_SIZE = 256; + static const std::size_t NUM_EXTRA_BLOCKS = 16; + static const std::size_t NUM_EXTRAS = BLOCK_SIZE * NUM_EXTRA_BLOCKS; enum { UPPER_MASK = 0xFF << 21 }; enum { LOWER_MASK = 0xFF }; diff --git a/src/rime/common.h b/src/rime/common.h index 01f068e87e..b359b34041 100644 --- a/src/rime/common.h +++ b/src/rime/common.h @@ -104,6 +104,27 @@ class path : public std::filesystem::path { path& operator/=(const std::string& p) { return *this /= path(p); } path& operator/=(const char* p) { return *this /= path(p); } + path stem() const { return path(fs_path::stem()); } + path filename() const { return path(fs_path::filename()); } + path extension() const { return path(fs_path::extension()); } + // return UTF-8 encoded std::string + std::string string_utf8() const { +#if __cplusplus >= 202002L + const auto u8s = this->u8string(); + return std::string(u8s.begin(), u8s.end()); +#else + return this->u8string(); +#endif + } + // return UTF-8 encoded std::string of generic format + std::string generic_string_utf8() const { +#if __cplusplus >= 202002L + const auto u8s = this->generic_u8string(); + return std::string(u8s.begin(), u8s.end()); +#else + return this->generic_u8string(); +#endif + } friend path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; } @@ -128,7 +149,7 @@ class path : public std::filesystem::path { } #ifdef RIME_ENABLE_LOGGING friend std::ostream& operator<<(std::ostream& os, const path& p) { - return os << p.u8string(); + return os << p.string_utf8(); } #endif }; diff --git a/src/rime/config/config_data.cc b/src/rime/config/config_data.cc index e3e73dbd05..d06670dcd3 100644 --- a/src/rime/config/config_data.cc +++ b/src/rime/config/config_data.cc @@ -66,7 +66,7 @@ bool ConfigData::LoadFromFile(const path& file_path, ConfigCompiler* compiler) { modified_ = false; root.reset(); if (!std::filesystem::exists(file_path)) { - if (!boost::ends_with(file_path.u8string(), ".custom.yaml")) + if (!boost::ends_with(file_path.string_utf8(), ".custom.yaml")) LOG(WARNING) << "nonexistent config file '" << file_path << "'."; return false; } diff --git a/src/rime/dict/dict_compiler.cc b/src/rime/dict/dict_compiler.cc index 5996ee2cc3..982ef0926e 100644 --- a/src/rime/dict/dict_compiler.cc +++ b/src/rime/dict/dict_compiler.cc @@ -209,7 +209,7 @@ bool DictCompiler::Compile(const path& schema_file) { static path relocate_target(const path& source_path, ResourceResolver* target_resolver) { - auto resource_id = source_path.filename().u8string(); + auto resource_id = source_path.filename().string_utf8(); return target_resolver->ResolvePath(resource_id); } diff --git a/src/rime/dict/entry_collector.cc b/src/rime/dict/entry_collector.cc index 48d9402753..e1c0d00172 100644 --- a/src/rime/dict/entry_collector.cc +++ b/src/rime/dict/entry_collector.cc @@ -56,7 +56,7 @@ void EntryCollector::LoadPresetVocabulary(DictSettings* settings) { void EntryCollector::Collect(const path& dict_file) { LOG(INFO) << "collecting entries from " << dict_file; - current_dict_file = dict_file.u8string(); + current_dict_file = dict_file.string_utf8(); line_number = 0; // read table std::ifstream fin(dict_file.c_str()); diff --git a/src/rime/dict/user_db.cc b/src/rime/dict/user_db.cc index e8f6788755..95a0971902 100644 --- a/src/rime/dict/user_db.cc +++ b/src/rime/dict/user_db.cc @@ -108,7 +108,7 @@ bool UserDbHelper::UpdateUserInfo() { } bool UserDbHelper::IsUniformFormat(const path& file_path) { - return boost::ends_with(file_path.filename().u8string(), + return boost::ends_with(file_path.filename().string_utf8(), plain_userdb_extension); } diff --git a/src/rime/dict/vocabulary.h b/src/rime/dict/vocabulary.h index c7753d942b..41357fef5b 100644 --- a/src/rime/dict/vocabulary.h +++ b/src/rime/dict/vocabulary.h @@ -40,6 +40,8 @@ struct ShortDictEntry { double weight = 0.0; ShortDictEntry() = default; + ShortDictEntry(const string& t, const Code& c, double w) + : text(t), code(c), weight(w) {} bool operator<(const ShortDictEntry& other) const; }; diff --git a/src/rime/gear/simplifier.cc b/src/rime/gear/simplifier.cc index b337cf1a66..6cc112db64 100644 --- a/src/rime/gear/simplifier.cc +++ b/src/rime/gear/simplifier.cc @@ -41,7 +41,7 @@ class Opencc { opencc::Config config; try { // opencc accepts file path encoded in UTF-8. - converter_ = config.NewFromFile(config_path_.u8string()); + converter_ = config.NewFromFile(config_path_.string_utf8()); const list conversions = converter_->GetConversionChain()->GetConversions(); @@ -315,7 +315,7 @@ Simplifier* SimplifierComponent::Create(const Ticket& ticket) { return new Simplifier(ticket, opencc); } path opencc_config_path = path(opencc_config); - if (opencc_config_path.extension().u8string() == ".ini") { + if (opencc_config_path.extension().string_utf8() == ".ini") { LOG(ERROR) << "please upgrade opencc_config to an opencc 1.0 config file."; return nullptr; } diff --git a/src/rime/lever/deployment_tasks.cc b/src/rime/lever/deployment_tasks.cc index 6e6ff89570..aa3ca7357c 100644 --- a/src/rime/lever/deployment_tasks.cc +++ b/src/rime/lever/deployment_tasks.cc @@ -52,8 +52,8 @@ bool DetectModifications::Run(Deployer* deployer) { for (fs::directory_iterator iter(p), end; iter != end; ++iter) { path entry(iter->path()); if (fs::is_regular_file(fs::canonical(entry)) && - entry.extension().u8string() == ".yaml" && - entry.filename().u8string() != "user.yaml") { + entry.extension().string_utf8() == ".yaml" && + entry.filename().string_utf8() != "user.yaml") { last_modified = (std::max)(last_modified, filesystem::to_time_t(fs::last_write_time(entry))); @@ -460,7 +460,7 @@ bool PrebuildAllSchemas::Run(Deployer* deployer) { for (fs::directory_iterator iter(shared_data_path), end; iter != end; ++iter) { path entry(iter->path()); - if (boost::ends_with(entry.filename().u8string(), ".schema.yaml")) { + if (boost::ends_with(entry.filename().string_utf8(), ".schema.yaml")) { the t(new SchemaUpdate(entry)); if (!t->Run(deployer)) success = false; @@ -525,7 +525,7 @@ bool UserDictSync::Run(Deployer* deployer) { } static bool IsCustomizedCopy(const path& file_path) { - auto file_name = file_path.filename().u8string(); + auto file_name = file_path.filename().string_utf8(); if (boost::ends_with(file_name, ".yaml") && !boost::ends_with(file_name, ".custom.yaml")) { Config config; @@ -556,7 +556,7 @@ bool BackupConfigFiles::Run(Deployer* deployer) { path entry(iter->path()); if (!fs::is_regular_file(entry)) continue; - auto file_extension = entry.extension().u8string(); + auto file_extension = entry.extension().string_utf8(); bool is_yaml_file = file_extension == ".yaml"; bool is_text_file = file_extension == ".txt"; if (!is_yaml_file && !is_text_file) @@ -596,7 +596,7 @@ bool CleanupTrash::Run(Deployer* deployer) { path entry(iter->path()); if (!fs::is_regular_file(entry)) continue; - auto file_name = entry.filename().u8string(); + auto file_name = entry.filename().string_utf8(); if (file_name == "rime.log" || boost::ends_with(file_name, ".bin") || boost::ends_with(file_name, ".reverse.kct") || boost::ends_with(file_name, ".userdb.kct.old") || @@ -650,7 +650,7 @@ bool CleanOldLogFiles::Run(Deployer* deployer) { try { // preparing files for (const auto& entry : fs::directory_iterator(dir)) { - const string& file_name(entry.path().filename().u8string()); + const string& file_name(path(entry.path().filename()).string_utf8()); if (entry.is_regular_file() && !entry.is_symlink() && boost::starts_with(file_name, app_name) && boost::ends_with(file_name, ".log") && @@ -658,7 +658,7 @@ bool CleanOldLogFiles::Run(Deployer* deployer) { files_to_remove.push_back(entry.path()); } else if (entry.is_symlink()) { auto target = fs::read_symlink(entry.path()); - const string& target_file_name(target.filename().u8string()); + const string& target_file_name(path(target.filename()).string_utf8()); if (boost::starts_with(target_file_name, app_name) && boost::ends_with(target_file_name, ".log")) { files_in_use.insert(target); diff --git a/src/rime/lever/switcher_settings.cc b/src/rime/lever/switcher_settings.cc index ad8c91608d..87d0e19e4e 100644 --- a/src/rime/lever/switcher_settings.cc +++ b/src/rime/lever/switcher_settings.cc @@ -53,7 +53,7 @@ void SwitcherSettings::GetAvailableSchemasFromDirectory(const path& dir) { } for (fs::directory_iterator it(dir), end; it != end; ++it) { path file_path(it->path()); - if (boost::ends_with(file_path.u8string(), ".schema.yaml")) { + if (boost::ends_with(file_path.string_utf8(), ".schema.yaml")) { Config config; if (config.LoadFromFile(file_path)) { SchemaInfo info; diff --git a/src/rime/lever/user_dict_manager.cc b/src/rime/lever/user_dict_manager.cc index e0331e4c07..0be9e310a3 100644 --- a/src/rime/lever/user_dict_manager.cc +++ b/src/rime/lever/user_dict_manager.cc @@ -40,7 +40,7 @@ void UserDictManager::GetUserDictList(UserDictList* user_dict_list, return; } for (fs::directory_iterator it(path_), end; it != end; ++it) { - string name = it->path().filename().u8string(); + string name = path(it->path().filename()).string_utf8(); if (boost::ends_with(name, component->extension())) { boost::erase_last(name, component->extension()); user_dict_list->push_back(name); diff --git a/src/rime/resource.cc b/src/rime/resource.cc index 045e047f8a..04beb6b8dd 100644 --- a/src/rime/resource.cc +++ b/src/rime/resource.cc @@ -10,7 +10,7 @@ namespace rime { string ResourceResolver::ToResourceId(const string& file_path) const { - string string_path = path(file_path).generic_u8string(); + string string_path = path(file_path).generic_string_utf8(); bool has_prefix = boost::starts_with(string_path, type_.prefix); bool has_suffix = boost::ends_with(string_path, type_.suffix); size_t start = (has_prefix ? type_.prefix.length() : 0); diff --git a/tools/rime_table_decompiler.cc b/tools/rime_table_decompiler.cc index 06e265fc27..05775a4860 100644 --- a/tools/rime_table_decompiler.cc +++ b/tools/rime_table_decompiler.cc @@ -122,7 +122,7 @@ int main(int argc, char* argv[]) { fout << "# Rime dictionary\n\n"; fout << "---\n" "name: " - << file_path.stem().u8string() + << rime::path(file_path).stem().string_utf8() << "\n" "version: \"1.0\"\n" "...\n\n";