Replace utils::replace, strip, and strip_end with their Boost equivalents

This commit is contained in:
Charles Dang 2016-10-17 00:00:46 +11:00
parent 047bae7b66
commit 8e3c554094
9 changed files with 51 additions and 73 deletions

View File

@ -38,6 +38,8 @@
#include "formula/string_utils.hpp"
#include "addon/client.hpp"
#include <boost/algorithm/string.hpp>
static lg::log_domain log_config("config");
#define ERR_CFG LOG_STREAM(err , log_config)
#define LOG_CFG LOG_STREAM(info, log_config)
@ -235,7 +237,7 @@ static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignor
std::istream *stream = filesystem::istream_file(ign_file);
std::string line;
while (std::getline(*stream, line)) {
utils::strip(line);
boost::trim(line);
const size_t l = line.size();
// .gitignore & WML like comments
if (l == 0 || !line.compare(0,2,"# ")) continue;

View File

@ -21,6 +21,8 @@
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include <boost/algorithm/string.hpp>
static lg::log_domain log_network("network");
#define LOG_CS if (lg::err().dont_log(log_network)) ; else lg::err()(log_network, false)
@ -43,7 +45,7 @@ std::string fast_interpolate_variables_into_string(const std::string &str, const
if(symbols) {
for(const plain_string_map::value_type& sym : *symbols) {
res = utils::replace(res, "$" + sym.first, sym.second);
res = boost::replace_all(res, "$" + sym.first, sym.second);
}
}

View File

@ -43,6 +43,8 @@
#include <SDL_image.h>
#include "utils/functional.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/functional/hash.hpp>
#include <list>
@ -410,7 +412,7 @@ static bool localized_file_uptodate (const std::string& loc_file)
if (p1 == std::string::npos)
continue;
std::string state = line.substr(0, p1);
utils::strip(state);
boost::trim(state);
if (state == "fuzzy") {
size_t p2 = line.find(fsep, p1 + fsep.length());
if (p2 == std::string::npos)

View File

@ -19,6 +19,8 @@ See the COPYING file for more details.
#include "formula/string_utils.hpp"
#include "gettext.hpp"
#include <boost/algorithm/string.hpp>
namespace events {
//simple command args parser, separated from command_handler for clarity.
@ -70,7 +72,8 @@ public:
advance_to_arg(n);
if (n < args.size()) {
std::string data(str_, args[n]);
return utils::strip(data);
boost::trim(data);
return data;
}
else {
return "";

View File

@ -29,6 +29,8 @@
#include <cassert>
#include <array>
#include <boost/algorithm/string.hpp>
static lg::log_domain log_engine("engine");
#define ERR_GENERAL LOG_STREAM(err, lg::general())
#define ERR_NG LOG_STREAM(err, log_engine)
@ -57,37 +59,6 @@ bool notspace(const char c)
return !portable_isspace(c);
}
std::string replace(std::string str, const std::string &src, const std::string &dst)
{
std::string::size_type pos = 0;
while ( (pos = str.find(src, pos)) != std::string::npos ) {
str.replace( pos, src.size(), dst );
pos++;
}
return str;
}
std::string &strip(std::string &str)
{
// If all the string contains is whitespace,
// then the whitespace may have meaning, so don't strip it
std::string::iterator it = std::find_if(str.begin(), str.end(), notspace);
if (it == str.end())
return str;
str.erase(str.begin(), it);
str.erase(std::find_if(str.rbegin(), str.rend(), notspace).base(), str.end());
return str;
}
std::string &strip_end(std::string &str)
{
str.erase(std::find_if(str.rbegin(), str.rend(), notspace).base(), str.end());
return str;
}
/**
* Splits a (comma-)separated string into a vector of pieces.
* @param[in] val A (comma-)separated string.
@ -113,7 +84,7 @@ std::vector< std::string > split(std::string const &val, const char c, const int
if (*i2 == c) {
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip_end(new_val);
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
++i2;
@ -130,7 +101,7 @@ std::vector< std::string > split(std::string const &val, const char c, const int
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip_end(new_val);
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
@ -186,27 +157,28 @@ std::vector< std::string > square_parenthetical_split(std::string const &val,
size_t found_asterisk = piece.find_first_of('*');
if (found_asterisk == std::string::npos) {
std::string tmp2(piece);
square_expansion.push_back(strip(tmp2));
boost::trim(tmp2);
square_expansion.push_back(tmp2);
}
else { //'*' multiple expansion
std::string s_begin = piece.substr(0,found_asterisk);
s_begin = strip(s_begin);
boost::trim(s_begin);
std::string s_end = piece.substr(found_asterisk+1);
s_end = strip(s_end);
boost::trim(s_end);
for (int ast=std::stoi(s_end); ast>0; --ast)
square_expansion.push_back(s_begin);
}
}
else { //expand number range
std::string s_begin = piece.substr(0,found_tilde);
s_begin = strip(s_begin);
boost::trim(s_begin);
int begin = std::stoi(s_begin);
size_t padding = 0, padding_end = 0;
while (padding<s_begin.size() && s_begin[padding]=='0') {
padding++;
}
std::string s_end = piece.substr(found_tilde+1);
s_end = strip(s_end);
boost::trim(s_end);
int end = std::stoi(s_end);
while (padding_end<s_end.size() && s_end[padding_end]=='0') {
padding_end++;
@ -254,7 +226,7 @@ std::vector< std::string > square_parenthetical_split(std::string const &val,
std::string tmp_val(j1, i2);
new_val.append(tmp_val);
if (flags & STRIP_SPACES)
strip_end(new_val);
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
j++;
@ -364,7 +336,7 @@ std::vector< std::string > parenthetical_split(std::string const &val,
if(!in_parenthesis && separator && *i2 == separator){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip_end(new_val);
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
++i2;
@ -380,7 +352,7 @@ std::vector< std::string > parenthetical_split(std::string const &val,
if(!separator && part.empty()){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
boost::trim(new_val);
res.push_back(new_val);
++i2;
i1=i2;
@ -397,7 +369,7 @@ std::vector< std::string > parenthetical_split(std::string const &val,
if (!separator && part.empty()){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
boost::trim(new_val);
res.push_back(new_val);
++i2;
i1=i2;
@ -417,7 +389,7 @@ std::vector< std::string > parenthetical_split(std::string const &val,
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
@ -791,7 +763,7 @@ std::vector< std::string > quoted_split(std::string const &val, char c, int flag
} else if (*i2 == c) {
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
++i2;
@ -808,7 +780,7 @@ std::vector< std::string > quoted_split(std::string const &val, char c, int flag
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);

View File

@ -225,15 +225,6 @@ std::string unescape(const std::string &str);
/** Percent-escape characters in a UTF-8 string intended to be part of a URL. */
std::string urlencode(const std::string &str);
/** Replace all instances of src in str with dst */
std::string replace(std::string str, const std::string &src, const std::string &dst);
/** Remove whitespace from the front and back of the string 'str'. */
std::string &strip(std::string &str);
/** Remove whitespace from the back of the string 'str'. */
std::string &strip_end(std::string &str);
/** Surround the string 'str' with double quotes. */
inline std::string quote(const std::string &str)
{

View File

@ -62,6 +62,8 @@
#include <csignal>
#include <boost/algorithm/string.hpp>
static lg::log_domain log_server("server");
/**
* fatal and directly server related errors/warnings,
@ -1965,7 +1967,7 @@ void server::start_new_server() {
}
std::string server::process_command(std::string query, std::string issuer_name) {
utils::strip(query);
boost::trim(query);
if (issuer_name == "*socket*" && query.at(0) == '+') {
// The first argument might be "+<issuer>: ".
@ -1977,7 +1979,7 @@ std::string server::process_command(std::string query, std::string issuer_name)
if (!issuer.empty()) {
issuer_name = "+" + issuer + "+";
query = std::string(issuer_end + 1, query.end());
utils::strip(query);
boost::trim(query);
}
}
@ -1987,7 +1989,7 @@ std::string server::process_command(std::string query, std::string issuer_name)
const std::string command = utf8::lowercase(std::string(query.begin(), i));
std::string parameters = (i == query.end() ? "" : std::string(i + 1, query.end()));
utils::strip(parameters);
boost::trim(parameters);
std::ostringstream out;
std::map<std::string, server::cmd_handler>::iterator handler_itor = cmd_handlers_.find(command);
@ -2175,7 +2177,7 @@ void server::pm_handler(const std::string& issuer_name, const std::string& /*que
const std::string& sender = issuer_name;
const std::string receiver(parameters.begin(), first_space);
std::string message(first_space + 1, parameters.end());
utils::strip(message);
boost::trim(message);
if (message.empty()) {
*out << "You must type a message.";
return;
@ -2300,9 +2302,9 @@ void server::bans_handler(const std::string& /*issuer_name*/, const std::string&
ban_manager_.list_deleted_bans(*out);
} else if (utf8::lowercase(parameters).find("deleted") == 0) {
std::string mask = parameters.substr(7);
ban_manager_.list_deleted_bans(*out, utils::strip(mask));
ban_manager_.list_deleted_bans(*out, boost::trim(mask));
} else {
ban_manager_.list_bans(*out, utils::strip(parameters));
ban_manager_.list_bans(*out, boost::trim(parameters));
}
} catch ( utf8::invalid_utf8_exception & e ) {
@ -2336,7 +2338,7 @@ void server::ban_handler(const std::string& issuer_name, const std::string& /*qu
--second_space;
}
std::string reason(second_space + 1, parameters.end());
utils::strip(reason);
boost::trim(reason);
if (reason.empty()) {
*out << "You need to give a reason for the ban.";
return;
@ -2404,7 +2406,7 @@ void server::kickban_handler(const std::string& issuer_name, const std::string&
--second_space;
}
std::string reason(second_space + 1, parameters.end());
utils::strip(reason);
boost::trim(reason);
if (reason.empty()) {
*out << "You need to give a reason for the ban.";
return;
@ -2492,7 +2494,7 @@ void server::gban_handler(const std::string& issuer_name, const std::string& /*q
--second_space;
}
std::string reason(second_space + 1, parameters.end());
utils::strip(reason);
boost::trim(reason);
if (reason.empty()) {
*out << "You need to give a reason for the ban.";
return;

View File

@ -26,6 +26,8 @@
#include <algorithm>
#include <boost/algorithm/string.hpp>
context_free_grammar_generator::~context_free_grammar_generator()
{
}
@ -40,7 +42,7 @@ context_free_grammar_generator::context_free_grammar_generator(const std::string
while (*reading != 0) {
if (*reading == '=') {
// Leading and trailing whitespace is not significant, but internal whitespace is
std::string key = utils::strip(buf);
std::string key = boost::trim_copy(buf);
if(key == "!" || key =="(" || key == ")") {
throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: nonterminals (, ! and ) may not be overridden");
}
@ -80,7 +82,7 @@ context_free_grammar_generator::context_free_grammar_generator(const std::string
if (!filled) {
throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: misplaced } symbol");
}
filled->push_back('{' + utils::strip(buf));
filled->push_back('{' + boost::trim_copy(buf));
buf.clear();
} else buf.push_back(*reading);
}
@ -93,7 +95,7 @@ context_free_grammar_generator::context_free_grammar_generator(const std::map<st
{
for(auto rule : source) {
std::string key = rule.first; // Need to do this because utils::strip is mutating
key = utils::strip(key);
boost::trim(key);
if(key == "!" || key =="(" || key == ")") {
throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: nonterminals (, ! and ) may not be overridden");
}
@ -114,8 +116,8 @@ context_free_grammar_generator::context_free_grammar_generator(const std::map<st
if (!filled) {
throw name_generator_invalid_exception("[context_free_grammar_generator] Parsing error: misplaced } symbol");
}
filled->push_back('{' + utils::strip(buf));
filled->push_back('{' + boost::trim_copy(buf));
buf.clear();
} else buf.push_back(c);
}

View File

@ -19,6 +19,8 @@
#include <cassert>
#include <functional>
#include <boost/algorithm/string.hpp>
version_info::version_info()
: nums_(3,0), special_(""), special_separator_('\0')
{
@ -39,7 +41,7 @@ version_info::version_info(const std::string& str)
, special_separator_('\0')
{
std::string v = str;
utils::strip(v);
boost::trim(v);
if(v.empty())
return;