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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
Copyright 2007 - 2008 MySQL AB, 2008 - 2010 Sun Microsystems, Inc. All rights reserved.
The MySQL Connector/C++ is licensed under the terms of the GPL
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
MySQL Connectors. There are special exceptions to the terms and
conditions of the GPL as it is applied to this software, see the
FLOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
*/
#include "stringutils.h"
#include <stdlib.h>
#include <stdio.h>
namespace StringUtils
{
template <class StdStringType>
StdStringType & _trim( StdStringType & victim )
{
static const String::value_type * space = _T(" ");
String::size_type begin = victim.find_first_not_of( space );
String::size_type end = victim.find_last_not_of( space );
if ( begin == String::npos )
begin = 0;
if ( end == String::npos )
end = victim.size();
else
++end;
return victim.assign( victim, begin, end - begin );
}
template <class StdStringType>
unsigned _split( std::vector<StdStringType> & list, const StdStringType & str, const StdStringType & delim
, bool trimItems, bool includeEmpty )
{
unsigned int count= 0;
String::size_type prevPos= 0;
String::size_type newPos= 0;
String::size_type delimSize= delim.size();
String::size_type strSize= str.size();
StdStringType item;
while ( prevPos < strSize && (newPos = str.find(delim, prevPos)) != String::npos )
{
if ( newPos == prevPos && delimSize == 0 )
++newPos;
if ( newPos > prevPos || includeEmpty )
{
item = str.substr(prevPos, newPos - prevPos );
if ( trimItems )
_trim<StdStringType>( item );
list.push_back( item );
++count;
}
prevPos = newPos + delimSize;
}
/* if string has delimiter at the end, and includeEmpty flag is set
- we are doing that if delimiter is present (delimSize > 0) */
if ( prevPos == strSize && includeEmpty && delimSize > 0 )
{
list.push_back( _T("") );
++count;
}
/* else - adding part after last token*/
else if (prevPos < strSize )
{
item = str.substr(prevPos);
if (trimItems)
_trim<StdStringType>(item);
list.push_back( item );
++count;
}
return count;
}
// This function skips empties
unsigned split( List & list, const String & str, const String & delim
, bool trimItems, bool includeEmpty)
{
return _split<String>( list, str, delim, trimItems, includeEmpty );
}
// hack for split template
//ciString trim ( const String & victim )
unsigned split( std::vector<ciString> & list, const ciString & str, const ciString & delim
, bool trimItems, bool includeEmpty)
{
return _split<ciString>( list, str, delim, trimItems, includeEmpty );
}
String & trim( String & victim )
{
return _trim<String>( victim );
}
int toInt( const String & str, bool isNull )
{
if ( isNull )
return 0;
return atoi( str.c_str() );
}
bool toBoolean ( const String & str, bool isNull )
{
return toInt( str, isNull ) != 0;
}
long long toLong( const String & str, bool isNull )
{
if (isNull)
return 0L;
return strtoll(str.c_str(), NULL, 10);
}
float toFloat( const String & str, bool isNull )
{
return static_cast<float>( toDouble( str.c_str() ) );
}
double toDouble( const String & str, bool isNull )
{
if (isNull)
return 0.0;
return atof(str.c_str());
}
const String & defaultIfEmpty ( const String & str, const String & defStr )
{
if ( str.empty() )
return defStr;
else
return str;
}
String toHexString( char c, bool leading0x )
{
String result;
if ( leading0x )
result= "0x";
char buf[3];
sprintf( buf, "%02x", c & 0xff );
result.append( buf );
return result;
}
String::size_type toHexString( String & buff, char c[], int count, bool leading0x )
{
while( --count >= 0 )
{
buff+= toHexString( *c, leading0x );
if ( count )
{
buff+= " ";
++c;
}
}
return buff.size();
}
String & concatSeparated( String & to, const String & add, const String & separator )
{
if ( to.length() > 0 && add.length() > 0 )
to+= separator;
to+= add;
return to;
}
}
|