introduce string_view

This commit is contained in:
Chunting Gu
2021-06-17 18:58:53 +08:00
parent 751a3539ae
commit 26bb6b341a
38 changed files with 404 additions and 344 deletions
+30 -9
View File
@@ -36,19 +36,19 @@ bool ToSizeT(const std::string& str, int base, std::size_t* size) {
return true;
}
void Split(boost::string_view input, char delim, bool compress_token,
std::vector<boost::string_view>* output) {
void Split(string_view input, char delim, bool compress_token,
std::vector<string_view>* output) {
std::size_t i = 0;
std::size_t p = 0;
i = input.find(delim);
while (i != boost::string_view::npos) {
while (i != string_view::npos) {
output->emplace_back(input.substr(p, i - p));
p = i + 1;
if (compress_token) {
while (input[p] == delim) {
while (p < input.size() && input[p] == delim) {
++p;
}
}
@@ -59,10 +59,19 @@ void Split(boost::string_view input, char delim, bool compress_token,
output->emplace_back(input.substr(p, i - p));
}
bool SplitKV(const std::string& input, char delim, bool trim_spaces,
std::string* key, std::string* value) {
void Trim(string_view& sv, const char* spaces) {
sv.remove_prefix(std::min(sv.find_first_not_of(spaces), sv.size()));
std::size_t pos = sv.find_last_not_of(spaces);
if (pos != sv.npos) {
sv.remove_suffix(sv.size() - pos - 1);
}
}
bool SplitKV(string_view input, char delim, bool trim_spaces, string_view* key,
string_view* value) {
std::size_t pos = input.find(delim);
if (pos == std::string::npos) {
if (pos == input.npos) {
return false;
}
@@ -70,11 +79,23 @@ bool SplitKV(const std::string& input, char delim, bool trim_spaces,
*value = input.substr(pos + 1);
if (trim_spaces) {
boost::trim(*key);
boost::trim(*value);
Trim(*key);
Trim(*value);
}
return true;
}
bool SplitKV(string_view input, char delim, bool trim_spaces, std::string* key,
std::string* value) {
string_view key_view;
string_view value_view;
if (SplitKV(input, delim, trim_spaces, &key_view, &value_view)) {
*key = ToString(key_view);
*value = ToString(value_view);
return true;
}
return false;
}
} // namespace webcc