diff options
author | Paul Westbrook <pwestbro@google.com> | 2015-11-01 15:29:33 -0800 |
---|---|---|
committer | Paul Westbrook <pwestbro@google.com> | 2015-11-02 18:55:30 +0000 |
commit | 5a1f600e9d7d26c36b3e22ff0dc0ae9e3b2425fc (patch) | |
tree | 9a3a96971d8c687c1a1976dc9abf49dd8d3c62f2 /src/string_utils.h | |
parent | 1bc421c9ef13ad855a3f749143fa8c4bc568ef16 (diff) | |
download | libweave-5a1f600e9d7d26c36b3e22ff0dc0ae9e3b2425fc.tar.gz |
Remove the unneeded libweave directory
Change-Id: I30fd8c5626cf83da6415ffa14a2019ef43be9916
Reviewed-on: https://weave-review.googlesource.com/1450
Reviewed-by: Paul Westbrook <pwestbro@google.com>
Diffstat (limited to 'src/string_utils.h')
-rw-r--r-- | src/string_utils.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/string_utils.h b/src/string_utils.h new file mode 100644 index 0000000..e349d9d --- /dev/null +++ b/src/string_utils.h @@ -0,0 +1,61 @@ +// Copyright 2015 The Weave Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef LIBWEAVE_SRC_STRING_UTILS_H_ +#define LIBWEAVE_SRC_STRING_UTILS_H_ + +#include <string> +#include <utility> +#include <vector> + +namespace weave { + +// Treats the string as a delimited list of substrings and returns the array +// of original elements of the list. +// |trim_whitespaces| causes each element to have all whitespaces trimmed off. +// |purge_empty_strings| specifies whether empty elements from the original +// string should be omitted. +std::vector<std::string> Split(const std::string& str, + const std::string& delimiter, + bool trim_whitespaces, + bool purge_empty_strings); + +// Splits the string into two pieces at the first position of the specified +// delimiter. +std::pair<std::string, std::string> SplitAtFirst(const std::string& str, + const std::string& delimiter, + bool trim_whitespaces); + +// Joins strings into a single string separated by |delimiter|. +template <class InputIterator> +std::string JoinRange(const std::string& delimiter, + InputIterator first, + InputIterator last) { + std::string result; + if (first == last) + return result; + result = *first; + for (++first; first != last; ++first) { + result += delimiter; + result += *first; + } + return result; +} + +template <class Container> +std::string Join(const std::string& delimiter, const Container& strings) { + using std::begin; + using std::end; + return JoinRange(delimiter, begin(strings), end(strings)); +} + +inline std::string Join(const std::string& delimiter, + const std::string& str1, + const std::string& str2) { + return str1 + delimiter + str2; +} + +} // namespace weave + +#endif // LIBWEAVE_SRC_STRING_UTILS_H_ |