diff options
author | Eliot Horowitz <eliot@10gen.com> | 2012-05-16 10:06:19 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2012-05-16 18:22:19 -0400 |
commit | ed1bc019e56dbbb67bc095a4fc7794fdb1a0f3f8 (patch) | |
tree | d8b5dd5e29436b6bd2fb018778c85dffe31443e2 | |
parent | efa3358d03a197cd8e520a17826bfb9ec469ba24 (diff) | |
download | mongo-ed1bc019e56dbbb67bc095a4fc7794fdb1a0f3f8.tar.gz |
moved code from .h to .cpp, no logic changes
-rw-r--r-- | src/mongo/util/text.cpp | 82 | ||||
-rw-r--r-- | src/mongo/util/text.h | 88 |
2 files changed, 97 insertions, 73 deletions
diff --git a/src/mongo/util/text.cpp b/src/mongo/util/text.cpp index 242a00874a0..9374666a866 100644 --- a/src/mongo/util/text.cpp +++ b/src/mongo/util/text.cpp @@ -22,8 +22,60 @@ #include "mongo/util/mongoutils/str.h" #include <boost/smart_ptr/scoped_array.hpp> +using namespace std; + namespace mongo { + // --- StringSplitter ---- + + /** get next split string fragment */ + string StringSplitter::next() { + const char * foo = strstr( _big , _splitter ); + if ( foo ) { + string s( _big , foo - _big ); + _big = foo + 1; + while ( *_big && strstr( _big , _splitter ) == _big ) + _big++; + return s; + } + + string s = _big; + _big += strlen( _big ); + return s; + } + + + void StringSplitter::split( vector<string>& l ) { + while ( more() ) { + l.push_back( next() ); + } + } + + vector<string> StringSplitter::split() { + vector<string> l; + split( l ); + return l; + } + + string StringSplitter::join( vector<string>& l , const string& split ) { + stringstream ss; + for ( unsigned i=0; i<l.size(); i++ ) { + if ( i > 0 ) + ss << split; + ss << l[i]; + } + return ss.str(); + } + + vector<string> StringSplitter::split( const string& big , const string& splitter ) { + StringSplitter ss( big.c_str() , splitter.c_str() ); + return ss.split(); + } + + + + // --- utf8 utils ------ + inline int leadingOnes(unsigned char c) { if (c < 0x80) return 0; static const char _leadingOnes[128] = { @@ -44,6 +96,10 @@ namespace mongo { } + bool isValidUTF8(string s) { + return isValidUTF8(s.c_str()); + } + bool isValidUTF8(const char *s) { int left = 0; // how many bytes are left in the current codepoint while (*s) { @@ -67,6 +123,32 @@ namespace mongo { return true; } + long long parseLL( const char *n ) { + long long ret; + uassert( 13307, "cannot convert empty string to long long", *n != 0 ); +#if !defined(_WIN32) + char *endPtr = 0; + errno = 0; + ret = strtoll( n, &endPtr, 10 ); + uassert( 13305, "could not convert string to long long", *endPtr == 0 && errno == 0 ); +#elif _MSC_VER>=1600 // 1600 is VS2k10 1500 is VS2k8 + size_t endLen = 0; + try { + ret = stoll( n, &endLen, 10 ); + } + catch ( ... ) { + endLen = 0; + } + uassert( 13306, "could not convert string to long long", endLen != 0 && n[ endLen ] == 0 ); +#else // stoll() wasn't introduced until VS 2010. + char* endPtr = 0; + ret = _strtoi64( n, &endPtr, 10 ); + uassert( 13310, "could not convert string to long long", (*endPtr == 0) && (ret != _I64_MAX) && (ret != _I64_MIN) ); +#endif // !defined(_WIN32) + return ret; + } + + #if defined(_WIN32) std::string toUtf8String(const std::wstring& wide) { diff --git a/src/mongo/util/text.h b/src/mongo/util/text.h index 423fb653e08..298a856c7b7 100644 --- a/src/mongo/util/text.h +++ b/src/mongo/util/text.h @@ -32,6 +32,9 @@ #pragma once +#include <vector> +#include <string> + namespace mongo { class StringSplitter { @@ -44,52 +47,18 @@ namespace mongo { } /** @return true if more to be taken via next() */ - bool more() { - return _big[0] != 0; - } + bool more() const { return _big[0] != 0; } /** get next split string fragment */ - string next() { - const char * foo = strstr( _big , _splitter ); - if ( foo ) { - string s( _big , foo - _big ); - _big = foo + 1; - while ( *_big && strstr( _big , _splitter ) == _big ) - _big++; - return s; - } - - string s = _big; - _big += strlen( _big ); - return s; - } - - void split( vector<string>& l ) { - while ( more() ) { - l.push_back( next() ); - } - } + std::string next(); - vector<string> split() { - vector<string> l; - split( l ); - return l; - } + void split( std::vector<std::string>& l ); - static vector<string> split( const string& big , const string& splitter ) { - StringSplitter ss( big.c_str() , splitter.c_str() ); - return ss.split(); - } + std::vector<std::string> split(); + + static std::vector<std::string> split( const std::string& big , const std::string& splitter ); - static string join( vector<string>& l , const string& split ) { - stringstream ss; - for ( unsigned i=0; i<l.size(); i++ ) { - if ( i > 0 ) - ss << split; - ss << l[i]; - } - return ss.str(); - } + static std::string join( std::vector<std::string>& l , const std::string& split ); private: const char * _big; @@ -101,7 +70,11 @@ namespace mongo { * guarantee that the codepoints are valid. */ bool isValidUTF8(const char *s); - inline bool isValidUTF8(string s) { return isValidUTF8(s.c_str()); } + bool isValidUTF8(std::string s); + + // expect that n contains a base ten number and nothing else after it + // NOTE win version hasn't been tested directly + long long parseLL( const char *n ); #if defined(_WIN32) @@ -117,37 +90,6 @@ namespace mongo { inline std::wstring toNativeString(const char *s) { return toWideString(s); } # endif -#endif - - // expect that n contains a base ten number and nothing else after it - // NOTE win version hasn't been tested directly - inline long long parseLL( const char *n ) { - long long ret; - uassert( 13307, "cannot convert empty string to long long", *n != 0 ); -#if !defined(_WIN32) - char *endPtr = 0; - errno = 0; - ret = strtoll( n, &endPtr, 10 ); - uassert( 13305, "could not convert string to long long", *endPtr == 0 && errno == 0 ); -#elif _MSC_VER>=1600 // 1600 is VS2k10 1500 is VS2k8 - size_t endLen = 0; - try { - ret = stoll( n, &endLen, 10 ); - } - catch ( ... ) { - endLen = 0; - } - uassert( 13306, "could not convert string to long long", endLen != 0 && n[ endLen ] == 0 ); -#else // stoll() wasn't introduced until VS 2010. - char* endPtr = 0; - ret = _strtoi64( n, &endPtr, 10 ); - uassert( 13310, "could not convert string to long long", (*endPtr == 0) && (ret != _I64_MAX) && (ret != _I64_MIN) ); -#endif // !defined(_WIN32) - return ret; - } - -#if defined(_WIN32) - class WindowsCommandLine { char** _argv; |