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(","));
179 /// Search \p search_token in \p str and return the position if it is
180 /// found, else -1. The last item in \p str must be "".
181 int findToken(char const * const str[], std::string const & search_token);
183 /// Convert internal line endings to line endings as expected by the OS
184 std::string const externalLineEnding(std::string const & str);
186 /// Convert line endings in any formnat to internal line endings
187 std::string const internalLineEnding(std::string const & str);
190 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
192 #include <boost/format.hpp>
195 string bformat(string const & fmt, Arg1 arg1)
197 return (boost::format(fmt) % arg1).str();
201 template<class Arg1, class Arg2>
202 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
204 return (boost::format(fmt) % arg1 % arg2).str();
208 template<class Arg1, class Arg2, class Arg3>
209 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
211 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
215 template<class Arg1, class Arg2, class Arg3, class Arg4>
216 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
218 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
223 template <class Arg1>
224 std::string bformat(std::string const & fmt, Arg1);
226 template <class Arg1, class Arg2>
227 std::string bformat(std::string const & fmt, Arg1, Arg2);
229 template <class Arg1, class Arg2, class Arg3>
230 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
232 template <class Arg1, class Arg2, class Arg3, class Arg4>
233 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
237 } // namespace support