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 | #ifndef BUFFET_STRING_UTILS_H_ |
| 6 | #define BUFFET_STRING_UTILS_H_ |
| 7 | |
| 8 | #include <string> |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 9 | #include <utility> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 12 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 13 | namespace string_utils { |
| 14 | |
| 15 | // Treats the string as a delimited list of substrings and returns the array |
| 16 | // of original elements of the list. |
| 17 | // By default, empty elements from the original string are omitted and |
| 18 | // each element has all whitespaces trimmed off. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 19 | std::vector<std::string> Split(const std::string& str, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 20 | char delimiter, |
| 21 | bool trim_whitespaces = true, |
| 22 | bool purge_empty_strings = true); |
| 23 | |
| 24 | // Splits the string into two pieces at the first position of the specified |
| 25 | // delimiter. By default, each part has all whitespaces trimmed off. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 26 | std::pair<std::string, std::string> SplitAtFirst(const std::string& str, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 27 | char delimiter, |
| 28 | bool trim_whitespaces = true); |
| 29 | |
| 30 | // Joins an array of strings into a single string separated by |delimiter|. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 31 | std::string Join(char delimiter, const std::vector<std::string>& strings); |
| 32 | std::string Join(const std::string& delimiter, |
| 33 | const std::vector<std::string>& strings); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 34 | std::string Join(char delimiter, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 35 | const std::string& str1, const std::string& str2); |
| 36 | std::string Join(const std::string& delimiter, |
| 37 | const std::string& str1, const std::string& str2); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 38 | |
Alex Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 39 | // string_utils::ToString() is a helper function to convert any scalar type |
| 40 | // to a string. In most cases, it redirects the call to std::to_string with |
| 41 | // two exceptions: for std::string itself and for double and bool. |
| 42 | template<typename T> |
| 43 | inline std::string ToString(T value) { return std::to_string(value); } |
| 44 | // Having the following overload is handy for templates where the type |
| 45 | // of template parameter isn't known and could be a string itself. |
| 46 | inline std::string ToString(std::string value) { return value; } |
| 47 | // We overload this for double because std::to_string(double) uses %f to |
| 48 | // format the value and I would like to use a shorter %g format instead. |
| 49 | std::string ToString(double value); |
| 50 | // And the bool to be converted as true/false instead of 1/0. |
| 51 | std::string ToString(bool value); |
| 52 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 53 | } // namespace string_utils |
| 54 | } // namespace buffet |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 55 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 56 | #endif // BUFFET_STRING_UTILS_H_ |