summaryrefslogtreecommitdiff
path: root/src/stringhelper.cpp
blob: 2b4f783a79382c5baaeeb8210d5b05462eb7faaa (plain)
    1 ////////////////////////////////////////////////////////////////////////
    2 // FILE:        stringhelper.cpp
    3 // AUTHOR:      Johannes Winkelmann, jw@tks6.net
    4 // COPYRIGHT:   (c) 2002 by Johannes Winkelmann
    5 // ---------------------------------------------------------------------
    6 //  This program is free software; you can redistribute it and/or modify
    7 //  it under the terms of the GNU General Public License as published by
    8 //  the Free Software Foundation; either version 2 of the License, or
    9 //  (at your option) any later version.
   10 ////////////////////////////////////////////////////////////////////////
   11 
   12 #include "stringhelper.h"
   13 #include <cctype>
   14 
   15 using namespace std;
   16 
   17 namespace StringHelper
   18 {
   19 
   20 
   21 
   22 
   23 /*!
   24   \param s the string to be searched
   25   \param del the delimiter char
   26   \return the value after the first occurance of \a del
   27  */
   28 string getValue( const string& s, char del )
   29 {
   30     string::size_type pos = s.find( del );
   31     if ( pos != string::npos && pos+1 < s.length() ) {
   32         return s.substr( pos + 1 );
   33     }
   34     return "";
   35 }
   36 
   37 /*!
   38   \param s the string to be searched
   39   \param del the delimiter char
   40   \return the value before the first occurance of \a del
   41  */
   42 string getValueBefore( const string& s, char del )
   43 {
   44     string::size_type pos = s.find( del );
   45     if ( pos != string::npos ) {
   46         return s.substr( 0, pos );
   47     }
   48     return s;
   49 }
   50 
   51 /*!
   52   strip whitespace in the beginning and end of string \a s
   53   \return a stripped string
   54 */
   55 string stripWhiteSpace( const string& s )
   56 {
   57     if ( s.empty() ) {
   58         return s;
   59     }
   60 
   61     size_t pos = 0;
   62     string line = s;
   63     size_t len = line.length();
   64     while ( pos < len && isspace( line[pos] ) ) {
   65         ++pos;
   66     }
   67     line.erase( 0, pos );
   68     pos = line.length()-1;
   69     while ( pos != (size_t) -1 && isspace( line[pos] ) ) {
   70         --pos;
   71     }
   72     if ( pos != (size_t) -1 ) {
   73         line.erase( pos+1 );
   74     }
   75     return line;
   76 }
   77 
   78 /*!
   79   make sure s1 starts with s2
   80 */
   81 bool startsWith( const string& s, const string& with )
   82 {
   83     if (s.length() < with.length())
   84         return false;
   85  
   86     return s.substr(0, with.length()) == with;
   87 }
   88 
   89 /*!
   90   make sure s1 starts with s2
   91 */
   92 bool startsWithNoCase( const string& s1, const string& s2 )
   93 {
   94     string::const_iterator p1 = s1.begin();
   95     string::const_iterator p2 = s2.begin();
   96 
   97     while ( p1 != s1.end() && p2 != s2.end() ) {
   98         if ( toupper( *p1 ) != toupper( *p2 ) ) {
   99             return false;
  100         }
  101         ++p1;
  102         ++p2;
  103     }
  104 
  105     if ( p1 == s1.end() && p2 != s2.end() ) {
  106         return false;
  107     }
  108 
  109     return true;
  110 }
  111 
  112 /*!
  113   Convert a string into a lowercase representation
  114   \param s the string to be converted
  115   \return a lowercase representation of \a s
  116 */
  117 string toLowerCase( const string& s )
  118 {
  119     string result = "";
  120     for ( string::size_type i = 0; i < s.length(); ++i ) {
  121         result += tolower( s[i] );
  122     }
  123 
  124     return result;
  125 }
  126 
  127 /*!
  128   Convert a string into a uppercase representation
  129   \param s the string to be converted
  130   \return a uppercase representation of \a s
  131 */
  132 string toUpperCase( const string& s )
  133 {
  134     string result = "";
  135     for ( string::size_type i = 0; i < s.length(); ++i ) {
  136         result += toupper( s[i] );
  137     }
  138 
  139     return result;
  140 }
  141 
  142 /*!
  143   replace all occurances of \a oldString in \a in with \a newString
  144 */
  145 string replaceAll( string& in,
  146                    const string& oldString,
  147                    const string& newString )
  148 {
  149     size_t pos;
  150     while ( (pos = in.find( oldString )) != string::npos ) {
  151         in =
  152             in.replace( pos, oldString.length(), newString );
  153     }
  154 
  155     return in;
  156 }
  157 
  158 
  159 }; // Namespace

Generated by cgit