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