Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // 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 | |
| 11 | namespace chromeos { |
| 12 | namespace string_utils { |
| 13 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 14 | std::vector<std::string> Split(const std::string& str, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 15 | 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 22 | const char* sz = str.c_str(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 23 | if (sz) { |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 24 | const char* szNext = strchr(sz, delimiter); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 25 | 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 43 | std::pair<std::string, std::string> SplitAtFirst(const std::string& str, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 44 | char delimiter, |
| 45 | bool trim_whitespaces) { |
| 46 | std::pair<std::string, std::string> pair; |
| 47 | if (delimiter == 0) |
| 48 | return pair; |
| 49 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 50 | const char* sz = str.c_str(); |
| 51 | const char* szNext = strchr(sz, delimiter); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 52 | 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 67 | std::string Join(char delimiter, const std::vector<std::string>& strings) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 68 | return JoinString(strings, delimiter); |
| 69 | } |
| 70 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 71 | std::string Join(const std::string& delimiter, |
| 72 | const std::vector<std::string>& strings) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 73 | return JoinString(strings, delimiter); |
| 74 | } |
| 75 | |
| 76 | std::string Join(char delimiter, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 77 | const std::string& str1, const std::string& str2) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 78 | return str1 + delimiter + str2; |
| 79 | } |
| 80 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 81 | std::string Join(const std::string& delimiter, |
| 82 | const std::string& str1, const std::string& str2) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 83 | return str1 + delimiter + str2; |
| 84 | } |
| 85 | |
| 86 | } // namespace string_utils |
| 87 | } // namespace chromeos |