Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/string_util.h

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 */
18std::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 */
21std::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 */
26inline std::string trim_right(const std::string &source , const std::string& substr = " ")
27{
28std::string str = source;
29
30return str.erase( str.find_last_not_of(substr) + 1);
31}
32
33
34inline std::string trim_label(const std::string &source )
35{
36std::string str = source;
37std::string str2 = str.erase( str.find_last_not_of("*") + 1);
38return 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 */
44inline std::string trim_left( const std::string& source, const std::string& substr = " ")
45{
46std::string str = source;
47return 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 */
53inline std::string trim(const std::string& source, const std::string& substr = " ")
54{
55return trim_left( trim_right( source , substr) , substr );
56}
57
58// const char* str input versions
59inline std::string trim_right(const char* src , const std::string& substr = " ")
60{
61std::string str = src ? src : "";
62return 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 */
68inline std::string trim_left( const char* src, const std::string& substr = " ")
69{
70std::string str = src ? src : "";
71return 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 */
77inline std::string trim(const char* src, const std::string& substr = " ")
78{
79return trim_left( trim_right( src , substr) , substr );
80}
81
82/**
83 * trim the space chars (or any substring) on the left of a string
84 */
85inline std::string string_left( const std::string& source, const std::string& substr = " ")
86{
87std::string str = source;
88std::string::size_type pos = source.find_first_of(substr);
89return pos!=std::string::npos ? str.erase(source.find_first_of(substr) ) : str;
90}
91
92
93inline char HexToDec(char nibble) { nibble = toupper(nibble); return (nibble < 'A') ? nibble - '0' : nibble - 'A' +10;}
94
95#endif
96

Archive Download this file

Revision: 431