1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/*
* stringutils.cc -- C++ STL string functions
* Copyright (C) 2003-2008 Dan Dennedy <dan@dennedy.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <algorithm>
#include <sstream>
using std::ostringstream;
#include <stdio.h>
#include "stringutils.h"
string StringUtils::replaceAll ( string haystack, string needle, string s )
{
string::size_type pos = 0;
while ( ( pos = haystack.find ( needle, pos ) ) != string::npos )
{
haystack.erase ( pos, needle.length() );
haystack.insert ( pos, s );
}
return haystack;
}
string StringUtils::stripWhite ( string s )
{
ostringstream dest;
char c;
for ( string::size_type pos = 0; pos < s.size(); ++pos )
{
c = s.at( pos );
if ( c != 0x20 && c != 0x09 && c != 0x0d && c != 0x0a )
dest << c;
}
dest << std::ends;
return dest.str();
}
bool StringUtils::begins( string source, string sub )
{
return ( source.substr ( 0, sub.length() ) == sub );
}
bool StringUtils::ends( string source, string sub )
{
if ( sub.length() >= source.length() )
return false;
return ( source.substr( source.length() - sub.length() ) == sub );
}
string StringUtils::ltos( long num )
{
char s[ 81 ];
sprintf ( s, "%ld", num );
return string( s );
}
string StringUtils::itos( int num )
{
char s[ 81 ];
sprintf ( s, "%d", num );
return string( s );
}
string StringUtils::toLower( string s )
{
transform( s.begin(), s.end(), s.begin(), (int(*)(int)) tolower );
return s;
}
string StringUtils::toUpper( string s )
{
transform( s.begin(), s.end(), s.begin(), (int(*)(int)) toupper );
return s;
}
|