Skip to content

Commit

Permalink
replace boost string functions
Browse files Browse the repository at this point in the history
split/join
trim
starts_with/ends_with
  • Loading branch information
groverlynn committed Nov 27, 2023
1 parent d12ef28 commit 1058295
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 37 deletions.
7 changes: 3 additions & 4 deletions src/rime/algo/calculus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//
// 2012-01-17 GONG Chen <[email protected]>
//
#include <boost/algorithm/string.hpp>
#include <utf8.h>
#include <rime/algo/calculus.h>
#include <rime/algo/strings.h>
#include <rime/common.h>

namespace rime {
Expand All @@ -31,9 +31,8 @@ Calculation* Calculus::Parse(const string& definition) {
size_t sep = definition.find_first_not_of("zyxwvutsrqponmlkjihgfedcba");
if (sep == string::npos)
return NULL;
vector<string> args;
boost::split(args, definition,
boost::is_from_range(definition[sep], definition[sep]));
const string& delim = definition.substr(sep, 1);
vector<string> args = strings::split(definition, delim);
if (args.empty())
return NULL;
auto it = factories_.find(args[0]);
Expand Down
26 changes: 26 additions & 0 deletions src/rime/algo/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ inline string join(std::initializer_list<C>&& container, T&& delim) {
return join(std::begin(container), std::end(container), delim);
}

inline void trim_left(string& str) {
str.erase(str.begin(),
std::find_if(str.begin(), str.end(),
[](unsigned char ch) { return !std::isspace(ch); }));
}

inline void trim_right(string& str) {
str.erase(std::find_if(str.rbegin(), str.rend(),
[](unsigned char ch) { return !std::isspace(ch); })
.base(),
str.end());
}

inline void trim(string& str) {
trim_right(str);
trim_left(str);
}

inline bool starts_with(const string& str, const string& prefix) {
return str.rfind(prefix, 0) == 0;
}

inline bool ends_with(const string& str, const string& suffix) {
return str.find(suffix, str.length() - suffix.length()) != std::string::npos;
}

} // namespace strings
} // namespace rime

Expand Down
10 changes: 4 additions & 6 deletions src/rime/config/config_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <yaml-cpp/yaml.h>
#include <rime/algo/strings.h>
#include <rime/config/config_compiler.h>
#include <rime/config/config_cow_ref.h>
#include <rime/config/config_data.h>
Expand Down Expand Up @@ -207,15 +207,13 @@ bool ConfigData::TraverseWrite(const string& path, an<ConfigItem> item) {
}

vector<string> ConfigData::SplitPath(const string& path) {
vector<string> keys;
auto is_separator = boost::is_any_of("/");
auto trimmed_path = boost::trim_left_copy_if(path, is_separator);
boost::split(keys, trimmed_path, is_separator);
auto trimmed_path = path.substr(strings::starts_with(path, "/"));
vector<string> keys = strings::split(trimmed_path, "/");
return keys;
}

string ConfigData::JoinPath(const vector<string>& keys) {
return boost::join(keys, "/");
return strings::join(keys, "/");
}

an<ConfigItem> ConfigData::Traverse(const string& path) {
Expand Down
6 changes: 3 additions & 3 deletions src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include <cfloat>
#include <cstdlib>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
#include <rime/algo/strings.h>
#include <rime/dict/dict_settings.h>
#include <rime/dict/reverse_lookup_dictionary.h>

Expand Down Expand Up @@ -107,15 +107,15 @@ bool ReverseDb::Build(DictSettings* settings,
// save reverse lookup entries
for (const auto& v : rev_table) {
const string& key(v.first);
string value(boost::algorithm::join(v.second, " "));
string value(strings::join(v.second, " "));
key_trie_builder.Add(key, 0.0, &key_ids[i]);
value_trie_builder.Add(value, 0.0, &value_ids[i]);
++i;
}
// save stems
for (const auto& v : stems) {
string key(v.first + kStemKeySuffix);
string value(boost::algorithm::join(v.second, " "));
string value(strings::join(v.second, " "));
key_trie_builder.Add(key, 0.0, &key_ids[i]);
value_trie_builder.Add(value, 0.0, &value_ids[i]);
++i;
Expand Down
8 changes: 4 additions & 4 deletions src/rime/dict/table_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// 2013-04-18 GONG Chen <[email protected]>
//
#include <boost/algorithm/string.hpp>
#include <rime/algo/strings.h>
#include <rime/dict/table_db.h>
#include <rime/dict/user_db.h>

Expand All @@ -22,7 +22,7 @@ static bool rime_table_entry_parser(const Tsv& row,
return false;
}
string code(row[1]);
boost::algorithm::trim(code);
strings::trim(code);
*key = code + " \t" + row[0];
UserDbValue v;
if (row.size() >= 3 && !row[2].empty()) {
Expand All @@ -42,13 +42,13 @@ static bool rime_table_entry_formatter(const string& key,
Tsv* tsv) {
Tsv& row(*tsv);
// key ::= code <space> <Tab> phrase
boost::algorithm::split(row, key, boost::algorithm::is_any_of("\t"));
row = strings::split(key, "\t");
if (row.size() != 2 || row[0].empty() || row[1].empty())
return false;
UserDbValue v(value);
if (v.commits < 0) // deleted entry
return false;
boost::algorithm::trim(row[0]); // remove trailing space
strings::trim(row[0]); // remove trailing space
row[0].swap(row[1]);
row.push_back(std::to_string(v.commits));
return true;
Expand Down
10 changes: 5 additions & 5 deletions src/rime/dict/tsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// 2013-04-14 GONG Chen <[email protected]>
//
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <rime/algo/strings.h>
#include <rime/common.h>
#include <rime/dict/db_utils.h>
#include <rime/dict/tsv.h>
Expand All @@ -24,15 +24,15 @@ int TsvReader::operator()(Sink* sink) {
bool enable_comment = true;
while (getline(fin, line)) {
++line_no;
boost::algorithm::trim_right(line);
strings::trim_right(line);
// skip empty lines and comments
if (line.empty())
continue;
if (enable_comment && line[0] == '#') {
if (boost::starts_with(line, "#@")) {
if (strings::starts_with(line, "#@")) {
// metadata
line.erase(0, 2);
boost::algorithm::split(row, line, boost::algorithm::is_any_of("\t"));
row = strings::split(line, "\t");
if (row.size() != 2 || !sink->MetaPut(row[0], row[1])) {
LOG(WARNING) << "invalid metadata at line " << line_no << ".";
}
Expand All @@ -43,7 +43,7 @@ int TsvReader::operator()(Sink* sink) {
continue;
}
// read a tsv entry
boost::algorithm::split(row, line, boost::algorithm::is_any_of("\t"));
row = strings::split(line, "\t");
if (!parser_(row, &key, &value) || !sink->Put(key, value)) {
LOG(WARNING) << "invalid entry at line " << line_no << ".";
continue;
Expand Down
15 changes: 7 additions & 8 deletions src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
#include <cstdlib>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <rime/algo/strings.h>
#include <rime/service.h>
#include <rime/algo/dynamics.h>
#include <rime/dict/text_db.h>
Expand All @@ -25,8 +25,7 @@ string UserDbValue::Pack() const {
}

bool UserDbValue::Unpack(const string& value) {
vector<string> kv;
boost::split(kv, value, boost::is_any_of(" "));
vector<string> kv = strings::split(value, " ");
for (const string& k_eq_v : kv) {
size_t eq = k_eq_v.find('=');
if (eq == string::npos)
Expand Down Expand Up @@ -83,7 +82,7 @@ static bool userdb_entry_formatter(const string& key,
const string& value,
Tsv* tsv) {
Tsv& row(*tsv);
boost::algorithm::split(row, key, boost::algorithm::is_any_of("\t"));
row = strings::split(key, "\t");
if (row.size() != 2 || row[0].empty() || row[1].empty())
return false;
row.push_back(value);
Expand All @@ -107,7 +106,7 @@ bool UserDbHelper::UpdateUserInfo() {
}

bool UserDbHelper::IsUniformFormat(const string& file_name) {
return boost::ends_with(file_name, plain_userdb_extension);
return strings::ends_with(file_name, plain_userdb_extension);
}

bool UserDbHelper::UniformBackup(const string& snapshot_file) {
Expand Down Expand Up @@ -147,10 +146,10 @@ string UserDbHelper::GetDbName() {
string name;
if (!db_->MetaFetch("/db_name", &name))
return name;
auto ext = boost::find_last(name, ".userdb");
if (!ext.empty()) {
auto ext = name.rfind(".userdb");
if (ext != std::string::npos) {
// remove ".userdb.*"
name.erase(ext.begin(), name.end());
name.erase(ext, name.length() - ext);
}
return name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <algorithm>
#include <stack>
#include <cmath>
#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <rime/composition.h>
#include <rime/candidate.h>
Expand All @@ -18,6 +17,7 @@
#include <rime/engine.h>
#include <rime/schema.h>
#include <rime/translation.h>
#include <rime/algo/strings.h>
#include <rime/algo/syllabifier.h>
#include <rime/dict/corrector.h>
#include <rime/dict/dictionary.h>
Expand Down Expand Up @@ -214,7 +214,7 @@ string ScriptTranslator::Spell(const Code& code) {
vector<string> syllables;
if (!dict_ || !dict_->Decode(code, &syllables) || syllables.empty())
return result;
result = boost::algorithm::join(syllables, string(1, delimiters_.at(0)));
result = strings::join(syllables, string(1, delimiters_.at(0)));
comment_formatter_.Apply(&result);
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rime/gear/switch_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// 2013-05-26 GONG Chen <[email protected]>
//
#include <boost/algorithm/string.hpp>
#include <rime/algo/strings.h>
#include <rime/candidate.h>
#include <rime/common.h>
#include <rime/config.h>
Expand Down Expand Up @@ -184,7 +184,7 @@ void FoldedOptions::Append(const SwitchOption& option, size_t state_index) {
}

void FoldedOptions::Finish() {
text_ = prefix_ + boost::algorithm::join(labels_, separator_) + suffix_;
text_ = prefix_ + strings::join(labels_, separator_) + suffix_;
}

class SwitchTranslation : public FifoTranslation {
Expand Down
6 changes: 3 additions & 3 deletions src/rime/gear/unity_table_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// 2013-08-31 GONG Chen <[email protected]>
//
#include <boost/algorithm/string.hpp>
#include <rime/algo/strings.h>
#include <rime/dict/dict_settings.h>
#include <rime/dict/user_dictionary.h>
#include <rime/dict/reverse_lookup_dictionary.h>
Expand Down Expand Up @@ -62,7 +62,7 @@ bool UnityTableEncoder::TranslateWord(const string& word,
string str_list;
if (rev_dict_->LookupStems(word, &str_list) ||
rev_dict_->ReverseLookup(word, &str_list)) {
boost::split(*code, str_list, boost::is_any_of(" "));
*code = strings::split(str_list, " ");
return !code->empty();
}
return false;
Expand All @@ -80,7 +80,7 @@ size_t UnityTableEncoder::LookupPhrases(UserDictEntryIterator* result,
}

bool UnityTableEncoder::HasPrefix(const string& key) {
return boost::starts_with(key, kEncodedPrefix);
return strings::starts_with(key, kEncodedPrefix);
}

bool UnityTableEncoder::AddPrefix(string* key) {
Expand Down

0 comments on commit 1058295

Please sign in to comment.