blob: e29675b4730064969943d3633bbe03032c2f21c4 [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#ifndef BUFFET_STRING_UTILS_H_
6#define BUFFET_STRING_UTILS_H_
7
8#include <string>
Alex Vakulenkoaf23b322014-05-08 16:25:45 -07009#include <utility>
Chris Sosa45d9f102014-03-24 11:18:54 -070010#include <vector>
11
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070012namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070013namespace 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 Vakulenkob8ba5952014-04-17 11:35:56 -070019std::vector<std::string> Split(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070020 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 Vakulenkob8ba5952014-04-17 11:35:56 -070026std::pair<std::string, std::string> SplitAtFirst(const std::string& str,
Chris Sosa45d9f102014-03-24 11:18:54 -070027 char delimiter,
28 bool trim_whitespaces = true);
29
30// Joins an array of strings into a single string separated by |delimiter|.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070031std::string Join(char delimiter, const std::vector<std::string>& strings);
32std::string Join(const std::string& delimiter,
33 const std::vector<std::string>& strings);
Chris Sosa45d9f102014-03-24 11:18:54 -070034std::string Join(char delimiter,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070035 const std::string& str1, const std::string& str2);
36std::string Join(const std::string& delimiter,
37 const std::string& str1, const std::string& str2);
Chris Sosa45d9f102014-03-24 11:18:54 -070038
Alex Vakulenko96c84d32014-06-06 11:07:32 -070039// 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.
42template<typename T>
43inline 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.
46inline 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.
49std::string ToString(double value);
50// And the bool to be converted as true/false instead of 1/0.
51std::string ToString(bool value);
52
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070053} // namespace string_utils
54} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070055
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070056#endif // BUFFET_STRING_UTILS_H_