Use string_view as input type for internal string utilities

Bug: None
Change-Id: I2bfdaf4e7fac109842cc9fde8dfa28ab4961c3fd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/232127
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35022}
This commit is contained in:
Niels Möller 2021-09-16 15:39:16 +02:00 committed by WebRTC LUCI CQ
parent d14f98f635
commit 4420380f38
2 changed files with 14 additions and 13 deletions

View file

@ -152,7 +152,7 @@ size_t hex_decode_with_delimiter(char* buffer,
source.length(), delimiter); source.length(), delimiter);
} }
size_t tokenize(const std::string& source, size_t tokenize(absl::string_view source,
char delimiter, char delimiter,
std::vector<std::string>* fields) { std::vector<std::string>* fields) {
fields->clear(); fields->clear();
@ -160,18 +160,18 @@ size_t tokenize(const std::string& source,
for (size_t i = 0; i < source.length(); ++i) { for (size_t i = 0; i < source.length(); ++i) {
if (source[i] == delimiter) { if (source[i] == delimiter) {
if (i != last) { if (i != last) {
fields->push_back(source.substr(last, i - last)); fields->emplace_back(source.substr(last, i - last));
} }
last = i + 1; last = i + 1;
} }
} }
if (last != source.length()) { 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(); return fields->size();
} }
bool tokenize_first(const std::string& source, bool tokenize_first(absl::string_view source,
const char delimiter, const char delimiter,
std::string* token, std::string* token,
std::string* rest) { std::string* rest) {
@ -183,12 +183,12 @@ bool tokenize_first(const std::string& source,
// Look for additional occurrances of delimiter. // Look for additional occurrances of delimiter.
size_t right_pos = left_pos + 1; size_t right_pos = left_pos + 1;
while (source[right_pos] == delimiter) { while (right_pos < source.size() && source[right_pos] == delimiter) {
right_pos++; right_pos++;
} }
*token = source.substr(0, left_pos); *token = std::string(source.substr(0, left_pos));
*rest = source.substr(right_pos); *rest = std::string(source.substr(right_pos));
return true; return true;
} }
@ -214,7 +214,7 @@ std::string join(const std::vector<std::string>& source, char delimiter) {
return joined_string; return joined_string;
} }
size_t split(const std::string& source, size_t split(absl::string_view source,
char delimiter, char delimiter,
std::vector<std::string>* fields) { std::vector<std::string>* fields) {
RTC_DCHECK(fields); RTC_DCHECK(fields);
@ -222,11 +222,11 @@ size_t split(const std::string& source,
size_t last = 0; size_t last = 0;
for (size_t i = 0; i < source.length(); ++i) { for (size_t i = 0; i < source.length(); ++i) {
if (source[i] == delimiter) { if (source[i] == delimiter) {
fields->push_back(source.substr(last, i - last)); fields->emplace_back(source.substr(last, i - last));
last = i + 1; last = i + 1;
} }
} }
fields->push_back(source.substr(last, source.length() - last)); fields->emplace_back(source.substr(last));
return fields->size(); return fields->size();
} }

View file

@ -17,6 +17,7 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/string_to_number.h" #include "rtc_base/string_to_number.h"
@ -62,20 +63,20 @@ std::string join(const std::vector<std::string>& source, char delimiter);
// Splits the source string into multiple fields separated by delimiter, // Splits the source string into multiple fields separated by delimiter,
// with duplicates of delimiter creating empty fields. // with duplicates of delimiter creating empty fields.
size_t split(const std::string& source, size_t split(absl::string_view source,
char delimiter, char delimiter,
std::vector<std::string>* fields); std::vector<std::string>* fields);
// Splits the source string into multiple fields separated by delimiter, // Splits the source string into multiple fields separated by delimiter,
// with duplicates of delimiter ignored. Trailing delimiter ignored. // with duplicates of delimiter ignored. Trailing delimiter ignored.
size_t tokenize(const std::string& source, size_t tokenize(absl::string_view source,
char delimiter, char delimiter,
std::vector<std::string>* fields); std::vector<std::string>* fields);
// Extract the first token from source as separated by delimiter, with // Extract the first token from source as separated by delimiter, with
// duplicates of delimiter ignored. Return false if the delimiter could not be // duplicates of delimiter ignored. Return false if the delimiter could not be
// found, otherwise return true. // found, otherwise return true.
bool tokenize_first(const std::string& source, bool tokenize_first(absl::string_view source,
const char delimiter, const char delimiter,
std::string* token, std::string* token,
std::string* rest); std::string* rest);