From bd0fb49da5bf6f6232b5da52b1eb98adc2abbaa2 Mon Sep 17 00:00:00 2001 From: Alberto Lerner Date: Sun, 4 Jul 2010 11:08:00 -0400 Subject: [PATCH] More stringutil goodies --- s/config.cpp | 13 +++++-------- util/stringutils.cpp | 27 +++++++++++++++++---------- util/stringutils.h | 3 ++- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/s/config.cpp b/s/config.cpp index e999d4dead5..fb1487abb4d 100644 --- a/s/config.cpp +++ b/s/config.cpp @@ -366,7 +366,7 @@ namespace mongo { bool ConfigServer::init( string s ){ vector configdbs; - splitStringDelim( s, configdbs, ',' ); + splitStringDelim( s, &configdbs, ',' ); return init( configdbs ); } @@ -380,16 +380,11 @@ namespace mongo { } ourHostname = hn; - stringstream fullString; - set hosts; for ( size_t i=0; i 0 ) - fullString << ","; - fullString << configHosts[i]; } for ( set::iterator i=hosts.begin(); i!=hosts.end(); i++ ){ @@ -407,8 +402,10 @@ namespace mongo { return false; } - _primary.setAddress( fullString.str() , true ); - log(1) << " config string : " << fullString.str() << endl; + string fullString; + joinStringDelim( configHosts, &fullString, ',' ); + _primary.setAddress( fullString , true ); + log(1) << " config string : " << fullString << endl; return true; } diff --git a/util/stringutils.cpp b/util/stringutils.cpp index 6d0b62ab44e..3f989fd8560 100644 --- a/util/stringutils.cpp +++ b/util/stringutils.cpp @@ -20,17 +20,24 @@ namespace mongo { - void splitStringDelim( const string& str, vector& vec, char delim ){ - string s(str); + void splitStringDelim( const string& str , vector* res , char delim ){ + if ( str.empty() ) + return; - while ( true ){ - size_t idx = s.find( delim ); - if ( idx == string::npos ){ - vec.push_back( s ); - break; - } - vec.push_back( s.substr( 0 , idx ) ); - s = s.substr( idx + 1 ); + size_t beg = 0; + size_t pos = str.find( delim ); + while ( pos != string::npos ){ + res->push_back( str.substr( beg, pos - beg) ); + beg = ++pos; + pos = str.find( delim, beg ); + } + res->push_back( str.substr( beg ) ); + } + + void joinStringDelim( const vector& strs , string* res , char delim ){ + for ( vector::const_iterator it = strs.begin(); it != strs.end(); ++it ){ + if ( it !=strs.begin() ) res->push_back( delim ); + res->append( *it ); } } diff --git a/util/stringutils.h b/util/stringutils.h index b3f56664b51..a4a2d8f0ce9 100644 --- a/util/stringutils.h +++ b/util/stringutils.h @@ -17,6 +17,7 @@ namespace mongo { - void splitStringDelim( const string& str, vector& vec, char delim ); + void splitStringDelim( const string& str , vector* res , char delim ); + void joinStringDelim( const vector& strs , string* res , char delim ); } // namespace mongo