1 | /*␊ |
2 | * string_util.h␊ |
3 | * ChameleonPrefPane␊ |
4 | *␊ |
5 | * Created by Rekursor on 1/22/10.␊ |
6 | *␊ |
7 | */␊ |
8 | #ifndef __CHSTR_UTIL_H␊ |
9 | #define __CHSTR_UTIL_H␊ |
10 | #include <string>␊ |
11 | #include <list>␊ |
12 | ␊ |
13 | /**␊ |
14 | * String Tokenizer␊ |
15 | * In: src string and separator␊ |
16 | * Out: a list of string tokens␊ |
17 | */␊ |
18 | std::list<std::string> tokenize(const std::string& src, const std::string& sep=" ");␊ |
19 | ␊ |
20 | /** 'in place' replace_all occurences of sToFind by sToReplace in a s string, return s */␊ |
21 | std::string& replace_all(std::string &s, const std::string &sToFind, const std::string &sToReplace);␊ |
22 | ␊ |
23 | /**␊ |
24 | * trim the space (or any substring) chars on the right of a string␊ |
25 | */␊ |
26 | inline std::string trim_right(const std::string &source , const std::string& substr = " ")␊ |
27 | {␊ |
28 | ␉std::string str = source;␊ |
29 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
30 | }␊ |
31 | ␊ |
32 | /**␊ |
33 | * trim the space chars (or any substring) on the left of a string␊ |
34 | */␊ |
35 | inline std::string trim_left( const std::string& source, const std::string& substr = " ")␊ |
36 | {␊ |
37 | ␉std::string str = source;␊ |
38 | ␉return str.erase(0 , source.find_first_not_of(substr) );␊ |
39 | }␊ |
40 | ␊ |
41 | /**␊ |
42 | * trim the space chars (or any substring) on the left and the right of a string␊ |
43 | */␊ |
44 | inline std::string trim(const std::string& source, const std::string& substr = " ")␊ |
45 | {␊ |
46 | ␉return trim_left( trim_right( source , substr) , substr );␊ |
47 | } ␊ |
48 | ␊ |
49 | // const char* str input versions␊ |
50 | inline std::string trim_right(const char* src , const std::string& substr = " ")␊ |
51 | {␊ |
52 | ␉std::string str = src ? src : "";␊ |
53 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
54 | }␊ |
55 | ␊ |
56 | /**␊ |
57 | * trim the space chars (or any substring) on the left of a string␊ |
58 | */␊ |
59 | inline std::string trim_left( const char* src, const std::string& substr = " ")␊ |
60 | {␊ |
61 | ␉std::string str = src ? src : "";␊ |
62 | ␉return str.erase(0 , str.find_first_not_of(substr) );␊ |
63 | }␊ |
64 | ␊ |
65 | /**␊ |
66 | * trim the space chars (or any substring) on the left and the right of a string␊ |
67 | */␊ |
68 | inline std::string trim(const char* src, const std::string& substr = " ")␊ |
69 | {␊ |
70 | ␉return trim_left( trim_right( src , substr) , substr );␊ |
71 | } ␊ |
72 | ␊ |
73 | #endif␊ |
74 | |