Skip to content

Commit

Permalink
refactor: replace boost lexicast with std algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and eagleoflqj committed Oct 24, 2023
1 parent 63886fb commit 775f850
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 27 deletions.
9 changes: 4 additions & 5 deletions src/rime/config/config_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/config/config_data.h>
#include <rime/config/config_types.h>

Expand Down Expand Up @@ -61,7 +60,7 @@ bool ConfigValue::GetInt(int* value) const {
}
// decimal
try {
*value = boost::lexical_cast<int>(value_);
*value = std::stoi(value_);
} catch (...) {
return false;
}
Expand All @@ -72,7 +71,7 @@ bool ConfigValue::GetDouble(double* value) const {
if (!value || value_.empty())
return false;
try {
*value = boost::lexical_cast<double>(value_);
*value = std::stod(value_);
} catch (...) {
return false;
}
Expand All @@ -92,12 +91,12 @@ bool ConfigValue::SetBool(bool value) {
}

bool ConfigValue::SetInt(int value) {
value_ = boost::lexical_cast<string>(value);
value_ = std::to_string(value);
return true;
}

bool ConfigValue::SetDouble(double value) {
value_ = boost::lexical_cast<string>(value);
value_ = std::to_string(value);
return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/rime/dict/entry_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <fstream>
#include <utility>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/algo/strings.h>
#include <rime/dict/dict_settings.h>
#include <rime/dict/entry_collector.h>
Expand Down
5 changes: 2 additions & 3 deletions src/rime/dict/preset_vocabulary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// 2011-11-27 GONG Chen <[email protected]>
//
#include <filesystem>
#include <boost/lexical_cast.hpp>
#include <utf8.h>
#include <rime/resource.h>
#include <rime/service.h>
Expand Down Expand Up @@ -74,7 +73,7 @@ bool PresetVocabulary::GetWeightForEntry(const string& key, double* weight) {
if (!db_ || !db_->Fetch(key, &weight_str))
return false;
try {
*weight = boost::lexical_cast<double>(weight_str);
*weight = std::stod(weight_str);
} catch (...) {
return false;
}
Expand Down Expand Up @@ -105,7 +104,7 @@ bool PresetVocabulary::IsQualifiedPhrase(const string& phrase,
return false;
}
if (min_phrase_weight_ > 0.0) {
double weight = boost::lexical_cast<double>(weight_str);
double weight = std::stod(weight_str);
if (weight < min_phrase_weight_)
return false;
}
Expand Down
1 change: 0 additions & 1 deletion src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <boost/lexical_cast.hpp>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
Expand Down
5 changes: 2 additions & 3 deletions src/rime/dict/table_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// 2013-04-18 GONG Chen <[email protected]>
//
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/dict/table_db.h>
#include <rime/dict/user_db.h>

Expand All @@ -28,7 +27,7 @@ static bool rime_table_entry_parser(const Tsv& row,
UserDbValue v;
if (row.size() >= 3 && !row[2].empty()) {
try {
v.commits = boost::lexical_cast<int>(row[2]);
v.commits = std::stoi(row[2]);
const double kS = 1e8;
v.dee = (v.commits + 1) / kS;
} catch (...) {
Expand All @@ -51,7 +50,7 @@ static bool rime_table_entry_formatter(const string& key,
return false;
boost::algorithm::trim(row[0]); // remove trailing space
row[0].swap(row[1]);
row.push_back(boost::lexical_cast<string>(v.commits));
row.push_back(std::to_string(v.commits));
return true;
}

Expand Down
13 changes: 6 additions & 7 deletions src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/service.h>
#include <rime/algo/dynamics.h>
#include <rime/dict/text_db.h>
Expand All @@ -34,11 +33,11 @@ bool UserDbValue::Unpack(const string& value) {
string v(k_eq_v.substr(eq + 1));
try {
if (k == "c") {
commits = boost::lexical_cast<int>(v);
commits = std::stoi(v);
} else if (k == "d") {
dee = (std::min)(10000.0, boost::lexical_cast<double>(v));
dee = (std::min)(10000.0, std::stod(v));
} else if (k == "t") {
tick = boost::lexical_cast<TickCount>(v);
tick = std::stoul(v);
}
} catch (...) {
LOG(ERROR) << "failed in parsing key-value from userdb entry '" << k_eq_v
Expand Down Expand Up @@ -170,7 +169,7 @@ static TickCount get_tick_count(Db* db) {
string tick;
if (db && db->MetaFetch("/tick", &tick)) {
try {
return boost::lexical_cast<TickCount>(tick);
return std::stoul(tick);
} catch (...) {
}
}
Expand All @@ -190,7 +189,7 @@ UserDbMerger::~UserDbMerger() {
bool UserDbMerger::MetaPut(const string& key, const string& value) {
if (key == "/tick") {
try {
their_tick_ = boost::lexical_cast<TickCount>(value);
their_tick_ = std::stoul(value);
max_tick_ = (std::max)(our_tick_, their_tick_);
} catch (...) {
}
Expand Down Expand Up @@ -225,7 +224,7 @@ void UserDbMerger::CloseMerge() {
return;
Deployer& deployer(Service::instance().deployer());
try {
db_->MetaUpdate("/tick", boost::lexical_cast<string>(max_tick_));
db_->MetaUpdate("/tick", std::to_string(max_tick_));
db_->MetaUpdate("/user_id", deployer.user_id);
} catch (...) {
LOG(ERROR) << "failed to update tick count.";
Expand Down
5 changes: 2 additions & 3 deletions src/rime/dict/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cfloat>
#include <cmath>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/scope_exit.hpp>
#include <rime/common.h>
#include <rime/language.h>
Expand Down Expand Up @@ -381,7 +380,7 @@ bool UserDictionary::UpdateEntry(const DictEntry& entry,
bool UserDictionary::UpdateTickCount(TickCount increment) {
tick_ += increment;
try {
return db_->MetaUpdate("/tick", boost::lexical_cast<string>(tick_));
return db_->MetaUpdate("/tick", std::to_string(tick_));
} catch (...) {
return false;
}
Expand All @@ -397,7 +396,7 @@ bool UserDictionary::FetchTickCount() {
// an earlier version mistakenly wrote tick count into an empty key
if (!db_->MetaFetch("/tick", &value) && !db_->Fetch("", &value))
return false;
tick_ = boost::lexical_cast<TickCount>(value);
tick_ = std::stoul(value);
return true;
} catch (...) {
// tick_ = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/rime/gear/key_binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// 2011-11-23 GONG Chen <[email protected]>
//
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <rime/common.h>
#include <rime/composition.h>
#include <rime/context.h>
Expand Down Expand Up @@ -79,7 +78,7 @@ inline static bool is_switch_index(const string& option) {
static Switches::SwitchOption switch_by_index(Switches& switches,
const string& option) {
try {
size_t index = boost::lexical_cast<size_t>(option.substr(1));
size_t index = std::stoul(option.substr(1));
return switches.ByIndex(index);
} catch (...) {
}
Expand Down
3 changes: 1 addition & 2 deletions src/rime/lever/customizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// 2011-12-12 GONG Chen <[email protected]>
//
#include <stdint.h>
#include <boost/lexical_cast.hpp>
#include <rime/common.h>
#include <rime/config.h>
#include <rime/algo/utilities.h>
Expand Down Expand Up @@ -73,7 +72,7 @@ bool Customizer::UpdateConfigFile() {
}
string customization;
if (!custom_path.empty() && fs::exists(custom_path)) {
customization = boost::lexical_cast<string>(Checksum(custom_path.string()));
customization = std::to_string(Checksum(custom_path.string()));
}
if (applied_customization != customization) {
need_update = true;
Expand Down

0 comments on commit 775f850

Please sign in to comment.