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/docstring.h"
28 int compare_no_case(std::string const & s, std::string const & s2);
29 int compare_no_case(lyx::docstring const & s, lyx::docstring const & s2);
32 int compare_ascii_no_case(std::string const & s, std::string const & s2);
35 int compare_no_case(std::string const & s, std::string const & s2, unsigned int len);
39 int compare(char const * a, char const * b)
41 #ifndef CXX_GLOBAL_CSTD
42 return std::strcmp(a, b);
50 int compare(char const * a, char const * b, unsigned int len)
52 #ifndef CXX_GLOBAL_CSTD
53 return std::strncmp(a, b, len);
55 return strncmp(a, b, len);
60 bool isStrInt(std::string const & str);
62 /// does the std::string represent an unsigned integer value ?
63 bool isStrUnsignedInt(std::string const & str);
66 bool isStrDbl(std::string const & str);
69 char lowercase(char c);
72 char uppercase(char c);
74 /// changes the case only if c is a one-byte char
75 char_type lowercase(char_type c);
77 /// changes the case only if c is a one-byte char
78 char_type uppercase(char_type c);
80 /// same as lowercase(), but ignores locale
81 std::string const ascii_lowercase(std::string const &);
84 std::string const lowercase(std::string const &);
87 std::string const uppercase(std::string const &);
89 /// Does the std::string start with this prefix?
90 bool prefixIs(std::string const &, std::string const &);
92 /// Does the string end with this char?
93 bool suffixIs(std::string const &, char);
95 /// Does the std::string end with this suffix?
96 bool suffixIs(std::string const &, std::string const &);
100 bool contains(std::string const & a, B b)
102 return a.find(b) != std::string::npos;
105 template <typename B>
106 bool contains(lyx::docstring const & a, B b)
108 return a.find(b) != lyx::docstring::npos;
112 bool containsOnly(std::string const &, std::string const &);
114 /** Extracts a token from this string at the nth delim.
115 Doesn't modify the original string. Similar to strtok.
118 token("a;bc;d", ';', 1) == "bc";
119 token("a;bc;d", ';', 2) == "d";
122 std::string const token(std::string const & a, char delim, int n);
124 lyx::docstring const token(lyx::docstring const & a,
125 lyx::char_type delim, int n);
127 /** Search a token in this string using the delim.
128 Doesn't modify the original string. Returns -1 in case of
132 tokenPos("a;bc;d", ';', "bc") == 1;
133 tokenPos("a;bc;d", ';', "d") == 2;
136 int tokenPos(std::string const & a, char delim, std::string const & tok);
139 /// Substitute all \a oldchar with \a newchar
140 std::string const subst(std::string const & a, char oldchar, char newchar);
142 /// Substitute all \a oldchar with \a newchar
143 lyx::docstring const subst(docstring const & a,
144 lyx::char_type oldchar, lyx::char_type newchar);
146 /// substitutes all instances of \a oldstr with \a newstr
147 std::string const subst(std::string const & a,
148 std::string const & oldstr, std::string const & newstr);
150 /// substitutes all instances of \a oldstr with \a newstr
151 lyx::docstring const subst(lyx::docstring const & a,
152 lyx::docstring const & oldstr, lyx::docstring const & newstr);
154 /** Trims characters off the end and beginning of a string.
156 trim("ccabccc", "c") == "ab".
159 std::string const trim(std::string const & a, char const * p = " ");
161 /** Trims characters off the end of a string.
163 rtrim("abccc", "c") == "ab".
166 std::string const rtrim(std::string const & a, char const * p = " ");
168 /** Trims characters off the beginning of a string.
170 ltrim("ababcdef", "ab") = "cdef"
173 std::string const ltrim(std::string const & a, char const * p = " ");
175 /** Splits the string by the first delim.
176 Splits the string by the first appearance of delim.
177 The leading string up to delim is returned in piece (not including
178 delim), while the original string is cut from after the delimiter.
181 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
184 std::string const split(std::string const & a, std::string & piece, char delim);
186 /// Same as split but does not return a piece
187 std::string const split(std::string const & a, char delim);
189 /// Same as split but uses the last delim.
190 std::string const rsplit(std::string const & a, std::string & piece, char delim);
192 /// Escapes non ASCII chars
193 std::string const escape(std::string const & lab);
195 /// gives a vector of stringparts which have the delimiter delim
196 std::vector<std::string> const getVectorFromString(std::string const & str,
197 std::string const & delim = std::string(","));
199 // the same vice versa
200 std::string const getStringFromVector(std::vector<std::string> const & vec,
201 std::string const & delim = std::string(","));
203 /// Search \p search_token in \p str and return the position if it is
204 /// found, else -1. The last item in \p str must be "".
205 int findToken(char const * const str[], std::string const & search_token);
207 /// Convert internal line endings to line endings as expected by the OS
208 lyx::docstring const externalLineEnding(lyx::docstring const & str);
210 /// Convert line endings in any formnat to internal line endings
211 lyx::docstring const internalLineEnding(lyx::docstring const & str);
214 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
216 #include <boost/format.hpp>
219 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1)
221 return (boost::basic_format<char_type>(fmt) % arg1).str();
225 template<class Arg1, class Arg2>
226 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2)
228 return (boost::basic_format<char_type>(fmt) % arg1 % arg2).str();
232 template<class Arg1, class Arg2, class Arg3>
233 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
235 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3).str();
239 template<class Arg1, class Arg2, class Arg3, class Arg4>
240 lyx::docstring bformat(lyx::docstring const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
242 return (boost::basic_format<char_type>(fmt) % arg1 % arg2 % arg3 % arg4).str();
247 template <class Arg1>
248 lyx::docstring bformat(lyx::docstring const & fmt, Arg1);
250 template <class Arg1, class Arg2>
251 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2);
253 template <class Arg1, class Arg2, class Arg3>
254 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3);
256 template <class Arg1, class Arg2, class Arg3, class Arg4>
257 lyx::docstring bformat(lyx::docstring const & fmt, Arg1, Arg2, Arg3, Arg4);
261 } // namespace support