diff --git a/rtc_base/string_encode.cc b/rtc_base/string_encode.cc index 3b41076fd8..364eaa0f0f 100644 --- a/rtc_base/string_encode.cc +++ b/rtc_base/string_encode.cc @@ -152,7 +152,7 @@ size_t hex_decode_with_delimiter(char* buffer, source.length(), delimiter); } -size_t tokenize(const std::string& source, +size_t tokenize(absl::string_view source, char delimiter, std::vector* fields) { fields->clear(); @@ -160,18 +160,18 @@ size_t tokenize(const std::string& source, for (size_t i = 0; i < source.length(); ++i) { if (source[i] == delimiter) { if (i != last) { - fields->push_back(source.substr(last, i - last)); + fields->emplace_back(source.substr(last, i - last)); } last = i + 1; } } if (last != source.length()) { - fields->push_back(source.substr(last, source.length() - last)); + fields->emplace_back(source.substr(last, source.length() - last)); } return fields->size(); } -bool tokenize_first(const std::string& source, +bool tokenize_first(absl::string_view source, const char delimiter, std::string* token, std::string* rest) { @@ -183,12 +183,12 @@ bool tokenize_first(const std::string& source, // Look for additional occurrances of delimiter. size_t right_pos = left_pos + 1; - while (source[right_pos] == delimiter) { + while (right_pos < source.size() && source[right_pos] == delimiter) { right_pos++; } - *token = source.substr(0, left_pos); - *rest = source.substr(right_pos); + *token = std::string(source.substr(0, left_pos)); + *rest = std::string(source.substr(right_pos)); return true; } @@ -214,7 +214,7 @@ std::string join(const std::vector& source, char delimiter) { return joined_string; } -size_t split(const std::string& source, +size_t split(absl::string_view source, char delimiter, std::vector* fields) { RTC_DCHECK(fields); @@ -222,11 +222,11 @@ size_t split(const std::string& source, size_t last = 0; for (size_t i = 0; i < source.length(); ++i) { if (source[i] == delimiter) { - fields->push_back(source.substr(last, i - last)); + fields->emplace_back(source.substr(last, i - last)); last = i + 1; } } - fields->push_back(source.substr(last, source.length() - last)); + fields->emplace_back(source.substr(last)); return fields->size(); } diff --git a/rtc_base/string_encode.h b/rtc_base/string_encode.h index 577c45f9f0..356fdfaaf7 100644 --- a/rtc_base/string_encode.h +++ b/rtc_base/string_encode.h @@ -17,6 +17,7 @@ #include #include +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "rtc_base/checks.h" #include "rtc_base/string_to_number.h" @@ -62,20 +63,20 @@ std::string join(const std::vector& source, char delimiter); // Splits the source string into multiple fields separated by delimiter, // with duplicates of delimiter creating empty fields. -size_t split(const std::string& source, +size_t split(absl::string_view source, char delimiter, std::vector* fields); // Splits the source string into multiple fields separated by delimiter, // with duplicates of delimiter ignored. Trailing delimiter ignored. -size_t tokenize(const std::string& source, +size_t tokenize(absl::string_view source, char delimiter, std::vector* fields); // Extract the first token from source as separated by delimiter, with // duplicates of delimiter ignored. Return false if the delimiter could not be // found, otherwise return true. -bool tokenize_first(const std::string& source, +bool tokenize_first(absl::string_view source, const char delimiter, std::string* token, std::string* rest);