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.
19 #include "support/types.h"
28 int compare_no_case(std::string const & s, std::string const & s2);
31 int compare_ascii_no_case(std::string const & s, std::string const & s2);
34 int compare_no_case(std::string const & s, std::string const & s2, unsigned int len);
38 int compare(char const * a, char const * b)
40 #ifndef CXX_GLOBAL_CSTD
41 return std::strcmp(a, b);
49 int compare(char const * a, char const * b, unsigned int len)
51 #ifndef CXX_GLOBAL_CSTD
52 return std::strncmp(a, b, len);
54 return strncmp(a, b, len);
59 bool isStrInt(std::string const & str);
61 /// does the std::string represent an unsigned integer value ?
62 bool isStrUnsignedInt(std::string const & str);
65 bool isStrDbl(std::string const & str);
68 char lowercase(char c);
71 char uppercase(char c);
73 /// same as lowercase(), but ignores locale
74 std::string const ascii_lowercase(std::string const &);
77 std::string const lowercase(std::string const &);
80 std::string const uppercase(std::string const &);
82 /// Does the std::string start with this prefix?
83 bool prefixIs(std::string const &, std::string const &);
85 /// Does the string end with this char?
86 bool suffixIs(std::string const &, char);
88 /// Does the std::string end with this suffix?
89 bool suffixIs(std::string const &, std::string const &);
93 bool contains(std::string const & a, B b)
95 return a.find(b) != std::string::npos;
99 bool contains(lyx::docstring const & a, B b)
101 return a.find(b) != lyx::docstring::npos;
105 bool containsOnly(std::string const &, std::string const &);
107 /** Extracts a token from this string at the nth delim.
108 Doesn't modify the original string. Similar to strtok.
111 token("a;bc;d", ';', 1) == "bc";
112 token("a;bc;d", ';', 2) == "d";
115 std::string const token(std::string const & a, char delim, int n);
118 /** Search a token in this string using the delim.
119 Doesn't modify the original string. Returns -1 in case of
123 tokenPos("a;bc;d", ';', "bc") == 1;
124 tokenPos("a;bc;d", ';', "d") == 2;
127 int tokenPos(std::string const & a, char delim, std::string const & tok);
130 /// Substitute all \a oldchar with \a newchar
131 std::string const subst(std::string const & a, char oldchar, char newchar);
133 /// Substitute all \a oldchar with \a newchar
134 docstring const subst(docstring const & a,
135 char_type oldchar, char_type newchar);
137 /// substitutes all instances of \a oldstr with \a newstr
138 std::string const subst(std::string const & a,
139 std::string const & oldstr, std::string const & newstr);
141 /// substitutes all instances of \a oldstr with \a newstr
142 docstring const subst(docstring const & a,
143 docstring const & oldstr, docstring const & newstr);
145 /** Trims characters off the end and beginning of a string.
147 trim("ccabccc", "c") == "ab".
150 std::string const trim(std::string const & a, char const * p = " ");
152 /** Trims characters off the end of a string.
154 rtrim("abccc", "c") == "ab".
157 std::string const rtrim(std::string const & a, char const * p = " ");
159 /** Trims characters off the beginning of a string.
161 ltrim("ababcdef", "ab") = "cdef"
164 std::string const ltrim(std::string const & a, char const * p = " ");
166 /** Splits the string by the first delim.
167 Splits the string by the first appearance of delim.
168 The leading string up to delim is returned in piece (not including
169 delim), while the original string is cut from after the delimiter.
172 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
175 std::string const split(std::string const & a, std::string & piece, char delim);
177 /// Same as split but does not return a piece
178 std::string const split(std::string const & a, char delim);
180 /// Same as split but uses the last delim.
181 std::string const rsplit(std::string const & a, std::string & piece, char delim);
183 /// Escapes non ASCII chars
184 std::string const escape(std::string const & lab);
186 /// gives a vector of stringparts which have the delimiter delim
187 std::vector<std::string> const getVectorFromString(std::string const & str,
188 std::string const & delim = std::string(","));
190 // the same vice versa
191 std::string const getStringFromVector(std::vector<std::string> const & vec,
192 std::string const & delim = std::string(","));
194 /// Search \p search_token in \p str and return the position if it is
195 /// found, else -1. The last item in \p str must be "".
196 int findToken(char const * const str[], std::string const & search_token);
198 /// Convert internal line endings to line endings as expected by the OS
199 docstring const externalLineEnding(docstring const & str);
201 /// Convert line endings in any formnat to internal line endings
202 docstring const internalLineEnding(docstring const & str);
205 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
207 #include <boost/format.hpp>
210 string bformat(string const & fmt, Arg1 arg1)
212 return (boost::format(fmt) % arg1).str();
216 template<class Arg1, class Arg2>
217 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
219 return (boost::format(fmt) % arg1 % arg2).str();
223 template<class Arg1, class Arg2, class Arg3>
224 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
226 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
230 template<class Arg1, class Arg2, class Arg3, class Arg4>
231 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
233 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
238 template <class Arg1>
239 std::string bformat(std::string const & fmt, Arg1);
241 template <class Arg1, class Arg2>
242 std::string bformat(std::string const & fmt, Arg1, Arg2);
244 template <class Arg1, class Arg2, class Arg3>
245 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
247 template <class Arg1, class Arg2, class Arg3, class Arg4>
248 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
252 } // namespace support