Everything about text processing. More...
Classes | |
class | kmp |
Exact pattern finding with the Knuth-Morris-Pratt's algorithm. More... | |
Functions | |
template<typename StreamType , typename StringType > | |
StreamType & | getline (StreamType &is, StringType &str) |
A portable version of std::getline( is, str, ' ' ) that removes a tailing ''. | |
template<typename StringType > | |
void | trim_left (StringType &str, const typename StringType::value_type *const s=" ") |
Remove characters at the begining of a string. | |
template<typename StringType > | |
void | trim_right (StringType &str, const typename StringType::value_type *const s=" ") |
Remove characters at the end of a string. | |
template<typename StringType > | |
void | trim (StringType &str, const typename StringType::value_type *const s=" ") |
Remove characters at the begining end at the end of a string. | |
template<typename StringType > | |
void | squeeze (StringType &str, const typename StringType::value_type *const s) |
Squeeze successive characters of a string into one character. | |
template<typename StringType > | |
std::size_t | replace (StringType &str, const StringType &e1, const StringType &e2) |
Replace a set of characters by other characters. | |
template<typename T , typename StringType > | |
bool | is_of_type (const StringType &str) |
Test if the content of a string is immediately convertible to a type. | |
template<typename Sequence > | |
void | split (Sequence &sequence, const typename Sequence::value_type &str, const typename Sequence::value_type::value_type sep) |
Split a string into several substrings, according to a given separator. | |
template<typename Sequence > | |
void | split (Sequence &sequence, typename Sequence::value_type::const_iterator first, typename Sequence::value_type::const_iterator last, const typename Sequence::value_type::value_type sep) |
Split a string into several substrings, according to a given separator. | |
template<typename InputIterator , typename OutputIterator > | |
void | c_escape (InputIterator first, InputIterator last, OutputIterator out) |
Find escaped symbols in a sequence of characters and replace them by their c-equivalent. | |
template<typename StringType > | |
bool | glob_match (const StringType &pattern, const StringType &text, const typename StringType::value_type any_sequence= '*', const typename StringType::value_type zero_or_one= '?', const typename StringType::value_type any= '.') |
Check if a string matches a given pattern. | |
template<typename StringType > | |
bool | glob_potential_match (const StringType &pattern, const StringType &text, const typename StringType::value_type any_sequence= '*', const typename StringType::value_type zero_or_one= '?', const typename StringType::value_type any= '.') |
Check if a string may match a given pattern. |
Everything about text processing.
void claw::text::c_escape | ( | InputIterator | first, |
InputIterator | last, | ||
OutputIterator | out | ||
) |
Find escaped symbols in a sequence of characters and replace them by their c-equivalent.
first | Iterator on the beginning of the string to escape. |
last | Iterator just past the end of the string to escape. |
out | Iterator on the beginning of the output string. |
std::string s("\\a\\t\\n\\r"); std::string r; claw::text::c_escape( s.begin(), s.end(), std::insert_iterator(r, r.end()) ); if ( r == "\a\t\n\r" ) std::cout << "It works!" << std::endl;
Definition at line 261 of file string_algorithm.tpp.
References claw::find_first_not_of().
{ typedef typename std::iterator_traits<InputIterator>::value_type char_type; typedef std::basic_string<char_type> string_type; const string_type oct("01234567"); const string_type hex("0123456789ABCDEFabcdef"); bool escape(false); for ( ; first!=last; ++out ) if ( escape ) { switch( *first ) { case 'a': *out = '\a'; ++first; break; case 'b': *out = '\b'; ++first; break; case 'f': *out = '\f'; ++first; break; case 'n': *out = '\n'; ++first; break; case 'r': *out = '\r'; ++first; break; case 't': *out = '\t'; ++first; break; case 'v': *out = '\v'; ++first; break; case 'o': { ++first; int v(0); const InputIterator e ( find_first_not_of(first, last, oct.begin(), oct.end()) ); std::basic_istringstream<char_type> iss( string_type(first, e) ); iss >> std::oct >> v; *out = (char_type)v; first = e; break; } case 'x': { ++first; int v(0); const InputIterator e ( find_first_not_of(first, last, hex.begin(), hex.end()) ); std::basic_istringstream<char_type> iss( string_type(first, e) ); iss >> std::hex >> v; *out = (char_type)v; first = e; break; } default: *out = *first; ++first; } escape = false; } else if ( *first == '\\' ) { escape = true; ++first; } else { *out = *first; ++first; } } // c_escape()
StreamType & claw::text::getline | ( | StreamType & | is, |
StringType & | str | ||
) |
A portable version of std::getline( is, str, '
' ) that removes a tailing ''.
is | The stream in which we read. |
str | The line read from the stream. |
Definition at line 46 of file string_algorithm.tpp.
Referenced by claw::configuration_file::get_line(), claw::graphic::xbm::reader::read_line(), claw::graphic::xbm::reader::remove_comments(), and split().
{ std::getline( is, str ); if ( !str.empty() ) if ( str[ str.size() - 1 ] == typename StringType::value_type('\r') ) str.erase( str.size() - 1 ); return is; } // getline()
bool claw::text::glob_match | ( | const StringType & | pattern, |
const StringType & | text, | ||
const typename StringType::value_type | any_sequence = '*' , |
||
const typename StringType::value_type | zero_or_one = '?' , |
||
const typename StringType::value_type | any = '.' |
||
) |
Check if a string matches a given pattern.
pattern | The pattern. |
text | The text to check. |
any_sequence | A value representing any sequence of values, empty or not. |
zero_or_one | A value representing any value or no value. |
any | A value representing any value. |
Definition at line 338 of file string_algorithm.tpp.
References claw::glob_match().
{ return claw::glob_match ( pattern.begin(), pattern.end(), text.begin(), text.end(), any_sequence, zero_or_one, any ); } // glob_match()
bool claw::text::glob_potential_match | ( | const StringType & | pattern, |
const StringType & | text, | ||
const typename StringType::value_type | any_sequence = '*' , |
||
const typename StringType::value_type | zero_or_one = '?' , |
||
const typename StringType::value_type | any = '.' |
||
) |
Check if a string may match a given pattern.
pattern | The pattern. |
text | The text to check. |
any_sequence | A value representing any sequence of values, empty or not. |
zero_or_one | A value representing any value or no value. |
any | A value representing any value. |
Definition at line 360 of file string_algorithm.tpp.
References claw::glob_potential_match().
{ return claw::glob_potential_match ( pattern.begin(), pattern.end(), text.begin(), text.end(), any_sequence, zero_or_one, any ); } // glob_potential_match()
bool claw::text::is_of_type | ( | const StringType & | str ) |
Test if the content of a string is immediately convertible to a type.
str | The string to test. |
Definition at line 180 of file string_algorithm.tpp.
{ std::basic_istringstream< typename StringType::value_type, typename StringType::traits_type, typename StringType::allocator_type > iss(str); T val; bool result = false; if ( iss >> val ) result = iss.eof(); return result; } // is_of_type()
std::size_t claw::text::replace | ( | StringType & | str, |
const StringType & | e1, | ||
const StringType & | e2 | ||
) |
Replace a set of characters by other characters.
str | The string to modify. |
e1 | The characters to remove. |
e2 | The characters replacing the ones in e1. |
Each character e1[i] will be replaced with e2[i]. If e1 is smaller than e2, the latter will be completed by repeating its last character.
Example : std::string s("word aaa bbb abab"); claw::replace( s, "ab", "ba" ); std::cout << s << std::end; // result is "word bbb aaa baba"
Definition at line 167 of file string_algorithm.tpp.
References claw::replace().
{ return claw::replace ( str.begin(), str.end(), e1.begin(), e1.end(), e2.begin(), e2.end() ); } // replace()
void claw::text::split | ( | Sequence & | sequence, |
typename Sequence::value_type::const_iterator | first, | ||
typename Sequence::value_type::const_iterator | last, | ||
const typename Sequence::value_type::value_type | sep | ||
) |
Split a string into several substrings, according to a given separator.
sequence | A sequence in which the substrings are added. |
first | Iterator on the beginning of the string to split. |
last | Iterator just past the end of the string to split. |
sep | The separator on which the string is splitted. |
Definition at line 222 of file string_algorithm.tpp.
References getline().
{ typedef typename Sequence::value_type string_type; string_type line; std::basic_istringstream< typename string_type::value_type, typename string_type::traits_type, typename string_type::allocator_type > iss( string_type(first, last) ); while ( std::getline(iss, line, sep) ) *std::insert_iterator<Sequence>(sequence, sequence.end()) = line; } // split()
void claw::text::split | ( | Sequence & | sequence, |
const typename Sequence::value_type & | str, | ||
const typename Sequence::value_type::value_type | sep | ||
) |
Split a string into several substrings, according to a given separator.
sequence | A sequence in which the substrings are added. |
str | The string to split. |
sep | The separator on which the string is splitted. |
Definition at line 205 of file string_algorithm.tpp.
{ split(sequence, str.begin(), str.end(), sep); } // split()
void claw::text::squeeze | ( | StringType & | str, |
const typename StringType::value_type *const | s | ||
) |
Squeeze successive characters of a string into one character.
str | The string to modify. |
s | The characters to remove. |
Example : std::string s("word aaa bbb abab"); claw::squeeze( s, "ab" ); std::cout << s << std::end; // result is "word a b abab"
Definition at line 120 of file string_algorithm.tpp.
{ typedef typename StringType::size_type size_type; size_type first(0); do { first = str.find_first_of(s, first); if ( first != StringType::npos ) { size_type last = str.find_first_not_of(str[first], first+1); if ( last == StringType::npos ) str = str.substr(0, first+1); else if ( last - first > 1 ) str = str.substr(0, first+1) + str.substr(last); ++first; } } while ( (first != StringType::npos) && (first != str.length()) ); } // squeeze()
void claw::text::trim | ( | StringType & | str, |
const typename StringType::value_type *const | s = " " |
||
) |
Remove characters at the begining end at the end of a string.
str | The string to modify. |
s | The characters to remove. |
Definition at line 96 of file string_algorithm.tpp.
Referenced by claw::configuration_file::process_line(), claw::graphic::xbm::reader::read_line(), and claw::graphic::xbm::reader::remove_comments().
{ typename StringType::size_type first = str.find_first_not_of(s); typename StringType::size_type last = str.find_last_not_of(s); if (first != StringType::npos) str = str.substr( first, last - first + 1 ); } // trim()
void claw::text::trim_left | ( | StringType & | str, |
const typename StringType::value_type *const | s = " " |
||
) |
Remove characters at the begining of a string.
str | The string to modify. |
s | The characters to remove. |
Definition at line 64 of file string_algorithm.tpp.
Referenced by claw::configuration_file::get_line().
{ typename StringType::size_type p = str.find_first_not_of(s); if (p != StringType::npos) str = str.substr(p); } // trim_left()
void claw::text::trim_right | ( | StringType & | str, |
const typename StringType::value_type *const | s = " " |
||
) |
Remove characters at the end of a string.
str | The string to modify. |
s | The characters to remove. |
Definition at line 80 of file string_algorithm.tpp.
Referenced by claw::configuration_file::open().
{ typename StringType::size_type p = str.find_last_not_of(s); if (p != StringType::npos) str = str.substr( 0, p+1 ); } // trim_right()