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"
29 int compare_no_case(std::string const & s, std::string 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 /// same as lowercase(), but ignores locale
75 std::string const ascii_lowercase(std::string const &);
78 std::string const lowercase(std::string const &);
81 std::string const uppercase(std::string const &);
83 /// Does the std::string start with this prefix?
84 bool prefixIs(std::string const &, std::string const &);
86 /// Does the string end with this char?
87 bool suffixIs(std::string const &, char);
89 /// Does the std::string end with this suffix?
90 bool suffixIs(std::string const &, std::string const &);
94 bool contains(std::string const & a, B b)
96 return a.find(b) != std::string::npos;
100 bool containsOnly(std::string const &, std::string const &);
102 /** Extracts a token from this string at the nth delim.
103 Doesn't modify the original string. Similar to strtok.
106 token("a;bc;d", ';', 1) == "bc";
107 token("a;bc;d", ';', 2) == "d";
110 std::string const token(std::string const & a, char delim, int n);
113 /** Search a token in this string using the delim.
114 Doesn't modify the original string. Returns -1 in case of
118 tokenPos("a;bc;d", ';', "bc") == 1;
119 tokenPos("a;bc;d", ';', "d") == 2;
122 int tokenPos(std::string const & a, char delim, std::string const & tok);
125 /// Substitute all \a oldchar with \a newchar
126 std::string const subst(std::string const & a, char oldchar, char newchar);
128 /// substitutes all instances of \a oldstr with \a newstr
129 std::string const subst(std::string const & a,
130 std::string const & oldstr, std::string const & newstr);
132 /** Trims characters off the end and beginning of a string.
134 trim("ccabccc", "c") == "ab".
137 std::string const trim(std::string const & a, char const * p = " ");
139 /** Trims characters off the end of a string.
141 rtrim("abccc", "c") == "ab".
144 std::string const rtrim(std::string const & a, char const * p = " ");
146 /** Trims characters off the beginning of a string.
148 ltrim("ababcdef", "ab") = "cdef"
151 std::string const ltrim(std::string const & a, char const * p = " ");
153 /** Splits the string by the first delim.
154 Splits the string by the first appearance of delim.
155 The leading string up to delim is returned in piece (not including
156 delim), while the original string is cut from after the delimiter.
159 s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";
162 std::string const split(std::string const & a, std::string & piece, char delim);
164 /// Same as split but does not return a piece
165 std::string const split(std::string const & a, char delim);
167 /// Same as split but uses the last delim.
168 std::string const rsplit(std::string const & a, std::string & piece, char delim);
170 /// Escapes non ASCII chars
171 std::string const escape(std::string const & lab);
173 /// gives a vector of stringparts which have the delimiter delim
174 std::vector<std::string> const getVectorFromString(std::string const & str,
175 std::string const & delim = std::string(","));
177 // the same vice versa
178 std::string const getStringFromVector(std::vector<std::string> const & vec,
179 std::string const & delim = std::string(","));
181 /// Search \p search_token in \p str and return the position if it is
182 /// found, else -1. The last item in \p str must be "".
183 int findToken(char const * const str[], std::string const & search_token);
185 /// Convert internal line endings to line endings as expected by the OS
186 std::string const externalLineEnding(std::string const & str);
188 /// Convert line endings in any formnat to internal line endings
189 std::string const internalLineEnding(std::string const & str);
192 #ifdef I_AM_NOT_AFRAID_OF_HEADER_LIBRARIES
194 #include <boost/format.hpp>
197 string bformat(string const & fmt, Arg1 arg1)
199 return (boost::format(fmt) % arg1).str();
203 template<class Arg1, class Arg2>
204 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2)
206 return (boost::format(fmt) % arg1 % arg2).str();
210 template<class Arg1, class Arg2, class Arg3>
211 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3)
213 return (boost::format(fmt) % arg1 % arg2 % arg3).str();
217 template<class Arg1, class Arg2, class Arg3, class Arg4>
218 string bformat(string const & fmt, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
220 return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
225 template <class Arg1>
226 std::string bformat(std::string const & fmt, Arg1);
228 template <class Arg1, class Arg2>
229 std::string bformat(std::string const & fmt, Arg1, Arg2);
231 template <class Arg1, class Arg2, class Arg3>
232 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3);
234 template <class Arg1, class Arg2, class Arg3, class Arg4>
235 std::string bformat(std::string const & fmt, Arg1, Arg2, Arg3, Arg4);
239 } // namespace support