4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
12 * A collection of string helper functions that works with string.
13 * Some of these would certainly benefit from a rewrite/optimization.
27 int compare_no_case(std::string const & s, std::string const & s2);
30 int compare_ascii_no_case(std::string const & s, std::string const & s2);
33 int compare_no_case(std::string const & s, std::string const & s2, unsigned int len);
37 int compare(char const * a, char const * b)
39 #ifndef CXX_GLOBAL_CSTD
40 return std::strcmp(a, b);
48 int compare(char const * a, char const * b, unsigned int len)
50 #ifndef CXX_GLOBAL_CSTD
51 return std::strncmp(a, b, len);
53 return strncmp(a, b, len);
58 bool isStrInt(std::string const & str);
60 /// does the std::string represent an unsigned integer value ?
61 bool isStrUnsignedInt(std::string const & str);
64 int strToInt(std::string const & str);
66 /// convert string to an unsigned integer
67 unsigned int strToUnsignedInt(std::string const & str);
70 bool isStrDbl(std::string const & str);
73 double strToDbl(std::string const & str);
76 char lowercase(char c);
79 char uppercase(char c);
81 /// same as lowercase(), but ignores locale
82 std::string const ascii_lowercase(std::string const &);
85 std::string const lowercase(std::string const &);
88 std::string const uppercase(std::string const &);
90 /// Does the std::string start with this prefix?
91 bool prefixIs(std::string const &, std::string const &);
93 /// Does the string end with this char?
94 bool suffixIs(std::string const &, char);
96 /// Does the std::string end with this suffix?
97 bool suffixIs(std::string const &, std::string const &);
100 template <typename B>
101 bool contains(std::string const & a, B b)
103 return a.find(b) != std::string::npos;
107 bool containsOnly(std::string const &, std::string const &);
109 /** Extracts a token from this string at the nth delim.
110 Doesn't modify the original string. Similar to strtok.
113 token("a;bc;d", ';', 1) == "bc";
114 token("a;bc;d", ';', 2) == "d";
117 std::string const token(std::string const & a, char delim, int n);
120 /** Search a token in this string using the delim.
121 Doesn't modify the original string. Returns -1 in case of
125 tokenPos("a;bc;d", ';', "bc") == 1;
126 tokenPos("a;bc;d", ';', "d") == 2;
129 int tokenPos(std::string const & a, char delim, std::string const & tok);
132 /// Substitute all \a oldchar with \a newchar
133 std::string const subst(std::string const & a, char oldchar, char newchar);
135 /// substitutes all instances of \a oldstr with \a newstr
136 std::string const subst(std::string const & a,
137 std::string const & oldstr, std::string const & newstr);
139 /** Trims characters off the end and beginning of a string.
141 trim("ccabccc", "c") == "ab".
144 std::string const trim(std::string const & a, char const * p = " ");
146 /** Trims characters off the end of a string.
148 rtrim("abccc", "c") == "ab".
151 std::string const rtrim(std::string const & a, char const * p = " ");
153 /** Trims characters off the beginning of a string.
155 ltrim("ababcdef", "ab") = "cdef"
158 std::string const ltrim(std::string const & a, char const * p = " ");
160 /** Splits the string by the first delim.
161 Splits the string by the first appearance of delim.
162 The leading string up to delim is returned in piece (not including
163 delim), while the original string is cut from after the delimiter.
166 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
169 std::string const split(std::string const & a, std::string & piece, char delim);
171 /// Same as split but does not return a piece
172 std::string const split(std::string const & a, char delim);
174 /// Same as split but uses the last delim.
175 std::string const rsplit(std::string const & a, std::string & piece, char delim);
177 /// Escapes non ASCII chars
178 std::string const escape(std::string const & lab);
180 /// gives a vector of stringparts which have the delimiter delim
181 std::vector<std::string> const getVectorFromString(std::string const & str,
182 std::string const & delim = std::string(","));
184 // the same vice versa
185 std::string const getStringFromVector(std::vector<std::string> const & vec,
186 std::string const & delim = std::string(","));
189 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
191 #include <boost/format.hpp>
194 string bformat(string const & fmt, Arg1 arg1)
196 return (boost::format(fmt) % arg1).str();
200 template<class Arg1, class Arg2>
201 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
203 return (boost::format(fmt) % arg1 % arg2).str();
207 template<class Arg1, class Arg2, class Arg3>
208 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
210 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
214 template<class Arg1, class Arg2, class Arg3, class Arg4>
215 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
217 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
222 template <class Arg1>
223 std::string bformat(std::string const & fmt, Arg1);
225 template <class Arg1, class Arg2>
226 std::string bformat(std::string const & fmt, Arg1, Arg2);
228 template <class Arg1, class Arg2, class Arg3>
229 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
231 template <class Arg1, class Arg2, class Arg3, class Arg4>
232 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
236 } // namespace support