1 | /*␊ |
2 | * string_util.cpp␊ |
3 | * ChameleonPrefPane␊ |
4 | *␊ |
5 | * Created by Rekursor on 1/23/10.␊ |
6 | */␊ |
7 | ␊ |
8 | #include "string_util.h"␊ |
9 | ␊ |
10 | /*␊ |
11 | * String Tokenizer␊ |
12 | * In: src string and separator␊ |
13 | * Out: a list of string tokens␊ |
14 | */␊ |
15 | std::list<std::string> tokenize(const std::string& src, const std::string& sep)␊ |
16 | {␊ |
17 | ␉std::list<std::string> ret;␊ |
18 | ␉if (src.length()==0) return ret;␊ |
19 | ␉␊ |
20 | ␉std::string::size_type left=0, right=0;␊ |
21 | ␉std::string token;␊ |
22 | ␉std::string::size_type len = sep.length();␊ |
23 | ␉␊ |
24 | ␉for (left=0; (right = src.find(sep, left)) != std::string::npos; left = right + len )␊ |
25 | ␉{␊ |
26 | ␉␉token = src.substr (left, right-left);␊ |
27 | ␉␉if (token.length()>0) ret.push_back (token);␊ |
28 | ␉}␊ |
29 | ␉token = src.substr(left);␊ |
30 | ␉if (token.length()>0) ret.push_back (token);␊ |
31 | ␉␊ |
32 | ␉return ret;␊ |
33 | }␊ |
34 | ␊ |
35 | std::string& replace_all(std::string &s, const std::string &sToFind, const std::string &sToReplace)␊ |
36 | {␊ |
37 | ␉if(sToReplace.empty()) return s;␊ |
38 | ␉␊ |
39 | ␉std::string::size_type b = 0;␊ |
40 | ␉for (;;)␊ |
41 | ␉{␊ |
42 | ␉␉b = s.find(sToFind, b);␊ |
43 | ␉␉if (b == s.npos) break;␊ |
44 | ␉␉s.replace(b, sToFind.size(), sToReplace);␊ |
45 | ␉␉b += sToReplace.size();␊ |
46 | ␉}␊ |
47 | ␉return s;␊ |
48 | }␊ |
49 | ␉␉ ␊ |
50 | |