]> git.tdb.fi Git - libs/core.git/blob - source/utils.h
Add utility functions to check the contents of a string
[libs/core.git] / source / utils.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_STRINGS_UTILS_H_
9 #define MSP_STRINGS_UTILS_H_
10
11 #include <string>
12 #include <vector>
13
14 namespace Msp {
15
16 /**
17 Compares two strings, ignoring upper/lower case.
18
19 @param   s1  First string
20 @param   s2  Second string
21
22 @return  -1 if s1<s2, 0 if s1==s2, 1 if s1>s2
23 */
24 int strcasecmp(const std::string &s1, const std::string &s2);
25
26 /**
27 Converts a string to lower case.
28 */
29 std::string tolower(const std::string &);
30
31 /**
32 Converts a string to upper case.
33 */
34 std::string toupper(const std::string &);
35
36 /**
37 Checks whether a string consists of digits only.
38 */
39 bool isnumrc(const std::string &);
40
41 /**
42 Checks whether a string consists of alphabetic characters only.
43 */
44 bool isalpha(const std::string &);
45
46 /**
47 Checks whether a string consists of alphanumeric characters only.
48 */
49 bool isalnum(const std::string &);
50
51 /* These are required to make the standard version work from inside the Msp
52 namespace */
53 using std::tolower;
54 using std::toupper;
55 using std::isalpha;
56 using std::isalnum;
57
58 /**
59 Splits a string at occurrences of any of the characters in sep.  If max_split
60 is non-negative, at most that many split will be performed, i.e. the resulting
61 vector will contain at most max_split+1 elements.  Two or more consecutive
62 separator characters will be treated as a single separator.
63
64 @param   str        A string
65 @param   sep        Separator characters
66 @param   max_split  Maximum number of splits to perform
67 */
68 std::vector<std::string> split(const std::string &str, const std::string &sep=" \t\r\n", int max_split=-1);
69
70 /**
71 Splits a string on occurrences of a single character.
72 */
73 std::vector<std::string> split(const std::string &str, char sep, int max_split=-1);
74
75 /**
76 Splits a string on occurrences of another string.
77 */
78 std::vector<std::string> split_long(const std::string &str, const std::string &sep, int max_split=-1);
79
80 /**
81 Splits a string on occurrences of another string.  Two consecutive separators
82 will cause an empty string to be placed in the result.
83 */
84 std::vector<std::string> split_fields(const std::string &str, const std::string &sep, int max_split=-1);
85
86 /**
87 Splits a string on occurrences of a single character.  Two consecutive
88 separators will cause an empty string to be placed in the result.
89 */
90 std::vector<std::string> split_fields(const std::string &str, char sep, int max_split=-1);
91
92 /**
93 Concatenates strings from an iterator range.
94
95 @param  begin  First iterator
96 @param  end    Last iterator
97 @param  sep    Separator to be inserted between strings
98 */
99 template<typename Iter>
100 std::string join(Iter begin, Iter end, const std::string &sep=" ")
101 {
102         std::string result;
103         for(Iter i=begin; i!=end; ++i)
104         {
105                 if(i!=begin)
106                         result+=sep;
107                 result+=*i;
108         }
109
110         return result;
111 }
112
113 /**
114 Strips leading and trailing whitespace from a string.
115 */
116 std::string strip(const std::string &);
117
118 /**
119 Unescapes a string with C escape sequences.
120 */
121 std::string c_unescape(const std::string &str);
122
123 /**
124 Escapes any non-printable characters in a string with C escape sequences.
125
126 @param   str          A string
127 @param   escape_8bit  If true, consider characters with high bit set as
128                       non-printable
129
130 @return  An escaped version of the string
131 */
132 std::string c_escape(const std::string &str, bool escape_8bit=true);
133
134 } // namespace Msp
135
136 #endif