OpenWalnut  1.5.0dev
WStringUtils.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include "WStringUtils.h"
32 
33 const std::string string_utils::WHITESPACE( "\r\n\t " ); //!< These characters are regarded as whitespaces
34 
35 std::string string_utils::rTrim( const std::string &source, const std::string& t )
36 {
37  std::string str = source;
38  str.erase( str.find_last_not_of( t ) + 1 );
39  return str;
40 }
41 
42 std::string string_utils::lTrim( const std::string& source, const std::string& t )
43 {
44  std::string str = source;
45  str.erase( 0 , source.find_first_not_of( t ) );
46  return str;
47 }
48 
49 std::string string_utils::trim( const std::string& source, const std::string& t )
50 {
51  std::string str = source;
52  return lTrim( rTrim( str , t ) , t );
53 }
54 
55 std::string string_utils::toUpper( const std::string& source )
56 {
57  std::string str = source;
58  std::transform( source.begin(), source.end(), str.begin(), ::toupper );
59  return str;
60 }
61 
62 std::string string_utils::toLower( const std::string& source )
63 {
64  std::string str = source;
65  std::transform( source.begin(), source.end(), str.begin(), ::tolower );
66  return str;
67 }
68 
69 std::vector< std::string > string_utils::tokenize( const std::string& source,
70  const std::string& delim,
71  bool compress )
72 {
73  std::vector< std::string > result;
74  namespace ba = boost::algorithm;
75  ba::token_compress_mode_type compression = ba::token_compress_on;
76  if( !compress )
77  {
78  compression = ba::token_compress_off;
79  }
80  ba::split( result, source, ba::is_any_of( delim ), compression );
81  if( !result.empty() )
82  {
83  // NOTE: moved the back() command to another if as if compiled on Windows, OW crashes since the compiler does not stop evaluation of the
84  // condition after the first statement evaluates to false.
85 
86  if( result.back() == "" )
87  {
88  result.pop_back();
89  }
90  }
91  return result;
92 }
std::string toLower(const std::string &source)
Transforms all characters in the given string into lower case characters.
std::string toUpper(const std::string &source)
Transforms all characters in the given string into upper case characters.
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).
std::string trim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from both ends (right and left side) of th...
const std::string WHITESPACE
We consider the following characters as whitespace:
std::string rTrim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from the end (or right side) of the given ...
std::string lTrim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from the start (or left side) of the given...