1 // Copyright 2013 The Chromium 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 LIBRARIES_SDK_UTIL_STRING_UTIL_H_
6 #define LIBRARIES_SDK_UTIL_STRING_UTIL_H_
7
8 #include <string>
9 #include <vector>
10
11 namespace sdk_util {
12
13 // Splits |str| into a vector of strings delimited by |c|, placing the results
14 // in |r|. If several instances of |c| are contiguous, or if |str| begins with
15 // or ends with |c|, then an empty string is inserted. If |str| is empty, then
16 // no strings are inserted.
17 //
18 // NOTE: Unlike Chrome's base::SplitString, this DOES NOT trim white space.
SplitString(const std::string & str,char c,std::vector<std::string> * r)19 inline void SplitString(const std::string& str,
20 char c,
21 std::vector<std::string>* r) {
22 r->clear();
23 size_t last = 0;
24 size_t size = str.size();
25 for (size_t i = 0; i <= size; ++i) {
26 if (i == size || str[i] == c) {
27 std::string tmp(str, last, i - last);
28 // Avoid converting an empty source string into a vector of one empty
29 // string.
30 if (i != size || !r->empty() || !tmp.empty())
31 r->push_back(tmp);
32 last = i + 1;
33 }
34 }
35 }
36
37 } // namespace sdk_util
38
39 #endif // LIBRARIES_SDK_UTIL_STRING_UTIL_H_
40