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 /// changes the case only if c is a one-byte char
74 char_type lowercase(char_type c);
76 /// changes the case only if c is a one-byte char
77 char_type uppercase(char_type c);
79 /// same as lowercase(), but ignores locale
80 std::string const ascii_lowercase(std::string const &);
83 std::string const lowercase(std::string const &);
86 std::string const uppercase(std::string const &);
88 /// Does the std::string start with this prefix?
89 bool prefixIs(std::string const &, std::string const &);
91 /// Does the string end with this char?
92 bool suffixIs(std::string const &, char);
94 /// Does the std::string end with this suffix?
95 bool suffixIs(std::string const &, std::string const &);
99 bool contains(std::string const & a, B b)
101 return a.find(b) != std::string::npos;
104 template <typename B>
105 bool contains(lyx::docstring const & a, B b)
107 return a.find(b) != lyx::docstring::npos;
111 bool containsOnly(std::string const &, std::string const &);
113 /** Extracts a token from this string at the nth delim.
114 Doesn't modify the original string. Similar to strtok.
117 token("a;bc;d", ';', 1) == "bc";
118 token("a;bc;d", ';', 2) == "d";
121 std::string const token(std::string const & a, char delim, int n);
124 /** Search a token in this string using the delim.
125 Doesn't modify the original string. Returns -1 in case of
129 tokenPos("a;bc;d", ';', "bc") == 1;
130 tokenPos("a;bc;d", ';', "d") == 2;
133 int tokenPos(std::string const & a, char delim, std::string const & tok);
136 /// Substitute all \a oldchar with \a newchar
137 std::string const subst(std::string const & a, char oldchar, char newchar);
139 /// Substitute all \a oldchar with \a newchar
140 docstring const subst(docstring const & a,
141 char_type oldchar, char_type newchar);
143 /// substitutes all instances of \a oldstr with \a newstr
144 std::string const subst(std::string const & a,
145 std::string const & oldstr, std::string const & newstr);
147 /// substitutes all instances of \a oldstr with \a newstr
148 docstring const subst(docstring const & a,
149 docstring const & oldstr, docstring const & newstr);
151 /** Trims characters off the end and beginning of a string.
153 trim("ccabccc", "c") == "ab".
156 std::string const trim(std::string const & a, char const * p = " ");
158 /** Trims characters off the end of a string.
160 rtrim("abccc", "c") == "ab".
163 std::string const rtrim(std::string const & a, char const * p = " ");
165 /** Trims characters off the beginning of a string.
167 ltrim("ababcdef", "ab") = "cdef"
170 std::string const ltrim(std::string const & a, char const * p = " ");
172 /** Splits the string by the first delim.
173 Splits the string by the first appearance of delim.
174 The leading string up to delim is returned in piece (not including
175 delim), while the original string is cut from after the delimiter.
178 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
181 std::string const split(std::string const & a, std::string & piece, char delim);
183 /// Same as split but does not return a piece
184 std::string const split(std::string const & a, char delim);
186 /// Same as split but uses the last delim.
187 std::string const rsplit(std::string const & a, std::string & piece, char delim);
189 /// Escapes non ASCII chars
190 std::string const escape(std::string const & lab);
192 /// gives a vector of stringparts which have the delimiter delim
193 std::vector<std::string> const getVectorFromString(std::string const & str,
194 std::string const & delim = std::string(","));
196 // the same vice versa
197 std::string const getStringFromVector(std::vector<std::string> const & vec,
198 std::string const & delim = std::string(","));
200 /// Search \p search_token in \p str and return the position if it is
201 /// found, else -1. The last item in \p str must be "".
202 int findToken(char const * const str[], std::string const & search_token);
204 /// Convert internal line endings to line endings as expected by the OS
205 docstring const externalLineEnding(docstring const & str);
207 /// Convert line endings in any formnat to internal line endings
208 docstring const internalLineEnding(docstring const & str);
211 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
213 #include <boost/format.hpp>
216 string bformat(string const & fmt, Arg1 arg1)
218 return (boost::format(fmt) % arg1).str();
222 template<class Arg1, class Arg2>
223 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
225 return (boost::format(fmt) % arg1 % arg2).str();
229 template<class Arg1, class Arg2, class Arg3>
230 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
232 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
236 template<class Arg1, class Arg2, class Arg3, class Arg4>
237 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
239 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
244 template <class Arg1>
245 std::string bformat(std::string const & fmt, Arg1);
247 template <class Arg1, class Arg2>
248 std::string bformat(std::string const & fmt, Arg1, Arg2);
250 template <class Arg1, class Arg2, class Arg3>
251 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
253 template <class Arg1, class Arg2, class Arg3, class Arg4>
254 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
258 } // namespace support