3 This file is part of libmspstrings
4 Copyright © 2006-2007 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 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.
43 @param sep Separator characters
44 @param max_split Maximum number of splits to perform
46 std::vector<std::string> split(const std::string &str, const std::string &sep=" \t\r\n", int max_split=-1);
49 Splits a string on occurrences of a single character.
51 std::vector<std::string> split(const std::string &str, char sep, int max_split=-1);
54 Splits a string on occurrences of another string.
56 std::vector<std::string> split_long(const std::string &str, const std::string &sep, int max_split=-1);
59 Splits a string on occurrences of another string. Two consecutive separators
60 will cause an empty string to be placed in the result.
62 std::vector<std::string> split_fields(const std::string &str, const std::string &sep, int max_split=-1);
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.
68 std::vector<std::string> split_fields(const std::string &str, char sep, int max_split=-1);
71 Concatenates strings from an iterator range.
73 @param begin First iterator
74 @param end Last iterator
75 @param sep Separator to be inserted between strings
77 template<typename Iter>
78 std::string join(Iter begin, Iter end, const std::string &sep=" ")
81 for(Iter i=begin; i!=end; ++i)
92 Strips leading and trailing whitespace from a string.
94 std::string strip(const std::string &);
97 Unescapes a string with C escape sequences.
99 std::string c_unescape(const std::string &str);
102 Escapes any non-printable characters in a string with C escape sequences.
105 @param escape_8bit If true, consider characters with high bit set as
108 @return An escaped version of the string
110 std::string c_escape(const std::string &str, bool escape_8bit=true);