blob: 8279a0efdb51c0065ecbec06e07e49090cc30565 [file] [log] [blame]
Alex Vakulenko6c0515d2014-08-20 15:38:07 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -07005#include <brillo/strings/string_utils.h>
Alex Vakulenko6c0515d2014-08-20 15:38:07 -07006
7#include <algorithm>
8#include <string.h>
9#include <utility>
10
11#include <base/strings/string_util.h>
12#include <base/strings/stringprintf.h>
13
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070014namespace brillo {
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070015namespace string_utils {
16
17std::vector<std::string> Split(const std::string& str,
Vitaly Buka852ff002015-03-10 19:33:33 -070018 const std::string& delimiter,
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070019 bool trim_whitespaces,
20 bool purge_empty_strings) {
21 std::vector<std::string> tokens;
Vitaly Buka852ff002015-03-10 19:33:33 -070022 if (str.empty())
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070023 return tokens;
24
Vitaly Buka852ff002015-03-10 19:33:33 -070025 for (std::string::size_type i = 0;;) {
26 const std::string::size_type pos =
27 delimiter.empty() ? (i + 1) : str.find(delimiter, i);
28 std::string tmp_str{str.substr(i, pos - i)};
29 if (trim_whitespaces)
30 base::TrimWhitespaceASCII(tmp_str, base::TRIM_ALL, &tmp_str);
31 if (!tmp_str.empty() || !purge_empty_strings)
32 tokens.emplace_back(std::move(tmp_str));
33 if (pos >= str.size())
34 break;
35 i = pos + delimiter.size();
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070036 }
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070037 return tokens;
38}
39
Alex Vakulenko097cab62014-09-24 08:59:14 -070040bool SplitAtFirst(const std::string& str,
Vitaly Buka852ff002015-03-10 19:33:33 -070041 const std::string& delimiter,
Alex Vakulenko097cab62014-09-24 08:59:14 -070042 std::string* left_part,
43 std::string* right_part,
44 bool trim_whitespaces) {
Alex Vakulenko097cab62014-09-24 08:59:14 -070045 bool delimiter_found = false;
Vitaly Buka852ff002015-03-10 19:33:33 -070046 std::string::size_type pos = str.find(delimiter);
47 if (pos != std::string::npos) {
48 *left_part = str.substr(0, pos);
49 *right_part = str.substr(pos + delimiter.size());
Alex Vakulenko097cab62014-09-24 08:59:14 -070050 delimiter_found = true;
51 } else {
52 *left_part = str;
Vitaly Buka852ff002015-03-10 19:33:33 -070053 right_part->clear();
Alex Vakulenko097cab62014-09-24 08:59:14 -070054 }
55
56 if (trim_whitespaces) {
57 base::TrimWhitespaceASCII(*left_part, base::TRIM_ALL, left_part);
58 base::TrimWhitespaceASCII(*right_part, base::TRIM_ALL, right_part);
59 }
60
61 return delimiter_found;
62}
63
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070064std::pair<std::string, std::string> SplitAtFirst(const std::string& str,
Vitaly Buka852ff002015-03-10 19:33:33 -070065 const std::string& delimiter,
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070066 bool trim_whitespaces) {
67 std::pair<std::string, std::string> pair;
Alex Vakulenko097cab62014-09-24 08:59:14 -070068 SplitAtFirst(str, delimiter, &pair.first, &pair.second, trim_whitespaces);
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070069 return pair;
70}
71
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070072std::string ToString(double value) {
73 return base::StringPrintf("%g", value);
74}
75
76std::string ToString(bool value) {
77 return value ? "true" : "false";
78}
79
Alex Vakulenkoc0f562c2015-02-03 07:38:51 -080080std::string GetBytesAsString(const std::vector<uint8_t>& buffer) {
Vitaly Buka3b206ce2015-03-10 13:45:04 -070081 return std::string(buffer.begin(), buffer.end());
Alex Vakulenkoc0f562c2015-02-03 07:38:51 -080082}
83
84std::vector<uint8_t> GetStringAsBytes(const std::string& str) {
Vitaly Buka3b206ce2015-03-10 13:45:04 -070085 return std::vector<uint8_t>(str.begin(), str.end());
Alex Vakulenkoc0f562c2015-02-03 07:38:51 -080086}
87
Alex Vakulenko6c0515d2014-08-20 15:38:07 -070088} // namespace string_utils
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070089} // namespace brillo