140
146
/*----------------------------------------------------------------------------*/
148
* \brief Replace a set of characters by other characters.
149
* \param str The string to modify.
150
* \param e1 The characters to remove.
151
* \param e2 The characters replacing the ones in \a e1.
153
* \return The number of replaced characters.
155
* Each character e1[i] will be replaced with e2[i]. If \a e1 is smaller than
156
* \a e2, the latter will be completed by repeating its last character.
160
* std::string s("word aaa bbb abab");
161
* claw::replace( s, "ab", "ba" );
162
* std::cout << s << std::end; // result is "word bbb aaa baba"
165
template<typename StringType>
166
std::size_t claw::text::replace
167
( StringType& str, const StringType& e1, const StringType& e2 )
171
( str.begin(), str.end(), e1.begin(), e1.end(), e2.begin(), e2.end() );
174
/*----------------------------------------------------------------------------*/
142
176
* \brief Test if the content of a string is immediately convertible to a type.
143
177
* \param str The string to test.
145
template<typename T, class StringType>
179
template<typename T, typename StringType>
146
180
bool claw::text::is_of_type( const StringType& str )
148
182
std::basic_istringstream< typename StringType::value_type,
163
197
* \brief Split a string into several substrings, according to a given
165
* \param sequence A back insertion sequence in which the substrings are added.
199
* \param sequence A sequence in which the substrings are added.
166
200
* \param str The string to split.
167
201
* \param sep The separator on which the string is splitted.
169
template<class BackInsertion, class StringType>
170
void claw::text::split( BackInsertion& sequence, const StringType& str,
171
const typename StringType::value_type sep )
174
std::basic_istringstream< typename StringType::value_type,
175
typename StringType::traits_type,
176
typename StringType::allocator_type > iss(str);
203
template<typename Sequence>
204
void claw::text::split
205
( Sequence& sequence, const typename Sequence::value_type& str,
206
const typename Sequence::value_type::value_type sep )
208
split(sequence, str.begin(), str.end(), sep);
211
/*----------------------------------------------------------------------------*/
213
* \brief Split a string into several substrings, according to a given
215
* \param sequence A sequence in which the substrings are added.
216
* \param first Iterator on the beginning of the string to split.
217
* \param last Iterator just past the end of the string to split.
218
* \param sep The separator on which the string is splitted.
220
template<typename Sequence>
221
void claw::text::split
222
( Sequence& sequence, typename Sequence::value_type::const_iterator first,
223
typename Sequence::value_type::const_iterator last,
224
const typename Sequence::value_type::value_type sep )
226
typedef typename Sequence::value_type string_type;
229
std::basic_istringstream< typename string_type::value_type,
230
typename string_type::traits_type,
231
typename string_type::allocator_type > iss( string_type(first, last) );
178
233
while ( std::getline(iss, line, sep) )
179
sequence.push_back(line);
234
*std::insert_iterator<Sequence>(sequence, sequence.end()) = line;
237
/*----------------------------------------------------------------------------*/
239
* \brief Find escaped symbols in a sequence of characters and replace them by
240
* their c-equivalent.
242
* \param first Iterator on the beginning of the string to escape.
243
* \param last Iterator just past the end of the string to escape.
244
* \param out Iterator on the beginning of the output string.
245
* \pre \a out points on a range long enough to store the resulting string.
248
* std::string s("\\a\\t\\n\\r");
251
* claw::text::c_escape( s.begin(), s.end(), std::insert_iterator(r, r.end()) );
253
* if ( r == "\a\t\n\r" )
254
* std::cout << "It works!" << std::endl;
257
* \remark This method has not been tested with wide chars yet.
259
template<typename InputIterator, typename OutputIterator>
260
void claw::text::c_escape
261
( InputIterator first, InputIterator last, OutputIterator out )
263
typedef typename std::iterator_traits<InputIterator>::value_type char_type;
264
typedef std::basic_string<char_type> string_type;
266
const string_type oct("01234567");
267
const string_type hex("0123456789ABCDEFabcdef");
271
for ( ; first!=last; ++out )
276
case 'a': *out = '\a'; ++first; break;
277
case 'b': *out = '\b'; ++first; break;
278
case 'f': *out = '\f'; ++first; break;
279
case 'n': *out = '\n'; ++first; break;
280
case 'r': *out = '\r'; ++first; break;
281
case 't': *out = '\t'; ++first; break;
282
case 'v': *out = '\v'; ++first; break;
287
const InputIterator e
288
( find_first_not_of(first, last, oct.begin(), oct.end()) );
290
std::basic_istringstream<char_type> iss( string_type(first, e) );
291
iss >> std::oct >> v;
300
const InputIterator e
301
( find_first_not_of(first, last, hex.begin(), hex.end()) );
303
std::basic_istringstream<char_type> iss( string_type(first, e) );
304
iss >> std::hex >> v;
309
default: *out = *first; ++first;
314
else if ( *first == '\\' )
326
/*----------------------------------------------------------------------------*/
328
* \brief Check if a string matches a given pattern.
329
* \param pattern The pattern.
330
* \param text The text to check.
331
* \param any_sequence A value representing any sequence of values, empty or
333
* \param zero_or_one A value representing any value or no value.
334
* \param any A value representing any value.
336
template<typename StringType>
337
bool claw::text::glob_match
338
( const StringType& pattern, const StringType& text,
339
const typename StringType::value_type any_sequence,
340
const typename StringType::value_type zero_or_one,
341
const typename StringType::value_type any )
343
return claw::glob_match
344
( pattern.begin(), pattern.end(), text.begin(), text.end(), any_sequence,
348
/*----------------------------------------------------------------------------*/
350
* \brief Check if a string may match a given pattern.
351
* \param pattern The pattern.
352
* \param text The text to check.
353
* \param any_sequence A value representing any sequence of values, empty or
355
* \param zero_or_one A value representing any value or no value.
356
* \param any A value representing any value.
358
template<typename StringType>
359
bool claw::text::glob_potential_match
360
( const StringType& pattern, const StringType& text,
361
const typename StringType::value_type any_sequence,
362
const typename StringType::value_type zero_or_one,
363
const typename StringType::value_type any )
365
return claw::glob_potential_match
366
( pattern.begin(), pattern.end(), text.begin(), text.end(), any_sequence,
368
} // glob_potential_match()