OpenWalnut  1.4.0
WStringUtils.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <string>
00027 #include <vector>
00028 
00029 #include <boost/algorithm/string.hpp>
00030 
00031 #include "WStringUtils.h"
00032 
00033 const std::string string_utils::WHITESPACE( "\r\n\t " ); //!< These characters are regarded as whitespaces
00034 
00035 std::string string_utils::rTrim( const std::string &source, const std::string& t )
00036 {
00037     std::string str = source;
00038     str.erase( str.find_last_not_of( t ) + 1 );
00039     return str;
00040 }
00041 
00042 std::string string_utils::lTrim( const std::string& source, const std::string& t )
00043 {
00044     std::string str = source;
00045     str.erase( 0 , source.find_first_not_of( t ) );
00046     return str;
00047 }
00048 
00049 std::string string_utils::trim( const std::string& source, const std::string& t )
00050 {
00051     std::string str = source;
00052     return lTrim( rTrim( str , t) , t );
00053 }
00054 
00055 std::string string_utils::toUpper( const std::string& source )
00056 {
00057     std::string str = source;
00058     std::transform( source.begin(), source.end(), str.begin(), ::toupper );
00059     return str;
00060 }
00061 
00062 std::string string_utils::toLower( const std::string& source )
00063 {
00064     std::string str = source;
00065     std::transform( source.begin(), source.end(), str.begin(), ::tolower );
00066     return str;
00067 }
00068 
00069 std::vector< std::string > string_utils::tokenize( const std::string& source,
00070                                                    const std::string& delim,
00071                                                    bool compress )
00072 {
00073     std::vector< std::string > result;
00074     namespace ba = boost::algorithm;
00075     ba::token_compress_mode_type compression = ba::token_compress_on;
00076     if( !compress )
00077     {
00078         compression = ba::token_compress_off;
00079     }
00080     ba::split( result, source, ba::is_any_of( delim ), compression );
00081     if( !result.empty() )
00082     {
00083         // NOTE: moved the back() command to another if as if compiled on Windows, OW crashes since the compiler does not stop evaluation of the
00084         // condition after the first statement evaluates to false.
00085 
00086         if( result.back() == "" )
00087         {
00088             result.pop_back();
00089         }
00090     }
00091     return result;
00092 }