Classes | Functions

claw::text Namespace Reference

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<class StringType >
void trim_left (StringType &str, const typename StringType::value_type *const s=" ")
 Remove characters at the begining of a string.
template<class StringType >
void trim_right (StringType &str, const typename StringType::value_type *const s=" ")
 Remove characters at the end of a string.
template<class 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<class StringType >
void squeeze (StringType &str, const typename StringType::value_type *const s)
 Squeeze successive characters of a string into one character.
template<typename T , class StringType >
bool is_of_type (const StringType &str)
 Test if the content of a string is immediately convertible to a type.
template<class BackInsertion , class StringType >
void split (BackInsertion &sequence, const StringType &str, const typename StringType::value_type sep)
 Split a string into several substrings, according to a given separator.

Detailed Description

Everything about text processing.


Function Documentation

template<typename StreamType , typename StringType >
StreamType & claw::text::getline ( StreamType &  is,
StringType &  str 
)

A portable version of std::getline( is, str, '
' ) that removes a tailing ''.

Parameters:
is The stream in which we read.
str The line read from the stream.

Definition at line 40 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()

template<typename T , class StringType >
bool claw::text::is_of_type ( const StringType &  str  ) 

Test if the content of a string is immediately convertible to a type.

Parameters:
str The string to test.

Definition at line 146 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()

template<class BackInsertion , class StringType >
void claw::text::split ( BackInsertion &  sequence,
const StringType &  str,
const typename StringType::value_type  sep 
)

Split a string into several substrings, according to a given separator.

Parameters:
sequence A back insertion sequence in which the substrings are added.
str The string to split.
sep The separator on which the string is splitted.

Definition at line 170 of file string_algorithm.tpp.

References getline().

{
  StringType line;
  std::basic_istringstream< typename StringType::value_type,
    typename StringType::traits_type,
    typename StringType::allocator_type > iss(str);

  while ( std::getline(iss, line, sep) )
    sequence.push_back(line);
} // split()

template<class StringType >
void claw::text::squeeze ( StringType &  str,
const typename StringType::value_type *const   s 
)

Squeeze successive characters of a string into one character.

Parameters:
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 114 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()

template<class StringType >
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.

Parameters:
str The string to modify.
s The characters to remove.

Definition at line 90 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()

template<class StringType >
void claw::text::trim_left ( StringType &  str,
const typename StringType::value_type *const   s = " " 
)

Remove characters at the begining of a string.

Parameters:
str The string to modify.
s The characters to remove.

Definition at line 58 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()

template<class StringType >
void claw::text::trim_right ( StringType &  str,
const typename StringType::value_type *const   s = " " 
)

Remove characters at the end of a string.

Parameters:
str The string to modify.
s The characters to remove.

Definition at line 74 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()