libchromeos: Migrate string_utils from Buffet to libchromeos BUG=chromium:405714 TEST=USE=buffet ./build_packages Change-Id: I95d8977aed6ffd718751de058d4cdda3a5395c25 Reviewed-on: https://chromium-review.googlesource.com/213361 Tested-by: Alex Vakulenko <avakulenko@chromium.org> Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org> Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp index 52a131f..a796de2 100644 --- a/buffet/buffet.gyp +++ b/buffet/buffet.gyp
@@ -37,7 +37,6 @@ 'manager.cc', 'mime_utils.cc', 'storage_impls.cc', - 'string_utils.cc', 'url_utils.cc' ], }, @@ -91,7 +90,6 @@ 'http_transport_fake.cc', 'http_utils_unittest.cc', 'mime_utils_unittest.cc', - 'string_utils_unittest.cc', 'url_utils_unittest.cc' ], },
diff --git a/buffet/commands/command_dictionary.cc b/buffet/commands/command_dictionary.cc index 64f47c1..54753ad 100644 --- a/buffet/commands/command_dictionary.cc +++ b/buffet/commands/command_dictionary.cc
@@ -5,10 +5,10 @@ #include "buffet/commands/command_dictionary.h" #include <base/values.h> +#include <chromeos/string_utils.h> #include "buffet/commands/command_definition.h" #include "buffet/commands/schema_constants.h" -#include "buffet/string_utils.h" namespace buffet { @@ -63,8 +63,8 @@ return false; } // Construct the compound command name as "pkg_name.cmd_name". - std::string full_command_name = string_utils::Join('.', package_name, - command_name); + std::string full_command_name = chromeos::string_utils::Join( + '.', package_name, command_name); // Get the "parameters" definition of the command and read it into // an object schema. const base::DictionaryValue* command_schema_def = nullptr; @@ -158,7 +158,7 @@ dict.reset(); return dict; } - auto cmd_name_parts = string_utils::SplitAtFirst(pair.first, '.'); + auto cmd_name_parts = chromeos::string_utils::SplitAtFirst(pair.first, '.'); std::string package_name = cmd_name_parts.first; std::string command_name = cmd_name_parts.second; base::DictionaryValue* package = nullptr;
diff --git a/buffet/commands/command_queue_unittest.cc b/buffet/commands/command_queue_unittest.cc index 2c0e01b..a9d5ad9 100644 --- a/buffet/commands/command_queue_unittest.cc +++ b/buffet/commands/command_queue_unittest.cc
@@ -6,11 +6,11 @@ #include <string> #include <vector> +#include <chromeos/string_utils.h> #include <gtest/gtest.h> #include "buffet/commands/command_dispatch_interface.h" #include "buffet/commands/command_queue.h" -#include "buffet/string_utils.h" namespace { @@ -46,7 +46,7 @@ // Get the comma-separated list of command IDs currently accumulated in the // command queue. std::string GetIDs() const { - using buffet::string_utils::Join; + using chromeos::string_utils::Join; return Join(',', std::vector<std::string>(ids_.begin(), ids_.end())); } @@ -99,7 +99,7 @@ std::string id1 = queue.Add(CreateDummyCommandInstance()); std::string id2 = queue.Add(CreateDummyCommandInstance()); std::set<std::string> ids{id1, id2}; // Make sure they are sorted properly. - std::string expected_set = buffet::string_utils::Join( + std::string expected_set = chromeos::string_utils::Join( ',', std::vector<std::string>(ids.begin(), ids.end())); EXPECT_EQ(expected_set, dispatch.GetIDs()); queue.Remove(id1);
diff --git a/buffet/commands/prop_constraints.cc b/buffet/commands/prop_constraints.cc index 210b254..c9008d2 100644 --- a/buffet/commands/prop_constraints.cc +++ b/buffet/commands/prop_constraints.cc
@@ -5,7 +5,6 @@ #include "buffet/commands/prop_constraints.h" #include "buffet/commands/prop_values.h" #include "buffet/commands/schema_constants.h" -#include "buffet/string_utils.h" namespace buffet { @@ -41,7 +40,8 @@ errors::commands::kOutOfRange, "Value %s is invalid. Expected one of [%s]", val.c_str(), - string_utils::Join(',', values).c_str()); + chromeos::string_utils::Join(',', + values).c_str()); return false; }
diff --git a/buffet/commands/prop_constraints.h b/buffet/commands/prop_constraints.h index 3aa6e8e..e765f44 100644 --- a/buffet/commands/prop_constraints.h +++ b/buffet/commands/prop_constraints.h
@@ -12,11 +12,11 @@ #include <base/basictypes.h> #include <base/values.h> #include <chromeos/error.h> +#include <chromeos/string_utils.h> #include "buffet/commands/prop_values.h" #include "buffet/commands/schema_constants.h" #include "buffet/commands/schema_utils.h" -#include "buffet/string_utils.h" namespace buffet { @@ -138,8 +138,8 @@ T v = value.GetValueAsAny().Get<T>(); if (v < this->limit_.value) return this->ReportErrorLessThan( - error, string_utils::ToString(v), - string_utils::ToString(this->limit_.value)); + error, chromeos::string_utils::ToString(v), + chromeos::string_utils::ToString(this->limit_.value)); return true; } @@ -175,8 +175,8 @@ T v = value.GetValueAsAny().Get<T>(); if (v > this->limit_.value) return this->ReportErrorGreaterThan( - error, string_utils::ToString(v), - string_utils::ToString(this->limit_.value)); + error, chromeos::string_utils::ToString(v), + chromeos::string_utils::ToString(this->limit_.value)); return true; } @@ -282,7 +282,7 @@ // Implementation of Constraint::Validate(). bool Validate(const PropValue& value, chromeos::ErrorPtr* error) const override { - using string_utils::ToString; + using chromeos::string_utils::ToString; T v = value.GetValueAsAny().Get<T>(); for (const auto& item : set_.value) { if (CompareValue(v, item))
diff --git a/buffet/commands/prop_types.cc b/buffet/commands/prop_types.cc index a3d7d6c..505dd5d 100644 --- a/buffet/commands/prop_types.cc +++ b/buffet/commands/prop_types.cc
@@ -11,11 +11,11 @@ #include <base/json/json_writer.h> #include <base/logging.h> #include <base/values.h> +#include <chromeos/string_utils.h> #include "buffet/commands/object_schema.h" #include "buffet/commands/prop_values.h" #include "buffet/commands/schema_constants.h" -#include "buffet/string_utils.h" namespace buffet {
diff --git a/buffet/data_encoding.cc b/buffet/data_encoding.cc index f2de937..8614d2a 100644 --- a/buffet/data_encoding.cc +++ b/buffet/data_encoding.cc
@@ -5,9 +5,8 @@ #include "buffet/data_encoding.h" #include <base/strings/stringprintf.h> -#include <string.h> - -#include "buffet/string_utils.h" +#include <chromeos/string_utils.h> +#include <cstring> namespace { @@ -80,17 +79,17 @@ for (const auto& p : params) { std::string key = UrlEncode(p.first.c_str(), encodeSpaceAsPlus); std::string value = UrlEncode(p.second.c_str(), encodeSpaceAsPlus); - pairs.push_back(string_utils::Join('=', key, value)); + pairs.push_back(chromeos::string_utils::Join('=', key, value)); } - return string_utils::Join('&', pairs); + return chromeos::string_utils::Join('&', pairs); } WebParamList WebParamsDecode(const std::string& data) { WebParamList result; - std::vector<std::string> params = string_utils::Split(data, '&'); + std::vector<std::string> params = chromeos::string_utils::Split(data, '&'); for (const auto& p : params) { - auto pair = string_utils::SplitAtFirst(p, '='); + auto pair = chromeos::string_utils::SplitAtFirst(p, '='); result.emplace_back(UrlDecode(pair.first.c_str()), UrlDecode(pair.second.c_str())); }
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc index 800cfa0..b111de1 100644 --- a/buffet/device_registration_info.cc +++ b/buffet/device_registration_info.cc
@@ -10,6 +10,7 @@ #include <base/json/json_writer.h> #include <base/values.h> +#include <chromeos/string_utils.h> #include "buffet/commands/command_definition.h" #include "buffet/commands/command_manager.h" @@ -19,7 +20,6 @@ #include "buffet/http_utils.h" #include "buffet/mime_utils.h" #include "buffet/storage_impls.h" -#include "buffet/string_utils.h" #include "buffet/url_utils.h" const char buffet::kErrorDomainOAuth2[] = "oauth2"; @@ -67,7 +67,7 @@ const std::string& access_token_type, const std::string& access_token) { std::string authorization = - buffet::string_utils::Join(' ', access_token_type, access_token); + chromeos::string_utils::Join(' ', access_token_type, access_token); return {buffet::http::request_header::kAuthorization, authorization}; }
diff --git a/buffet/http_connection_curl.cc b/buffet/http_connection_curl.cc index 3b2d0a8..86715ab 100644 --- a/buffet/http_connection_curl.cc +++ b/buffet/http_connection_curl.cc
@@ -5,10 +5,10 @@ #include "buffet/http_connection_curl.h" #include <base/logging.h> +#include <chromeos/string_utils.h> #include "buffet/http_request.h" #include "buffet/http_transport_curl.h" -#include "buffet/string_utils.h" namespace buffet { namespace http { @@ -97,7 +97,9 @@ curl_slist* header_list = nullptr; if (!headers_.empty()) { for (auto pair : headers_) { - std::string header = string_utils::Join(": ", pair.first, pair.second); + std::string header = chromeos::string_utils::Join(": ", + pair.first, + pair.second); VLOG(2) << "Request header: " << header; header_list = curl_slist_append(header_list, header.c_str()); } @@ -123,7 +125,7 @@ curl_slist_free_all(header_list); if (ret != CURLE_OK) { chromeos::Error::AddTo(error, http::curl::kErrorDomain, - string_utils::ToString(ret), + chromeos::string_utils::ToString(ret), curl_easy_strerror(ret)); } else { LOG(INFO) << "Response: " << GetResponseStatusCode() << " (" @@ -200,6 +202,7 @@ size_t Connection::header_callback(char* ptr, size_t size, size_t num, void* data) { + using chromeos::string_utils::SplitAtFirst; Connection* me = reinterpret_cast<Connection*>(data); size_t hdr_len = size * num; std::string header(ptr, hdr_len); @@ -213,12 +216,12 @@ if (!me->status_text_set_) { // First header - response code as "HTTP/1.1 200 OK". // Need to extract the OK part - auto pair = string_utils::SplitAtFirst(header, ' '); + auto pair = SplitAtFirst(header, ' '); me->protocol_version_ = pair.first; - me->status_text_ = string_utils::SplitAtFirst(pair.second, ' ').second; + me->status_text_ = SplitAtFirst(pair.second, ' ').second; me->status_text_set_ = true; } else { - auto pair = string_utils::SplitAtFirst(header, ':'); + auto pair = SplitAtFirst(header, ':'); if (!pair.second.empty()) me->headers_.insert(pair); }
diff --git a/buffet/http_connection_fake.cc b/buffet/http_connection_fake.cc index d57786a..1cc58e0 100644 --- a/buffet/http_connection_fake.cc +++ b/buffet/http_connection_fake.cc
@@ -5,10 +5,10 @@ #include "buffet/http_connection_fake.h" #include <base/logging.h> +#include <chromeos/string_utils.h> #include "buffet/http_request.h" #include "buffet/mime_utils.h" -#include "buffet/string_utils.h" namespace buffet { namespace http { @@ -37,8 +37,9 @@ } bool Connection::FinishRequest(chromeos::ErrorPtr* error) { + using chromeos::string_utils::ToString; request_.AddHeaders({{request_header::kContentLength, - string_utils::ToString(request_.GetData().size())}}); + ToString(request_.GetData().size())}}); fake::Transport* transport = static_cast<fake::Transport*>(transport_.get()); CHECK(transport) << "Expecting a fake transport"; auto handler = transport->GetHandler(request_.GetURL(), request_.GetMethod());
diff --git a/buffet/http_request.cc b/buffet/http_request.cc index a9fa439..1c32efe 100644 --- a/buffet/http_request.cc +++ b/buffet/http_request.cc
@@ -5,12 +5,12 @@ #include "buffet/http_request.h" #include <base/logging.h> +#include <chromeos/string_utils.h> #include "buffet/http_connection_curl.h" #include "buffet/http_transport_curl.h" #include "buffet/map_utils.h" #include "buffet/mime_utils.h" -#include "buffet/string_utils.h" namespace buffet { namespace http { @@ -195,11 +195,11 @@ p.second != range_value_omitted) { std::string range; if (p.first != range_value_omitted) { - range = string_utils::ToString(p.first); + range = chromeos::string_utils::ToString(p.first); } range += '-'; if (p.second != range_value_omitted) { - range += string_utils::ToString(p.second); + range += chromeos::string_utils::ToString(p.second); } ranges.push_back(range); } @@ -207,7 +207,8 @@ } if (!ranges.empty()) headers.emplace_back(request_header::kRange, - "bytes=" + string_utils::Join(',', ranges)); + "bytes=" + + chromeos::string_utils::Join(',', ranges)); headers.emplace_back(request_header::kAccept, GetAccept()); if (method_ != request_type::kGet && method_ != request_type::kHead) {
diff --git a/buffet/http_transport_fake.cc b/buffet/http_transport_fake.cc index d4b7f44..7df8567 100644 --- a/buffet/http_transport_fake.cc +++ b/buffet/http_transport_fake.cc
@@ -10,11 +10,11 @@ #include <base/json/json_writer.h> #include <base/logging.h> #include <chromeos/bind_lambda.h> +#include <chromeos/string_utils.h> #include "buffet/http_connection_fake.h" #include "buffet/http_request.h" #include "buffet/mime_utils.h" -#include "buffet/string_utils.h" #include "buffet/url_utils.h" namespace buffet { @@ -171,7 +171,8 @@ status_code_ = status_code; AddData(data, data_size); AddHeaders({ - {response_header::kContentLength, string_utils::ToString(data_size)}, + {response_header::kContentLength, + chromeos::string_utils::ToString(data_size)}, {response_header::kContentType, mime_type} }); }
diff --git a/buffet/http_utils_unittest.cc b/buffet/http_utils_unittest.cc index 22c2209..24c5dcd 100644 --- a/buffet/http_utils_unittest.cc +++ b/buffet/http_utils_unittest.cc
@@ -7,12 +7,12 @@ #include <base/values.h> #include <chromeos/bind_lambda.h> +#include <chromeos/string_utils.h> #include <gtest/gtest.h> #include "buffet/http_transport_fake.h" #include "buffet/http_utils.h" #include "buffet/mime_utils.h" -#include "buffet/string_utils.h" #include "buffet/url_utils.h" using namespace buffet; // NOLINT(build/namespaces) @@ -311,7 +311,7 @@ // Test valid JSON responses (with success or error codes). for (auto item : {"200;data", "400;wrong", "500;Internal Server error"}) { - auto pair = string_utils::SplitAtFirst(item, ';'); + auto pair = chromeos::string_utils::SplitAtFirst(item, ';'); auto response = http::PostFormData(kFakeUrl, { {"code", pair.first}, {"value", pair.second}, @@ -321,7 +321,7 @@ EXPECT_NE(nullptr, json.get()); std::string value; EXPECT_TRUE(json->GetString("data", &value)); - EXPECT_EQ(pair.first, string_utils::ToString(code)); + EXPECT_EQ(pair.first, chromeos::string_utils::ToString(code)); EXPECT_EQ(pair.second, value); }
diff --git a/buffet/mime_utils.cc b/buffet/mime_utils.cc index dc57497..5747405 100644 --- a/buffet/mime_utils.cc +++ b/buffet/mime_utils.cc
@@ -6,8 +6,7 @@ #include <algorithm> #include <base/strings/string_util.h> - -#include "buffet/string_utils.h" +#include <chromeos/string_utils.h> namespace buffet { @@ -65,7 +64,8 @@ bool mime::Split(const std::string& mime_string, std::string* type, std::string* subtype, mime::Parameters* parameters) { - std::vector<std::string> parts = string_utils::Split(mime_string, ';'); + std::vector<std::string> parts = chromeos::string_utils::Split(mime_string, + ';'); if (parts.empty()) return false; @@ -76,7 +76,7 @@ parameters->clear(); parameters->reserve(parts.size() - 1); for (size_t i = 1; i < parts.size(); i++) { - auto pair = string_utils::SplitAtFirst(parts[i], '='); + auto pair = chromeos::string_utils::SplitAtFirst(parts[i], '='); pair.second = DecodeParam(pair.second); parameters->push_back(pair); } @@ -87,7 +87,7 @@ bool mime::Split(const std::string& mime_string, std::string* type, std::string* subtype) { std::string mime = mime::RemoveParameters(mime_string); - auto types = string_utils::SplitAtFirst(mime, '/'); + auto types = chromeos::string_utils::SplitAtFirst(mime, '/'); if (type) *type = types.first; @@ -101,22 +101,23 @@ std::string mime::Combine(const std::string& type, const std::string& subtype, const mime::Parameters& parameters) { std::vector<std::string> parts; - parts.push_back(string_utils::Join('/', type, subtype)); + parts.push_back(chromeos::string_utils::Join('/', type, subtype)); for (const auto& pair : parameters) { - parts.push_back(string_utils::Join('=', pair.first, - EncodeParam(pair.second))); + parts.push_back(chromeos::string_utils::Join('=', + pair.first, + EncodeParam(pair.second))); } - return string_utils::Join("; ", parts); + return chromeos::string_utils::Join("; ", parts); } std::string mime::GetType(const std::string& mime_string) { std::string mime = mime::RemoveParameters(mime_string); - return string_utils::SplitAtFirst(mime, '/').first; + return chromeos::string_utils::SplitAtFirst(mime, '/').first; } std::string mime::GetSubtype(const std::string& mime_string) { std::string mime = mime::RemoveParameters(mime_string); - return string_utils::SplitAtFirst(mime, '/').second; + return chromeos::string_utils::SplitAtFirst(mime, '/').second; } mime::Parameters mime::GetParameters(const std::string& mime_string) { @@ -131,7 +132,7 @@ } std::string mime::RemoveParameters(const std::string& mime_string) { - return string_utils::SplitAtFirst(mime_string, ';').first; + return chromeos::string_utils::SplitAtFirst(mime_string, ';').first; } std::string mime::AppendParameter(const std::string& mime_string, @@ -139,7 +140,7 @@ const std::string& paramValue) { std::string mime(mime_string); mime += "; "; - mime += string_utils::Join('=', paramName, EncodeParam(paramValue)); + mime += chromeos::string_utils::Join('=', paramName, EncodeParam(paramValue)); return mime; }
diff --git a/buffet/string_utils.cc b/buffet/string_utils.cc deleted file mode 100644 index 8916e96..0000000 --- a/buffet/string_utils.cc +++ /dev/null
@@ -1,99 +0,0 @@ -// Copyright 2014 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "buffet/string_utils.h" - -#include <algorithm> -#include <string.h> -#include <utility> - -#include <base/strings/string_util.h> -#include <base/strings/stringprintf.h> - -namespace buffet { -namespace string_utils { - -std::vector<std::string> Split(const std::string& str, - char delimiter, - bool trim_whitespaces, - bool purge_empty_strings) { - std::vector<std::string> tokens; - if (delimiter == 0) - return tokens; - - const char* sz = str.c_str(); - if (sz) { - const char* szNext = strchr(sz, delimiter); - while (szNext) { - if (szNext != sz || !purge_empty_strings) - tokens.emplace_back(sz, szNext - sz); - sz = szNext + 1; - szNext = strchr(sz, delimiter); - } - if (*sz != 0 || !purge_empty_strings) - tokens.emplace_back(sz); - } - - if (trim_whitespaces) { - std::for_each(tokens.begin(), tokens.end(), - [](std::string& str) { - base::TrimWhitespaceASCII(str, base::TRIM_ALL, &str); }); - } - - return tokens; -} - -std::pair<std::string, std::string> SplitAtFirst(const std::string& str, - char delimiter, - bool trim_whitespaces) { - std::pair<std::string, std::string> pair; - if (delimiter == 0) - return pair; - - const char* sz = str.c_str(); - const char* szNext = strchr(sz, delimiter); - if (szNext) { - pair.first = std::string(sz, szNext); - pair.second = std::string(szNext + 1); - } else { - pair.first = str; - } - - if (trim_whitespaces) { - base::TrimWhitespaceASCII(pair.first, base::TRIM_ALL, &pair.first); - base::TrimWhitespaceASCII(pair.second, base::TRIM_ALL, &pair.second); - } - - return pair; -} - -std::string Join(char delimiter, const std::vector<std::string>& strings) { - return JoinString(strings, delimiter); -} - -std::string Join(const std::string& delimiter, - const std::vector<std::string>& strings) { - return JoinString(strings, delimiter); -} - -std::string Join(char delimiter, - const std::string& str1, const std::string& str2) { - return str1 + delimiter + str2; -} - -std::string Join(const std::string& delimiter, - const std::string& str1, const std::string& str2) { - return str1 + delimiter + str2; -} - -std::string ToString(double value) { - return base::StringPrintf("%g", value); -} - -std::string ToString(bool value) { - return value ? "true" : "false"; -} - -} // namespace string_utils -} // namespace buffet
diff --git a/buffet/string_utils.h b/buffet/string_utils.h deleted file mode 100644 index e29675b..0000000 --- a/buffet/string_utils.h +++ /dev/null
@@ -1,56 +0,0 @@ -// Copyright 2014 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BUFFET_STRING_UTILS_H_ -#define BUFFET_STRING_UTILS_H_ - -#include <string> -#include <utility> -#include <vector> - -namespace buffet { -namespace string_utils { - -// Treats the string as a delimited list of substrings and returns the array -// of original elements of the list. -// By default, empty elements from the original string are omitted and -// each element has all whitespaces trimmed off. -std::vector<std::string> Split(const std::string& str, - char delimiter, - bool trim_whitespaces = true, - bool purge_empty_strings = true); - -// Splits the string into two pieces at the first position of the specified -// delimiter. By default, each part has all whitespaces trimmed off. -std::pair<std::string, std::string> SplitAtFirst(const std::string& str, - char delimiter, - bool trim_whitespaces = true); - -// Joins an array of strings into a single string separated by |delimiter|. -std::string Join(char delimiter, const std::vector<std::string>& strings); -std::string Join(const std::string& delimiter, - const std::vector<std::string>& strings); -std::string Join(char delimiter, - const std::string& str1, const std::string& str2); -std::string Join(const std::string& delimiter, - const std::string& str1, const std::string& str2); - -// string_utils::ToString() is a helper function to convert any scalar type -// to a string. In most cases, it redirects the call to std::to_string with -// two exceptions: for std::string itself and for double and bool. -template<typename T> -inline std::string ToString(T value) { return std::to_string(value); } -// Having the following overload is handy for templates where the type -// of template parameter isn't known and could be a string itself. -inline std::string ToString(std::string value) { return value; } -// We overload this for double because std::to_string(double) uses %f to -// format the value and I would like to use a shorter %g format instead. -std::string ToString(double value); -// And the bool to be converted as true/false instead of 1/0. -std::string ToString(bool value); - -} // namespace string_utils -} // namespace buffet - -#endif // BUFFET_STRING_UTILS_H_
diff --git a/buffet/string_utils_unittest.cc b/buffet/string_utils_unittest.cc deleted file mode 100644 index 4a021f4..0000000 --- a/buffet/string_utils_unittest.cc +++ /dev/null
@@ -1,109 +0,0 @@ -// Copyright (c) 2014 The Chromium OS Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "buffet/string_utils.h" - -#include <gtest/gtest.h> - -using namespace buffet; // NOLINT(build/namespaces) - -TEST(StringUtils, Split) { - std::vector<std::string> parts; - - parts = string_utils::Split(",a,bc , d,,e,", ',', true, true); - EXPECT_EQ(4, parts.size()); - EXPECT_EQ("a", parts[0]); - EXPECT_EQ("bc", parts[1]); - EXPECT_EQ("d", parts[2]); - EXPECT_EQ("e", parts[3]); - - parts = string_utils::Split(",a,bc , d,,e,", ',', false, true); - EXPECT_EQ(4, parts.size()); - EXPECT_EQ("a", parts[0]); - EXPECT_EQ("bc ", parts[1]); - EXPECT_EQ(" d", parts[2]); - EXPECT_EQ("e", parts[3]); - - parts = string_utils::Split(",a,bc , d,,e,", ',', true, false); - EXPECT_EQ(7, parts.size()); - EXPECT_EQ("", parts[0]); - EXPECT_EQ("a", parts[1]); - EXPECT_EQ("bc", parts[2]); - EXPECT_EQ("d", parts[3]); - EXPECT_EQ("", parts[4]); - EXPECT_EQ("e", parts[5]); - EXPECT_EQ("", parts[6]); - - parts = string_utils::Split(",a,bc , d,,e,", ',', false, false); - EXPECT_EQ(7, parts.size()); - EXPECT_EQ("", parts[0]); - EXPECT_EQ("a", parts[1]); - EXPECT_EQ("bc ", parts[2]); - EXPECT_EQ(" d", parts[3]); - EXPECT_EQ("", parts[4]); - EXPECT_EQ("e", parts[5]); - EXPECT_EQ("", parts[6]); -} - -TEST(StringUtils, SplitAtFirst) { - std::pair<std::string, std::string> pair; - - pair = string_utils::SplitAtFirst(" 123 : 4 : 56 : 789 ", ':', true); - EXPECT_EQ("123", pair.first); - EXPECT_EQ("4 : 56 : 789", pair.second); - - pair = string_utils::SplitAtFirst(" 123 : 4 : 56 : 789 ", ':', false); - EXPECT_EQ(" 123 ", pair.first); - EXPECT_EQ(" 4 : 56 : 789 ", pair.second); - - pair = string_utils::SplitAtFirst("", '='); - EXPECT_EQ("", pair.first); - EXPECT_EQ("", pair.second); - - pair = string_utils::SplitAtFirst("=", '='); - EXPECT_EQ("", pair.first); - EXPECT_EQ("", pair.second); - - pair = string_utils::SplitAtFirst("a=", '='); - EXPECT_EQ("a", pair.first); - EXPECT_EQ("", pair.second); - - pair = string_utils::SplitAtFirst("abc=", '='); - EXPECT_EQ("abc", pair.first); - EXPECT_EQ("", pair.second); - - pair = string_utils::SplitAtFirst("=a", '='); - EXPECT_EQ("", pair.first); - EXPECT_EQ("a", pair.second); - - pair = string_utils::SplitAtFirst("=abc=", '='); - EXPECT_EQ("", pair.first); - EXPECT_EQ("abc=", pair.second); - - pair = string_utils::SplitAtFirst("abc", '='); - EXPECT_EQ("abc", pair.first); - EXPECT_EQ("", pair.second); -} - -TEST(StringUtils, Join_Char) { - EXPECT_EQ("", string_utils::Join(',', {})); - EXPECT_EQ("abc", string_utils::Join(',', {"abc"})); - EXPECT_EQ("abc,defg", string_utils::Join(',', {"abc", "defg"})); - EXPECT_EQ("1:2:3", string_utils::Join(':', {"1", "2", "3"})); - EXPECT_EQ("192.168.0.1", string_utils::Join('.', {"192", "168", "0", "1"})); - EXPECT_EQ("ff02::1", string_utils::Join(':', {"ff02", "", "1"})); -} - -TEST(StringUtils, Join_String) { - EXPECT_EQ("", string_utils::Join(",", {})); - EXPECT_EQ("abc", string_utils::Join(",", {"abc"})); - EXPECT_EQ("abc,defg", string_utils::Join(",", {"abc", "defg"})); - EXPECT_EQ("1 : 2 : 3", string_utils::Join(" : ", {"1", "2", "3"})); - EXPECT_EQ("123", string_utils::Join("", {"1", "2", "3"})); -} - -TEST(StringUtils, Join_Pair) { - EXPECT_EQ("ab,cd", string_utils::Join(',', "ab", "cd")); - EXPECT_EQ("key = value", string_utils::Join(" = ", "key", "value")); -}