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 /// substitutes all instances of \a oldstr with \a newstr
134 std::string const subst(std::string const & a,
135 std::string const & oldstr, std::string const & newstr);
137 /** Trims characters off the end and beginning of a string.
139 trim("ccabccc", "c") == "ab".
142 std::string const trim(std::string const & a, char const * p = " ");
144 /** Trims characters off the end of a string.
146 rtrim("abccc", "c") == "ab".
149 std::string const rtrim(std::string const & a, char const * p = " ");
151 /** Trims characters off the beginning of a string.
153 ltrim("ababcdef", "ab") = "cdef"
156 std::string const ltrim(std::string const & a, char const * p = " ");
158 /** Splits the string by the first delim.
159 Splits the string by the first appearance of delim.
160 The leading string up to delim is returned in piece (not including
161 delim), while the original string is cut from after the delimiter.
164 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
167 std::string const split(std::string const & a, std::string & piece, char delim);
169 /// Same as split but does not return a piece
170 std::string const split(std::string const & a, char delim);
172 /// Same as split but uses the last delim.
173 std::string const rsplit(std::string const & a, std::string & piece, char delim);
175 /// Escapes non ASCII chars
176 std::string const escape(std::string const & lab);
178 /// gives a vector of stringparts which have the delimiter delim
179 std::vector<std::string> const getVectorFromString(std::string const & str,
180 std::string const & delim = std::string(","));
182 // the same vice versa
183 std::string const getStringFromVector(std::vector<std::string> const & vec,
184 std::string const & delim = std::string(","));
186 /// Search \p search_token in \p str and return the position if it is
187 /// found, else -1. The last item in \p str must be "".
188 int findToken(char const * const str[], std::string const & search_token);
190 /// Convert internal line endings to line endings as expected by the OS
191 std::string const externalLineEnding(std::string const & str);
193 /// Convert line endings in any formnat to internal line endings
194 std::string const internalLineEnding(std::string const & str);
197 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
199 #include <boost/format.hpp>
202 string bformat(string const & fmt, Arg1 arg1)
204 return (boost::format(fmt) % arg1).str();
208 template<class Arg1, class Arg2>
209 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
211 return (boost::format(fmt) % arg1 % arg2).str();
215 template<class Arg1, class Arg2, class Arg3>
216 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
218 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
222 template<class Arg1, class Arg2, class Arg3, class Arg4>
223 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
225 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
230 template <class Arg1>
231 std::string bformat(std::string const & fmt, Arg1);
233 template <class Arg1, class Arg2>
234 std::string bformat(std::string const & fmt, Arg1, Arg2);
236 template <class Arg1, class Arg2, class Arg3>
237 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
239 template <class Arg1, class Arg2, class Arg3, class Arg4>
240 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
244 } // namespace support