refactor(string_util): Use rvalue references for *trim()

This commit is contained in:
Michael Carlberg 2016-12-14 06:51:07 +01:00
parent eca870774f
commit 5077325044
9 changed files with 22 additions and 15 deletions

View File

@ -23,9 +23,9 @@ namespace string_util {
string squeeze(const string& haystack, char needle); string squeeze(const string& haystack, char needle);
string strip(const string& haystack, char needle); string strip(const string& haystack, char needle);
string strip_trailing_newline(const string& haystack); string strip_trailing_newline(const string& haystack);
string ltrim(const string& haystack, char needle); string ltrim(string&& value, const char& needle);
string rtrim(const string& haystack, char needle); string rtrim(string&& value, const char& needle);
string trim(const string& haystack, char needle); string trim(string&& value, const char& needle);
string join(const vector<string>& strs, const string& delim); string join(const vector<string>& strs, const string& delim);
vector<string>& split_into(const string& s, char delim, vector<string>& container); vector<string>& split_into(const string& s, char delim, vector<string>& container);
vector<string> split(const string& s, char delim); vector<string> split(const string& s, char delim);

View File

@ -95,6 +95,8 @@ void config::parse_file() {
if (line[0] == '[' && line[line.length() - 1] == ']') { if (line[0] == '[' && line[line.length() - 1] == ']') {
section = line.substr(1, line.length() - 2); section = line.substr(1, line.length() - 2);
continue; continue;
} else if (section.empty()) {
continue;
} }
size_t equal_pos; size_t equal_pos;
@ -104,14 +106,19 @@ void config::parse_file() {
continue; continue;
} }
string key{string_util::trim(line.substr(0, equal_pos), ' ')}; string key{forward<string>(string_util::trim(forward<string>(line.substr(0, equal_pos)), ' '))};
string value{string_util::trim(string_util::trim(line.substr(equal_pos + 1), ' '), '"')};
auto it = m_sections[section].find(key); auto it = m_sections[section].find(key);
if (it != m_sections[section].end()) { if (it != m_sections[section].end()) {
throw key_error("Duplicate key name \"" + key + "\" defined on line " + to_string(lineno)); throw key_error("Duplicate key name \"" + key + "\" defined on line " + to_string(lineno));
} }
if (line.size() > equal_pos + 1) {
line.erase(0, equal_pos + 1);
}
string value{string_util::trim(string_util::trim(move(line), ' '), '"')};
m_sections[section].emplace_hint(it, move(key), move(value)); m_sections[section].emplace_hint(it, move(key), move(value));
} }

View File

@ -46,7 +46,7 @@ void parser::codeblock(string&& data) {
size_t pos; size_t pos;
while (data.length()) { while (data.length()) {
data = string_util::ltrim(data, ' '); data = string_util::ltrim(move(data), ' ');
if (data.empty()) { if (data.empty()) {
break; break;

View File

@ -45,7 +45,7 @@ namespace drawtypes {
vector<icon_t> vec; vector<icon_t> vec;
vector<string> frames; vector<string> frames;
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(move(name), '>'), '<');
auto anim_defaults = load_optional_icon(conf, section, name); auto anim_defaults = load_optional_icon(conf, section, name);

View File

@ -124,7 +124,7 @@ namespace drawtypes {
vector<token> tokens; vector<token> tokens;
size_t start, end, pos; size_t start, end, pos;
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(move(name), '>'), '<');
string text; string text;

View File

@ -87,7 +87,7 @@ namespace drawtypes {
*/ */
progressbar_t load_progressbar(const bar_settings& bar, const config& conf, const string& section, string name) { progressbar_t load_progressbar(const bar_settings& bar, const config& conf, const string& section, string name) {
// Remove the start and end tag from the name in case a format tag is passed // Remove the start and end tag from the name in case a format tag is passed
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(move(name), '>'), '<');
string format = "%fill%%indicator%%empty%"; string format = "%fill%%indicator%%empty%";
unsigned int width; unsigned int width;

View File

@ -27,7 +27,7 @@ namespace drawtypes {
* from the configuration * from the configuration
*/ */
ramp_t load_ramp(const config& conf, const string& section, string name, bool required) { ramp_t load_ramp(const config& conf, const string& section, string name, bool required) {
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(move(name), '>'), '<');
auto ramp_defaults = load_optional_icon(conf, section, name); auto ramp_defaults = load_optional_icon(conf, section, name);

View File

@ -142,7 +142,7 @@ namespace modules {
} }
// Trim leading and trailing whitespace // Trim leading and trailing whitespace
ws_name = string_util::trim(ws_name, ' '); ws_name = string_util::trim(move(ws_name), ' ');
auto icon = m_icons->get(ws->name, DEFAULT_WS_ICON); auto icon = m_icons->get(ws->name, DEFAULT_WS_ICON);
auto label = m_statelabels.find(ws_state)->second->clone(); auto label = m_statelabels.find(ws_state)->second->clone();

View File

@ -122,7 +122,7 @@ namespace string_util {
/** /**
* Remove needle from the start of the string * Remove needle from the start of the string
*/ */
string ltrim(const string& haystack, char needle) { string ltrim(string&& haystack, const char& needle) {
string str(haystack); string str(haystack);
while (str[0] == needle) { while (str[0] == needle) {
str.erase(0, 1); str.erase(0, 1);
@ -133,7 +133,7 @@ namespace string_util {
/** /**
* Remove needle from the end of the string * Remove needle from the end of the string
*/ */
string rtrim(const string& haystack, char needle) { string rtrim(string&& haystack, const char& needle) {
string str(haystack); string str(haystack);
while (str[str.length() - 1] == needle) { while (str[str.length() - 1] == needle) {
str.erase(str.length() - 1, 1); str.erase(str.length() - 1, 1);
@ -144,8 +144,8 @@ namespace string_util {
/** /**
* Remove needle from the start and end of the string * Remove needle from the start and end of the string
*/ */
string trim(const string& haystack, char needle) { string trim(string&& value, const char& needle) {
return rtrim(ltrim(haystack, needle), needle); return rtrim(ltrim(move(value), needle), needle);
} }
/** /**