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 | * trim the space (or any substring) chars on the right of a string␊ |
15 | */␊ |
16 | inline std::string trim_right(const std::string &source , const std::string& substr = " ")␊ |
17 | {␊ |
18 | ␉std::string str = source;␊ |
19 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
20 | }␊ |
21 | ␊ |
22 | /**␊ |
23 | * trim the space chars (or any substring) on the left of a string␊ |
24 | */␊ |
25 | inline std::string trim_left( const std::string& source, const std::string& substr = " ")␊ |
26 | {␊ |
27 | ␉std::string str = source;␊ |
28 | ␉return str.erase(0 , source.find_first_not_of(substr) );␊ |
29 | }␊ |
30 | ␊ |
31 | /**␊ |
32 | * trim the space chars (or any substring) on the left and the right of a string␊ |
33 | */␊ |
34 | inline std::string trim(const std::string& source, const std::string& substr = " ")␊ |
35 | {␊ |
36 | ␉return trim_left( trim_right( source , substr) , substr );␊ |
37 | } ␊ |
38 | ␊ |
39 | // const char* str input versions␊ |
40 | inline std::string trim_right(const char* src , const std::string& substr = " ")␊ |
41 | {␊ |
42 | ␉std::string str = src ? src : "";␊ |
43 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
44 | }␊ |
45 | ␊ |
46 | /**␊ |
47 | * trim the space chars (or any substring) on the left of a string␊ |
48 | */␊ |
49 | inline std::string trim_left( const char* src, const std::string& substr = " ")␊ |
50 | {␊ |
51 | ␉std::string str = src ? src : "";␊ |
52 | ␉return str.erase(0 , str.find_first_not_of(substr) );␊ |
53 | }␊ |
54 | ␊ |
55 | /**␊ |
56 | * trim the space chars (or any substring) on the left and the right of a string␊ |
57 | */␊ |
58 | inline std::string trim(const char* src, const std::string& substr = " ")␊ |
59 | {␊ |
60 | ␉return trim_left( trim_right( src , substr) , substr );␊ |
61 | } ␊ |
62 | ␊ |
63 | /**␊ |
64 | * String Tokenizer␊ |
65 | * In: src string and separator␊ |
66 | * Out: a list of string tokens␊ |
67 | */␊ |
68 | std::list<std::string> tokenize(const std::string& src, const std::string& sep=" ");␊ |
69 | ␊ |
70 | #endif␊ |
71 | |