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 bool isStrDbl(std::string const & str);
67 char lowercase(char c);
70 char uppercase(char c);
72 /// same as lowercase(), but ignores locale
73 std::string const ascii_lowercase(std::string const &);
76 std::string const lowercase(std::string const &);
79 std::string const uppercase(std::string const &);
81 /// Does the std::string start with this prefix?
82 bool prefixIs(std::string const &, std::string const &);
84 /// Does the string end with this char?
85 bool suffixIs(std::string const &, char);
87 /// Does the std::string end with this suffix?
88 bool suffixIs(std::string const &, std::string const &);
92 bool contains(std::string const & a, B b)
94 return a.find(b) != std::string::npos;
98 bool containsOnly(std::string const &, std::string const &);
100 /** Extracts a token from this string at the nth delim.
101 Doesn't modify the original string. Similar to strtok.
104 token("a;bc;d", ';', 1) == "bc";
105 token("a;bc;d", ';', 2) == "d";
108 std::string const token(std::string const & a, char delim, int n);
111 /** Search a token in this string using the delim.
112 Doesn't modify the original string. Returns -1 in case of
116 tokenPos("a;bc;d", ';', "bc") == 1;
117 tokenPos("a;bc;d", ';', "d") == 2;
120 int tokenPos(std::string const & a, char delim, std::string const & tok);
123 /// Substitute all \a oldchar with \a newchar
124 std::string const subst(std::string const & a, char oldchar, char newchar);
126 /// substitutes all instances of \a oldstr with \a newstr
127 std::string const subst(std::string const & a,
128 std::string const & oldstr, std::string const & newstr);
130 /** Trims characters off the end and beginning of a string.
132 trim("ccabccc", "c") == "ab".
135 std::string const trim(std::string const & a, char const * p = " ");
137 /** Trims characters off the end of a string.
139 rtrim("abccc", "c") == "ab".
142 std::string const rtrim(std::string const & a, char const * p = " ");
144 /** Trims characters off the beginning of a string.
146 ltrim("ababcdef", "ab") = "cdef"
149 std::string const ltrim(std::string const & a, char const * p = " ");
151 /** Splits the string by the first delim.
152 Splits the string by the first appearance of delim.
153 The leading string up to delim is returned in piece (not including
154 delim), while the original string is cut from after the delimiter.
157 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
160 std::string const split(std::string const & a, std::string & piece, char delim);
162 /// Same as split but does not return a piece
163 std::string const split(std::string const & a, char delim);
165 /// Same as split but uses the last delim.
166 std::string const rsplit(std::string const & a, std::string & piece, char delim);
168 /// Escapes non ASCII chars
169 std::string const escape(std::string const & lab);
171 /// gives a vector of stringparts which have the delimiter delim
172 std::vector<std::string> const getVectorFromString(std::string const & str,
173 std::string const & delim = std::string(","));
175 // the same vice versa
176 std::string const getStringFromVector(std::vector<std::string> const & vec,
177 std::string const & delim = std::string(","));
180 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
182 #include <boost/format.hpp>
185 string bformat(string const & fmt, Arg1 arg1)
187 return (boost::format(fmt) % arg1).str();
191 template<class Arg1, class Arg2>
192 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
194 return (boost::format(fmt) % arg1 % arg2).str();
198 template<class Arg1, class Arg2, class Arg3>
199 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
201 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
205 template<class Arg1, class Arg2, class Arg3, class Arg4>
206 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
208 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
213 template <class Arg1>
214 std::string bformat(std::string const & fmt, Arg1);
216 template <class Arg1, class Arg2>
217 std::string bformat(std::string const & fmt, Arg1, Arg2);
219 template <class Arg1, class Arg2, class Arg3>
220 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
222 template <class Arg1, class Arg2, class Arg3, class Arg4>
223 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
227 } // namespace support