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 | ␉␊ |
30 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
31 | }␊ |
32 | ␊ |
33 | ␊ |
34 | inline std::string trim_label(const std::string &source )␊ |
35 | {␊ |
36 | ␉std::string str = source;␊ |
37 | ␉std::string str2 = str.erase( str.find_last_not_of("*") + 1);␊ |
38 | ␉return str2.erase( str2.find_last_not_of(" ") + 1);␊ |
39 | }␊ |
40 | ␊ |
41 | /**␊ |
42 | * trim the space chars (or any substring) on the left of a string␊ |
43 | */␊ |
44 | inline std::string trim_left( const std::string& source, const std::string& substr = " ")␊ |
45 | {␊ |
46 | ␉std::string str = source;␊ |
47 | ␉return str.erase(0 , source.find_first_not_of(substr) );␊ |
48 | }␊ |
49 | ␊ |
50 | /**␊ |
51 | * trim the space chars (or any substring) on the left and the right of a string␊ |
52 | */␊ |
53 | inline std::string trim(const std::string& source, const std::string& substr = " ")␊ |
54 | {␊ |
55 | ␉return trim_left( trim_right( source , substr) , substr );␊ |
56 | } ␊ |
57 | ␊ |
58 | // const char* str input versions␊ |
59 | inline std::string trim_right(const char* src , const std::string& substr = " ")␊ |
60 | {␊ |
61 | ␉std::string str = src ? src : "";␊ |
62 | ␉return str.erase( str.find_last_not_of(substr) + 1);␊ |
63 | }␊ |
64 | ␊ |
65 | /**␊ |
66 | * trim the space chars (or any substring) on the left of a string␊ |
67 | */␊ |
68 | inline std::string trim_left( const char* src, const std::string& substr = " ")␊ |
69 | {␊ |
70 | ␉std::string str = src ? src : "";␊ |
71 | ␉return str.erase(0 , str.find_first_not_of(substr) );␊ |
72 | }␊ |
73 | ␊ |
74 | /**␊ |
75 | * trim the space chars (or any substring) on the left and the right of a string␊ |
76 | */␊ |
77 | inline std::string trim(const char* src, const std::string& substr = " ")␊ |
78 | {␊ |
79 | ␉return trim_left( trim_right( src , substr) , substr );␊ |
80 | } ␊ |
81 | ␊ |
82 | inline char HexToDec(char nibble) { nibble = toupper(nibble); return (nibble < 'A') ? nibble - '0' : nibble - 'A' +10;}␊ |
83 | ␊ |
84 | #endif␊ |
85 | |