Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/ErmaC/ChameleonPrefPane/Sources/string_util.cpp

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 */
15std::list<std::string> tokenize(const std::string& src, const std::string& sep)
16{
17std::list<std::string> ret;
18if (src.length()==0) return ret;
19
20std::string::size_type left=0, right=0;
21std::string token;
22std::string::size_type len = sep.length();
23
24for (left=0; (right = src.find(sep, left)) != std::string::npos; left = right + len )
25{
26token = src.substr (left, right-left);
27if (token.length()>0) ret.push_back (token);
28}
29token = src.substr(left);
30if (token.length()>0) ret.push_back (token);
31
32return ret;
33}
34
35std::string& replace_all(std::string &s, const std::string &sToFind, const std::string &sToReplace)
36{
37if(sToReplace.empty()) return s;
38
39std::string::size_type b = 0;
40for (;;)
41{
42b = s.find(sToFind, b);
43if (b == s.npos) break;
44s.replace(b, sToFind.size(), sToReplace);
45b += sToReplace.size();
46}
47return s;
48}
49
50

Archive Download this file

Revision: 396