00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #pragma once
00034
00035 namespace mongo {
00036
00037 class StringSplitter {
00038 public:
00042 StringSplitter( const char * big , const char * splitter )
00043 : _big( big ) , _splitter( splitter ) {
00044 }
00045
00047 bool more() {
00048 return _big[0];
00049 }
00050
00052 string next() {
00053 const char * foo = strstr( _big , _splitter );
00054 if ( foo ) {
00055 string s( _big , foo - _big );
00056 _big = foo + 1;
00057 while ( *_big && strstr( _big , _splitter ) == _big )
00058 _big++;
00059 return s;
00060 }
00061
00062 string s = _big;
00063 _big += strlen( _big );
00064 return s;
00065 }
00066
00067 void split( vector<string>& l ) {
00068 while ( more() ) {
00069 l.push_back( next() );
00070 }
00071 }
00072
00073 vector<string> split() {
00074 vector<string> l;
00075 split( l );
00076 return l;
00077 }
00078
00079 static vector<string> split( const string& big , const string& splitter ) {
00080 StringSplitter ss( big.c_str() , splitter.c_str() );
00081 return ss.split();
00082 }
00083
00084 static string join( vector<string>& l , const string& split ) {
00085 stringstream ss;
00086 for ( unsigned i=0; i<l.size(); i++ ) {
00087 if ( i > 0 )
00088 ss << split;
00089 ss << l[i];
00090 }
00091 return ss.str();
00092 }
00093
00094 private:
00095 const char * _big;
00096 const char * _splitter;
00097 };
00098
00099
00100
00101
00102
00103 bool isValidUTF8(const char *s);
00104 inline bool isValidUTF8(string s) { return isValidUTF8(s.c_str()); }
00105
00106 #if defined(_WIN32)
00107
00108 std::string toUtf8String(const std::wstring& wide);
00109
00110 std::wstring toWideString(const char *s);
00111
00112
00113 # if !defined(_UNICODE)
00114 #error temp error
00115 inline std::string toNativeString(const char *s) { return s; }
00116 # else
00117 inline std::wstring toNativeString(const char *s) { return toWideString(s); }
00118 # endif
00119
00120 #endif
00121
00122
00123
00124 inline long long parseLL( const char *n ) {
00125 long long ret;
00126 uassert( 13307, "cannot convert empty string to long long", *n != 0 );
00127 #if !defined(_WIN32)
00128 char *endPtr = 0;
00129 errno = 0;
00130 ret = strtoll( n, &endPtr, 10 );
00131 uassert( 13305, "could not convert string to long long", *endPtr == 0 && errno == 0 );
00132 #elif _MSC_VER>=1600 // 1600 is VS2k10 1500 is VS2k8
00133 size_t endLen = 0;
00134 try {
00135 ret = stoll( n, &endLen, 10 );
00136 }
00137 catch ( ... ) {
00138 endLen = 0;
00139 }
00140 uassert( 13306, "could not convert string to long long", endLen != 0 && n[ endLen ] == 0 );
00141 #else // stoll() wasn't introduced until VS 2010.
00142 char* endPtr = 0;
00143 ret = _strtoi64( n, &endPtr, 10 );
00144 uassert( 13310, "could not convert string to long long", (*endPtr == 0) && (ret != _I64_MAX) && (ret != _I64_MIN) );
00145 #endif // !defined(_WIN32)
00146 return ret;
00147 }
00148 }