From 67d2cf4df31b957cd051f8ebe6f39cac167f6972 Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Thu, 14 Nov 2024 02:37:09 +0800 Subject: [PATCH] build(dict): back to`std::stod` instead of `from_chars` for float-pointing (Apple)Clang and MSVC don't yet support `from_chars` for float-pointing, or requires a very new version of compiler / target platform. --- src/rime/dict/entry_collector.cc | 17 ++++++++--------- src/rime/dict/preset_vocabulary.cc | 6 ++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/rime/dict/entry_collector.cc b/src/rime/dict/entry_collector.cc index 3408b797c..a3834709f 100644 --- a/src/rime/dict/entry_collector.cc +++ b/src/rime/dict/entry_collector.cc @@ -167,17 +167,16 @@ void EntryCollector::CreateEntry(string_view word, e->raw_code.FromString(code_str); e->text = word; e->weight = 0.0; - bool scaled = boost::ends_with(weight_str, "%"); + bool scaled = strings::ends_with(weight_str, "%"); if ((weight_str.empty() || scaled) && preset_vocabulary) { preset_vocabulary->GetWeightForEntry(e->text, &e->weight); } if (scaled) { double percentage = 100.0; - auto scaled_weight = weight_str.substr(0, weight_str.length() - 1); - auto [ptr, ec] = std::from_chars( - scaled_weight.data(), scaled_weight.data() + scaled_weight.size(), - percentage); - if (ec != std::errc{}) { + try { + const string& scaled_weight { weight_str.substr(0, weight_str.length() - 1) }; + percentage = std::stod(scaled_weight); + } catch (...) { LOG(WARNING) << "invalid entry definition at #" << num_entries << ", line: " << line_number << " of file: " << current_dict_file << "."; @@ -185,9 +184,9 @@ void EntryCollector::CreateEntry(string_view word, } e->weight *= percentage / 100.0; } else if (!weight_str.empty()) { // absolute weight - auto [ptr, ec] = std::from_chars( - weight_str.data(), weight_str.data() + weight_str.size(), e->weight); - if (ec != std::errc{}) { + try { + e->weight = std::stod(string{weight_str}); + } catch (...) { LOG(WARNING) << "invalid entry definition at #" << num_entries << ", line: " << line_number << " of file: " << current_dict_file << "."; diff --git a/src/rime/dict/preset_vocabulary.cc b/src/rime/dict/preset_vocabulary.cc index c2e6c2bd4..38c3f9108 100644 --- a/src/rime/dict/preset_vocabulary.cc +++ b/src/rime/dict/preset_vocabulary.cc @@ -107,10 +107,8 @@ bool PresetVocabulary::IsQualifiedPhrase(string_view phrase, return false; } if (min_phrase_weight_ > 0.0) { - double weight = 0; - auto [ptr, ec] = std::from_chars( - weight_str.data(), weight_str.data() + weight_str.size(), weight); - if (ec == std::errc{} && weight < min_phrase_weight_) + double weight = std::stod(string{weight_str}); + if (weight < min_phrase_weight_) return false; } return true;