3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
8 #ifndef MSP_STRINGS_UTILS_H_
9 #define MSP_STRINGS_UTILS_H_
17 Compares two strings, ignoring upper/lower case.
19 @param s1 First string
20 @param s2 Second string
22 @return -1 if s1<s2, 0 if s1==s2, 1 if s1>s2
24 int strcasecmp(const std::string &s1, const std::string &s2);
27 Converts a string to lower case.
29 std::string tolower(const std::string &);
32 Converts a string to upper case.
34 std::string toupper(const std::string &);
37 Checks whether a string consists of digits only.
39 bool isnumrc(const std::string &);
42 Checks whether a string consists of alphabetic characters only.
44 bool isalpha(const std::string &);
47 Checks whether a string consists of alphanumeric characters only.
49 bool isalnum(const std::string &);
51 /* These are required to make the standard version work from inside the Msp
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.
65 @param sep Separator characters
66 @param max_split Maximum number of splits to perform
68 std::vector<std::string> split(const std::string &str, const std::string &sep=" \t\r\n", int max_split=-1);
71 Splits a string on occurrences of a single character.
73 std::vector<std::string> split(const std::string &str, char sep, int max_split=-1);
76 Splits a string on occurrences of another string.
78 std::vector<std::string> split_long(const std::string &str, const std::string &sep, int max_split=-1);
81 Splits a string on occurrences of another string. Two consecutive separators
82 will cause an empty string to be placed in the result.
84 std::vector<std::string> split_fields(const std::string &str, const std::string &sep, int max_split=-1);
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.
90 std::vector<std::string> split_fields(const std::string &str, char sep, int max_split=-1);
93 Concatenates strings from an iterator range.
95 @param begin First iterator
96 @param end Last iterator
97 @param sep Separator to be inserted between strings
99 template<typename Iter>
100 std::string join(Iter begin, Iter end, const std::string &sep=" ")
103 for(Iter i=begin; i!=end; ++i)
114 Strips leading and trailing whitespace from a string.
116 std::string strip(const std::string &);
119 Unescapes a string with C escape sequences.
121 std::string c_unescape(const std::string &str);
124 Escapes any non-printable characters in a string with C escape sequences.
127 @param escape_8bit If true, consider characters with high bit set as
130 @return An escaped version of the string
132 std::string c_escape(const std::string &str, bool escape_8bit=true);