blob: 8916e9635373c8cfb8f05eead531ab6d7a6b2ba0 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -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
5#include "buffet/string_utils.h"
6
7#include <algorithm>
8#include <string.h>
Alex Vakulenkoaf23b322014-05-08 16:25:45 -07009#include <utility>
10
Chris Sosa45d9f102014-03-24 11:18:54 -070011#include <base/strings/string_util.h>
Alex Vakulenko96c84d32014-06-06 11:07:32 -070012#include <base/strings/stringprintf.h>
Chris Sosa45d9f102014-03-24 11:18:54 -070013
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070014namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070015namespace string_utils {
16
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070017std::vector<std::string> Split(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070018 char delimiter,
19 bool trim_whitespaces,
20 bool purge_empty_strings) {
21 std::vector<std::string> tokens;
22 if (delimiter == 0)
23 return tokens;
24
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070025 const char* sz = str.c_str();
Chris Sosa45d9f102014-03-24 11:18:54 -070026 if (sz) {
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070027 const char* szNext = strchr(sz, delimiter);
Chris Sosa45d9f102014-03-24 11:18:54 -070028 while (szNext) {
29 if (szNext != sz || !purge_empty_strings)
30 tokens.emplace_back(sz, szNext - sz);
31 sz = szNext + 1;
32 szNext = strchr(sz, delimiter);
33 }
34 if (*sz != 0 || !purge_empty_strings)
35 tokens.emplace_back(sz);
36 }
37
38 if (trim_whitespaces) {
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070039 std::for_each(tokens.begin(), tokens.end(),
Alex Vakulenko96c84d32014-06-06 11:07:32 -070040 [](std::string& str) {
Ben Chand2070a72014-05-19 21:26:41 -070041 base::TrimWhitespaceASCII(str, base::TRIM_ALL, &str); });
Chris Sosa45d9f102014-03-24 11:18:54 -070042 }
43
44 return tokens;
45}
46
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070047std::pair<std::string, std::string> SplitAtFirst(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070048 char delimiter,
49 bool trim_whitespaces) {
50 std::pair<std::string, std::string> pair;
51 if (delimiter == 0)
52 return pair;
53
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070054 const char* sz = str.c_str();
55 const char* szNext = strchr(sz, delimiter);
Chris Sosa45d9f102014-03-24 11:18:54 -070056 if (szNext) {
57 pair.first = std::string(sz, szNext);
58 pair.second = std::string(szNext + 1);
59 } else {
60 pair.first = str;
61 }
62
63 if (trim_whitespaces) {
Ben Chand2070a72014-05-19 21:26:41 -070064 base::TrimWhitespaceASCII(pair.first, base::TRIM_ALL, &pair.first);
65 base::TrimWhitespaceASCII(pair.second, base::TRIM_ALL, &pair.second);
Chris Sosa45d9f102014-03-24 11:18:54 -070066 }
67
68 return pair;
69}
70
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070071std::string Join(char delimiter, const std::vector<std::string>& strings) {
Chris Sosa45d9f102014-03-24 11:18:54 -070072 return JoinString(strings, delimiter);
73}
74
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070075std::string Join(const std::string& delimiter,
76 const std::vector<std::string>& strings) {
Chris Sosa45d9f102014-03-24 11:18:54 -070077 return JoinString(strings, delimiter);
78}
79
80std::string Join(char delimiter,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070081 const std::string& str1, const std::string& str2) {
Chris Sosa45d9f102014-03-24 11:18:54 -070082 return str1 + delimiter + str2;
83}
84
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070085std::string Join(const std::string& delimiter,
86 const std::string& str1, const std::string& str2) {
Chris Sosa45d9f102014-03-24 11:18:54 -070087 return str1 + delimiter + str2;
88}
89
Alex Vakulenko96c84d32014-06-06 11:07:32 -070090std::string ToString(double value) {
91 return base::StringPrintf("%g", value);
92}
93
94std::string ToString(bool value) {
95 return value ? "true" : "false";
96}
97
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070098} // namespace string_utils
99} // namespace buffet