Skip to content

Commit

Permalink
Continue to Migrate from Boost to std (#755)
Browse files Browse the repository at this point in the history
* refactor: string formatting by sstream

* refactor: replace boost::all with std::all_of, etc.

* refactor(utilities.cc): improve CompareVersionString implementation

* refactor(string_table.cc): use stringstream map and dump marisa trie

* refactor(mappped_file.h): self decleared noncopyable

* refactor: apply clang format
  • Loading branch information
WhiredPlanck authored Nov 11, 2023
1 parent af3d4a7 commit 1c78709
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 50 deletions.
3 changes: 2 additions & 1 deletion plugins/plugins_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Distributed under the BSD License
//

#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <boost/dll.hpp>
#include <filesystem>
Expand Down Expand Up @@ -78,7 +79,7 @@ string PluginManager::plugin_name_of(fs::path plugin_file) {
}
// replace dash with underscore, for the plugin name is part of the module
// initializer function name.
boost::replace_all(name, "-", "_");
std::replace(name.begin(), name.end(), '-', '_');
return name;
}

Expand Down
36 changes: 13 additions & 23 deletions src/rime/algo/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,25 @@
//
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <rime/algo/utilities.h>

namespace rime {

int CompareVersionString(const string& x, const string& y) {
if (x.empty() && y.empty())
return 0;
if (x.empty())
return -1;
if (y.empty())
return 1;
vector<string> xx, yy;
boost::split(xx, x, boost::is_any_of("."));
boost::split(yy, y, boost::is_any_of("."));
size_t i = 0;
for (; i < xx.size() && i < yy.size(); ++i) {
int dx = atoi(xx[i].c_str());
int dy = atoi(yy[i].c_str());
if (dx != dy)
return dx - dy;
int c = xx[i].compare(yy[i]);
if (c != 0)
return c;
size_t i = 0, j = 0, m = x.size(), n = y.size();
while (i < m || j < n) {
int v1 = 0, v2 = 0;
while (i < m && x[i] != '.')
v1 = v1 * 10 + (int)(x[i++] - '0');
++i;
while (j < n && y[j] != '.')
v2 = v2 * 10 + (int)(y[j++] - '0');
++j;
if (v1 > v2)
return 1;
if (v1 < v2)
return -1;
}
if (i < xx.size())
return 1;
if (i < yy.size())
return -1;
return 0;
}

Expand Down
12 changes: 7 additions & 5 deletions src/rime/config/config_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <boost/format.hpp>
#include <yaml-cpp/yaml.h>
#include <rime/config/config_compiler.h>
#include <rime/config/config_cow_ref.h>
Expand Down Expand Up @@ -96,7 +96,9 @@ bool ConfigData::IsListItemReference(const string& key) {
}

string ConfigData::FormatListIndex(size_t index) {
return boost::str(boost::format("@%u") % index);
std::ostringstream formatted;
formatted << "@" << index;
return formatted.str();
}

static const string kAfter("after");
Expand Down Expand Up @@ -286,9 +288,9 @@ an<ConfigItem> ConvertFromYaml(const YAML::Node& node,
void EmitScalar(const string& str_value, YAML::Emitter* emitter) {
if (str_value.find_first_of("\r\n") != string::npos) {
*emitter << YAML::Literal;
} else if (!boost::algorithm::all(str_value,
boost::algorithm::is_alnum() ||
boost::algorithm::is_any_of("_."))) {
} else if (!std::all_of(str_value.cbegin(), str_value.cend(), [](auto ch) {
return std::isalnum(ch) || ch == '_' || ch == '.';
})) {
*emitter << YAML::DoubleQuoted;
}
*emitter << str_value;
Expand Down
7 changes: 5 additions & 2 deletions src/rime/dict/mapped_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <stdint.h>
#include <cstring>
#include <boost/utility.hpp>
#include <rime_api.h>
#include <rime/common.h>

Expand Down Expand Up @@ -81,7 +80,7 @@ struct List {

class MappedFileImpl;

class RIME_API MappedFile : boost::noncopyable {
class RIME_API MappedFile {
protected:
explicit MappedFile(const string& file_name);
virtual ~MappedFile();
Expand All @@ -106,6 +105,10 @@ class RIME_API MappedFile : boost::noncopyable {
char* address() const;

public:
// noncpyable
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;

bool Exists() const;
bool IsOpen() const;
void Close();
Expand Down
13 changes: 7 additions & 6 deletions src/rime/dict/string_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
// 2014-07-04 GONG Chen <[email protected]>
//

#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>
#include <rime/common.h>
#include <rime/dict/string_table.h>

namespace rime {

StringTable::StringTable(const char* ptr, size_t size) {
trie_.map(ptr, size);
std::stringstream stream;
stream.write(ptr, size);
stream >> trie_;
}

bool StringTable::HasKey(const string& key) {
Expand Down Expand Up @@ -106,10 +107,10 @@ void StringTableBuilder::Dump(char* ptr, size_t size) {
LOG(ERROR) << "insufficient memory to dump string table.";
return;
}
namespace io = boost::iostreams;
io::basic_array_sink<char> sink(ptr, size);
io::stream<io::basic_array_sink<char>> stream(sink);

std::stringstream stream;
stream << trie_;
stream.read(ptr, size);
}

} // namespace rime
6 changes: 4 additions & 2 deletions src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// 2011-11-02 GONG Chen <[email protected]>
//
#include <cstdlib>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <rime/service.h>
#include <rime/algo/dynamics.h>
#include <rime/dict/text_db.h>
Expand All @@ -19,7 +19,9 @@ UserDbValue::UserDbValue(const string& value) {
}

string UserDbValue::Pack() const {
return boost::str(boost::format("c=%1% d=%2% t=%3%") % commits % dee % tick);
std::ostringstream packed;
packed << "c=" << commits << " d=" << dee << " t=" << tick;
return packed.str();
}

bool UserDbValue::Unpack(const string& value) {
Expand Down
5 changes: 3 additions & 2 deletions src/rime/gear/shape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// 2013-07-02 GONG Chen <[email protected]>
//
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <rime/context.h>
#include <rime/engine.h>
#include <rime/key_event.h>
Expand All @@ -17,7 +17,8 @@ void ShapeFormatter::Format(string* text) {
if (!engine_->context()->get_option("full_shape")) {
return;
}
if (boost::all(*text, !boost::is_from_range('\x20', '\x7e'))) {
if (std::all_of(text->cbegin(), text->cend(),
[](auto ch) { return (ch >= 0x20 && ch <= 0x7e); })) {
return;
}
std::ostringstream oss;
Expand Down
11 changes: 6 additions & 5 deletions src/rime/key_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// 2011-04-20 GONG Chen <[email protected]>
//
#include <sstream>
#include <boost/format.hpp>
#include <iomanip>
#include <rime/key_event.h>
#include <rime/key_table.h>

Expand Down Expand Up @@ -36,15 +36,16 @@ string KeyEvent::repr() const {
return modifiers.str() + name;
}
// no name :-| return its hex value
string value;
std::ostringstream value;
value << "0x" << std::setfill('0');
if (keycode_ <= 0xffff) {
value = boost::str(boost::format("0x%4x") % keycode_);
value << std::setw(4) << std::hex << keycode_;
} else if (keycode_ <= 0xffffff) {
value = boost::str(boost::format("0x%6x") % keycode_);
value << std::setw(6) << std::hex << keycode_;
} else {
return "(unknown)"; // invalid keycode
}
return modifiers.str() + value;
return modifiers.str() + value.str();
}

bool KeyEvent::Parse(const string& repr) {
Expand Down
5 changes: 3 additions & 2 deletions src/rime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
#include <cstring>
#include <sstream>
#include <boost/format.hpp>
#include <rime/common.h>
#include <rime/composition.h>
#include <rime/config.h>
Expand Down Expand Up @@ -737,7 +736,9 @@ RIME_API Bool RimeConfigNext(RimeConfigIterator* iterator) {
++p->iter;
if (p->iter == p->end)
return False;
p->key = boost::str(boost::format("@%1%") % iterator->index);
std::ostringstream key;
key << "@" << iterator->index;
p->key = key.str();
p->path = p->prefix + p->key;
iterator->key = p->key.c_str();
iterator->path = p->path.c_str();
Expand Down
6 changes: 4 additions & 2 deletions test/config_compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Distributed under the BSD License
//

#include <boost/format.hpp>
#include <sstream>
#include <gtest/gtest.h>
#include <rime/component.h>
#include <rime/config.h>
Expand Down Expand Up @@ -34,7 +34,9 @@ class RimeConfigCompilerTest : public RimeConfigCompilerTestBase {

TEST_F(RimeConfigCompilerTest, IncludeLocalReference) {
for (size_t i = 0; i < 4; ++i) {
auto prefix = boost::str(boost::format("include_local_reference/@%u/") % i);
std::ostringstream oss;
oss << "include_local_reference/@" << i << "/";
const auto& prefix(oss.str());
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
string actual;
EXPECT_TRUE(config_->GetString(prefix + "terrans/player", &actual));
Expand Down

0 comments on commit 1c78709

Please sign in to comment.