Skip to content

Commit

Permalink
build(dict): back tostd::stod instead of from_chars for float-poi…
Browse files Browse the repository at this point in the history
…nting

(Apple)Clang and MSVC don't yet support `from_chars` for float-pointing, or requires a very new version of compiler / target platform.
  • Loading branch information
WhiredPlanck committed Nov 13, 2024
1 parent af7e7cb commit 67d2cf4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 8 additions & 9 deletions src/rime/dict/entry_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,26 @@ 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 << ".";
percentage = 100.0;
}
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 << ".";
Expand Down
6 changes: 2 additions & 4 deletions src/rime/dict/preset_vocabulary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 67d2cf4

Please sign in to comment.