xref: /aosp_15_r20/external/libbrillo/brillo/strings/string_utils.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #include <brillo/strings/string_utils.h>
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <algorithm>
8*1a96fba6SXin Li #include <string.h>
9*1a96fba6SXin Li #include <utility>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <base/strings/string_util.h>
12*1a96fba6SXin Li #include <base/strings/stringprintf.h>
13*1a96fba6SXin Li 
14*1a96fba6SXin Li namespace brillo {
15*1a96fba6SXin Li namespace string_utils {
16*1a96fba6SXin Li 
Split(const std::string & str,const std::string & delimiter,bool trim_whitespaces,bool purge_empty_strings)17*1a96fba6SXin Li std::vector<std::string> Split(const std::string& str,
18*1a96fba6SXin Li                                const std::string& delimiter,
19*1a96fba6SXin Li                                bool trim_whitespaces,
20*1a96fba6SXin Li                                bool purge_empty_strings) {
21*1a96fba6SXin Li   std::vector<std::string> tokens;
22*1a96fba6SXin Li   if (str.empty())
23*1a96fba6SXin Li     return tokens;
24*1a96fba6SXin Li 
25*1a96fba6SXin Li   for (std::string::size_type i = 0;;) {
26*1a96fba6SXin Li     const std::string::size_type pos =
27*1a96fba6SXin Li         delimiter.empty() ? (i + 1) : str.find(delimiter, i);
28*1a96fba6SXin Li     std::string tmp_str{str.substr(i, pos - i)};
29*1a96fba6SXin Li     if (trim_whitespaces)
30*1a96fba6SXin Li       base::TrimWhitespaceASCII(tmp_str, base::TRIM_ALL, &tmp_str);
31*1a96fba6SXin Li     if (!tmp_str.empty() || !purge_empty_strings)
32*1a96fba6SXin Li       tokens.emplace_back(std::move(tmp_str));
33*1a96fba6SXin Li     if (pos >= str.size())
34*1a96fba6SXin Li       break;
35*1a96fba6SXin Li     i = pos + delimiter.size();
36*1a96fba6SXin Li   }
37*1a96fba6SXin Li   return tokens;
38*1a96fba6SXin Li }
39*1a96fba6SXin Li 
SplitAtFirst(const std::string & str,const std::string & delimiter,std::string * left_part,std::string * right_part,bool trim_whitespaces)40*1a96fba6SXin Li bool SplitAtFirst(const std::string& str,
41*1a96fba6SXin Li                   const std::string& delimiter,
42*1a96fba6SXin Li                   std::string* left_part,
43*1a96fba6SXin Li                   std::string* right_part,
44*1a96fba6SXin Li                   bool trim_whitespaces) {
45*1a96fba6SXin Li   bool delimiter_found = false;
46*1a96fba6SXin Li   std::string::size_type pos = str.find(delimiter);
47*1a96fba6SXin Li   if (pos != std::string::npos) {
48*1a96fba6SXin Li     *left_part = str.substr(0, pos);
49*1a96fba6SXin Li     *right_part = str.substr(pos + delimiter.size());
50*1a96fba6SXin Li     delimiter_found = true;
51*1a96fba6SXin Li   } else {
52*1a96fba6SXin Li     *left_part = str;
53*1a96fba6SXin Li     right_part->clear();
54*1a96fba6SXin Li   }
55*1a96fba6SXin Li 
56*1a96fba6SXin Li   if (trim_whitespaces) {
57*1a96fba6SXin Li     base::TrimWhitespaceASCII(*left_part, base::TRIM_ALL, left_part);
58*1a96fba6SXin Li     base::TrimWhitespaceASCII(*right_part, base::TRIM_ALL, right_part);
59*1a96fba6SXin Li   }
60*1a96fba6SXin Li 
61*1a96fba6SXin Li   return delimiter_found;
62*1a96fba6SXin Li }
63*1a96fba6SXin Li 
SplitAtFirst(const std::string & str,const std::string & delimiter,bool trim_whitespaces)64*1a96fba6SXin Li std::pair<std::string, std::string> SplitAtFirst(const std::string& str,
65*1a96fba6SXin Li                                                  const std::string& delimiter,
66*1a96fba6SXin Li                                                  bool trim_whitespaces) {
67*1a96fba6SXin Li   std::pair<std::string, std::string> pair;
68*1a96fba6SXin Li   SplitAtFirst(str, delimiter, &pair.first, &pair.second, trim_whitespaces);
69*1a96fba6SXin Li   return pair;
70*1a96fba6SXin Li }
71*1a96fba6SXin Li 
ToString(double value)72*1a96fba6SXin Li std::string ToString(double value) {
73*1a96fba6SXin Li   return base::StringPrintf("%g", value);
74*1a96fba6SXin Li }
75*1a96fba6SXin Li 
ToString(bool value)76*1a96fba6SXin Li std::string ToString(bool value) {
77*1a96fba6SXin Li   return value ? "true" : "false";
78*1a96fba6SXin Li }
79*1a96fba6SXin Li 
GetBytesAsString(const std::vector<uint8_t> & buffer)80*1a96fba6SXin Li std::string GetBytesAsString(const std::vector<uint8_t>& buffer) {
81*1a96fba6SXin Li   return std::string(buffer.begin(), buffer.end());
82*1a96fba6SXin Li }
83*1a96fba6SXin Li 
GetStringAsBytes(const std::string & str)84*1a96fba6SXin Li std::vector<uint8_t> GetStringAsBytes(const std::string& str) {
85*1a96fba6SXin Li   return std::vector<uint8_t>(str.begin(), str.end());
86*1a96fba6SXin Li }
87*1a96fba6SXin Li 
88*1a96fba6SXin Li }  // namespace string_utils
89*1a96fba6SXin Li }  // namespace brillo
90