]> git.tdb.fi Git - libs/core.git/blob - source/utils.h
Add documentation for all functions in utils
[libs/core.git] / source / utils.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 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 Splits a string at occurrences of any of the characters in sep.  If max_split
38 is non-negative, at most that many split will be performed, i.e. the resulting
39 vector will contain at most max_split+1 elements.  Two or more consecutive
40 separator characters will be treated as a single separator.
41
42 @param   str        A string
43 @param   sep        Separator characters
44 @param   max_split  Maximum number of splits to perform
45 */
46 std::vector<std::string> split(const std::string &str, const std::string &sep=" \t\r\n", int max_split=-1);
47
48 /**
49 Splits a string on occurrences of a single character.
50 */
51 std::vector<std::string> split(const std::string &str, char sep, int max_split=-1);
52
53 /**
54 Splits a string on occurrences of another string.
55 */
56 std::vector<std::string> split_long(const std::string &str, const std::string &sep, int max_split=-1);
57
58 /**
59 Splits a string on occurrences of another string.  Two consecutive separators
60 will cause an empty string to be placed in the result.
61 */
62 std::vector<std::string> split_fields(const std::string &str, const std::string &sep, int max_split=-1);
63
64 /**
65 Splits a string on occurrences of a single character.  Two consecutive
66 separators will cause an empty string to be placed in the result.
67 */
68 std::vector<std::string> split_fields(const std::string &str, char sep, int max_split=-1);
69
70 /**
71 Concatenates strings from an iterator range.
72
73 @param  begin  First iterator
74 @param  end    Last iterator
75 @param  sep    Separator to be inserted between strings
76 */
77 template<typename Iter>
78 std::string join(Iter begin, Iter end, const std::string &sep=" ")
79 {
80         std::string result;
81         for(Iter i=begin; i!=end; ++i)
82         {
83                 if(i!=begin)
84                         result+=sep;
85                 result+=*i;
86         }
87
88         return result;
89 }
90
91 /**
92 Strips leading and trailing whitespace from a string.
93 */
94 std::string strip(const std::string &);
95
96 /**
97 Unescapes a string with C escape sequences.
98 */
99 std::string c_unescape(const std::string &str);
100
101 /**
102 Escapes any non-printable characters in a string with C escape sequences.
103
104 @param   str          A string
105 @param   escape_8bit  If true, consider characters with high bit set as
106                       non-printable
107
108 @return  An escaped version of the string
109 */
110 std::string c_escape(const std::string &str, bool escape_8bit=true);
111
112 } // namespace Msp
113
114 #endif