00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024
00025 #include <sstream>
00026 using std::ostringstream;
00027 #include <stdio.h>
00028
00029 #include "stringutils.h"
00030
00031 string StringUtils::replaceAll ( string haystack, string needle, string s )
00032 {
00033 string::size_type pos = 0;
00034 while ( ( pos = haystack.find ( needle, pos ) ) != string::npos )
00035 {
00036 haystack.erase ( pos, needle.length() );
00037 haystack.insert ( pos, s );
00038 pos += s.length();
00039 }
00040 return haystack;
00041 }
00042
00043 string StringUtils::stripWhite ( string s )
00044 {
00045 ostringstream dest;
00046 char c;
00047 for ( string::size_type pos = 0; pos < s.size(); ++pos )
00048 {
00049 c = s.at( pos );
00050 if ( c != 0x20 && c != 0x09 && c != 0x0d && c != 0x0a )
00051 dest << c;
00052 }
00053 return dest.str();
00054 }
00055
00056
00057 bool StringUtils::begins( string source, string sub )
00058 {
00059 return ( source.substr ( 0, sub.length() ) == sub );
00060 }
00061
00062 bool StringUtils::ends( string source, string sub )
00063 {
00064 if ( sub.length() >= source.length() )
00065 return false;
00066 return ( source.substr( source.length() - sub.length() ) == sub );
00067 }
00068
00069 string StringUtils::ltos( long num )
00070 {
00071 char s[ 81 ];
00072
00073 sprintf ( s, "%ld", num );
00074 return string( s );
00075 }
00076
00077 string StringUtils::itos( int num )
00078 {
00079 char s[ 81 ];
00080
00081 sprintf ( s, "%d", num );
00082 return string( s );
00083 }
00084
00090 int StringUtils::split( const string &input, const string &delimiter, vector< string > &items, const bool clean )
00091 {
00092 int delimiter_size = delimiter.size();
00093 int input_size = input.size();
00094
00095
00096 int position = 0;
00097 int end_position = input.find( delimiter, 0 );
00098
00099
00100 while ( end_position >= position )
00101 {
00102
00103 string s = input.substr( position, end_position - position );
00104 if ( !clean || s.size() > 0 )
00105 items.push_back( s );
00106
00107
00108 position = end_position + delimiter_size;
00109 end_position = input.find( delimiter, position );
00110 }
00111
00112
00113 string s = input.substr( position, input_size - position );
00114 if ( !clean || s.size() > 0 )
00115 items.push_back( s );
00116
00117
00118 return items.size();
00119 }
00120
00126 string StringUtils::join( vector< string >&items, const string &delimiter )
00127 {
00128 string output( "" );
00129
00130
00131 for ( vector< string >::iterator item = items.begin( ); item != items.end( ); item ++ )
00132 {
00133 if ( item == items.begin() )
00134 output += *item;
00135 else
00136 output += delimiter + *item;
00137 }
00138
00139 return string( output );
00140 }