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 * trim the space (or any substring) chars on the right of a string
15 */
16inline std::string trim_right(const std::string &source , const std::string& substr = " ")
17{
18std::string str = source;
19return 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 */
25inline std::string trim_left( const std::string& source, const std::string& substr = " ")
26{
27std::string str = source;
28return 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 */
34inline std::string trim(const std::string& source, const std::string& substr = " ")
35{
36return trim_left( trim_right( source , substr) , substr );
37}
38
39// const char* str input versions
40inline std::string trim_right(const char* src , const std::string& substr = " ")
41{
42std::string str = src ? src : "";
43return 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 */
49inline std::string trim_left( const char* src, const std::string& substr = " ")
50{
51std::string str = src ? src : "";
52return 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 */
58inline std::string trim(const char* src, const std::string& substr = " ")
59{
60return 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 */
68std::list<std::string> tokenize(const std::string& src, const std::string& sep=" ");
69
70#endif
71

Archive Download this file

Revision: 48