]> git.tdb.fi Git - libs/core.git/blob - source/strings/utils.h
Add move semantics to Variant
[libs/core.git] / source / strings / utils.h
1 #ifndef MSP_STRINGS_UTILS_H_
2 #define MSP_STRINGS_UTILS_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/core/mspcore_api.h>
7
8 namespace Msp {
9
10 /** Compares two strings, ignoring upper/lower case.  Returns an integer less
11 than, equal to or greater than zero depending on whether the first string
12 lexicographically precedes, is equal to or follows the second one,
13 respectively. */
14 MSPCORE_API int strcasecmp(const std::string &s1, const std::string &s2);
15
16 /** Converts a string to lower case. */
17 MSPCORE_API std::string tolower(const std::string &);
18
19 /** Converts a string to upper case. */
20 MSPCORE_API std::string toupper(const std::string &);
21
22 /** Checks whether a string consists of digits only. */
23 MSPCORE_API bool isnumrc(const std::string &);
24
25 /** Checks whether a string consists of alphabetic characters only. */
26 MSPCORE_API bool isalpha(const std::string &);
27
28 /** Checks whether a string consists of alphanumeric characters only. */
29 MSPCORE_API bool isalnum(const std::string &);
30
31 /* These are required to make the standard version work from inside the Msp
32 namespace */
33 using std::tolower;
34 using std::toupper;
35 using std::isalpha;
36 using std::isalnum;
37
38 /** Splits a string at occurrences of any of the characters in sep.  Default
39 is to split at whitespace.  Two or more consecutive separator characters will
40 be treated as a single separator.
41
42 If max_split is non-negative, at most that many split will be performed, i.e.
43 the resulting vector will contain at most max_split+1 elements. */
44 MSPCORE_API std::vector<std::string> split(const std::string &str, const std::string &sep = " \t\r\n", int max_split = -1);
45
46 /** Splits a string on occurrences of a single character. */
47 MSPCORE_API std::vector<std::string> split(const std::string &str, char sep, int max_split = -1);
48
49 /** Splits a string on occurrences of another string. */
50 MSPCORE_API std::vector<std::string> split_long(const std::string &str, const std::string &sep, int max_split = -1);
51
52 /** Splits a string on occurrences of another string.  Two consecutive
53 separators will cause an empty string to be placed in the result. */
54 MSPCORE_API std::vector<std::string> split_fields(const std::string &str, const std::string &sep, int max_split = -1);
55
56 /** Splits a string on occurrences of a single character.  Two consecutive
57 separators will cause an empty string to be placed in the result. */
58 MSPCORE_API std::vector<std::string> split_fields(const std::string &str, char sep, int max_split = -1);
59
60 /** Appends a string to another, using a separator if both are non-empty. */
61 MSPCORE_API std::string &append(std::string &str, const std::string &sep, const std::string &other);
62
63 /** Joins two strings, using a separator if both are non-empty. */
64 MSPCORE_API std::string join(const std::string &str1, const std::string &sep, const std::string &str2);
65
66 /** Concatenates strings from an iterator range. */
67 template<typename Iter>
68 std::string join(Iter begin, Iter end, const std::string &sep = " ")
69 {
70         std::string result;
71         for(Iter i=begin; i!=end; ++i)
72                 append(result, sep, *i);
73
74         return result;
75 }
76
77 /** Strips leading and trailing whitespace from a string. */
78 MSPCORE_API std::string strip(const std::string &);
79
80 /** Unescapes a string with C escape sequences. */
81 MSPCORE_API std::string c_unescape(const std::string &str);
82
83 /** Escapes any non-printable characters in a string with C escape sequences.
84 Optionally, any characters with the high bit set can be escaped as well. */
85 MSPCORE_API std::string c_escape(const std::string &str, bool escape_8bit = true);
86
87 } // namespace Msp
88
89 #endif