blob: c5d06eb62b10c4d1682f210a79495927a5afd2f5 [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>
9#include <base/strings/string_util.h>
10
11namespace chromeos {
12namespace string_utils {
13
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070014std::vector<std::string> Split(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070015 char delimiter,
16 bool trim_whitespaces,
17 bool purge_empty_strings) {
18 std::vector<std::string> tokens;
19 if (delimiter == 0)
20 return tokens;
21
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070022 const char* sz = str.c_str();
Chris Sosa45d9f102014-03-24 11:18:54 -070023 if (sz) {
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070024 const char* szNext = strchr(sz, delimiter);
Chris Sosa45d9f102014-03-24 11:18:54 -070025 while (szNext) {
26 if (szNext != sz || !purge_empty_strings)
27 tokens.emplace_back(sz, szNext - sz);
28 sz = szNext + 1;
29 szNext = strchr(sz, delimiter);
30 }
31 if (*sz != 0 || !purge_empty_strings)
32 tokens.emplace_back(sz);
33 }
34
35 if (trim_whitespaces) {
36 std::for_each(tokens.begin(), tokens.end(), [](std::string& str) {
37 TrimWhitespaceASCII(str, TRIM_ALL, &str); });
38 }
39
40 return tokens;
41}
42
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070043std::pair<std::string, std::string> SplitAtFirst(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070044 char delimiter,
45 bool trim_whitespaces) {
46 std::pair<std::string, std::string> pair;
47 if (delimiter == 0)
48 return pair;
49
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070050 const char* sz = str.c_str();
51 const char* szNext = strchr(sz, delimiter);
Chris Sosa45d9f102014-03-24 11:18:54 -070052 if (szNext) {
53 pair.first = std::string(sz, szNext);
54 pair.second = std::string(szNext + 1);
55 } else {
56 pair.first = str;
57 }
58
59 if (trim_whitespaces) {
60 TrimWhitespaceASCII(pair.first, TRIM_ALL, &pair.first);
61 TrimWhitespaceASCII(pair.second, TRIM_ALL, &pair.second);
62 }
63
64 return pair;
65}
66
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070067std::string Join(char delimiter, const std::vector<std::string>& strings) {
Chris Sosa45d9f102014-03-24 11:18:54 -070068 return JoinString(strings, delimiter);
69}
70
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070071std::string Join(const std::string& delimiter,
72 const std::vector<std::string>& strings) {
Chris Sosa45d9f102014-03-24 11:18:54 -070073 return JoinString(strings, delimiter);
74}
75
76std::string Join(char delimiter,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070077 const std::string& str1, const std::string& str2) {
Chris Sosa45d9f102014-03-24 11:18:54 -070078 return str1 + delimiter + str2;
79}
80
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070081std::string Join(const std::string& delimiter,
82 const std::string& str1, const std::string& str2) {
Chris Sosa45d9f102014-03-24 11:18:54 -070083 return str1 + delimiter + str2;
84}
85
86} // namespace string_utils
87} // namespace chromeos