From: stephan b. <sg...@us...> - 2004-12-26 11:58:45
|
Update of /cvsroot/pclasses/pclasses2/src/Util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9127/src/Util Modified Files: StringTool.cpp Log Message: More tweaking... Index: StringTool.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Util/StringTool.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- StringTool.cpp 26 Dec 2004 11:10:00 -0000 1.2 +++ StringTool.cpp 26 Dec 2004 11:58:33 -0000 1.3 @@ -1,4 +1,5 @@ #include <pclasses/Util/StringTool.h> +// #include <pclasses/s11n/s11n_debuggering_macros.h> // CERR namespace P { namespace StringTool { @@ -10,12 +11,12 @@ std::string::size_type pos = str.npos; EntityMap::const_iterator mit; EntityMap::const_iterator met = map.end(); - std::string key; std::string val; if( reverse ) { - + // CERR << "Reverse-translating entities.\n"; + // treat KEY=VAL as VAL=KEY mit = map.begin(); for( ; mit != met; ++mit ) { @@ -30,14 +31,28 @@ } else { - pos = str.size() - 1; - for( ; pos != std::string::npos; --pos ) + // CERR << "Translating entities in ["<<str<<"]...\n"; + // treat KEY=VAL as KEY=VAL + mit = map.begin(); + for( ; mit != met; ++mit ) { - mit = map.find( str.substr(pos,1) ); - if( met == mit ) continue; - ++count; - str.replace( pos, 1, (*mit).second ); + key = (*mit).first; + val = (*mit).second; + while( str.npos != (pos = str.rfind( key )) ) + { + ++count; + str.replace( pos, key.size(), val ); + } } +// this code works, and is much more efficient, but only accepts keys with length of 1: +// pos = str.size() - 1; +// for( ; pos != std::string::npos; --pos ) +// { +// mit = map.find( str.substr(pos,1) ); +// if( met == mit ) continue; +// ++count; +// str.replace( pos, 1, (*mit).second ); +// } } return count; } @@ -109,7 +124,7 @@ void normalizeString( std::string &str ) { - //COUT << "normalize_string("<<str<<")" << endl; + //COUT << "normalizeString("<<str<<")" << endl; trimString( str ); stripSlashes( str ); const char ch = str[0]; @@ -173,7 +188,7 @@ } - int int4hexchar( char c ) + int int4hexchar( int c ) { int i = -1; if( c >= 48 && c <=57 ) // 0-9 @@ -195,14 +210,16 @@ int hex2int( const std::string & wd ) { unsigned int mult = 1; - int ret = 0; + int ret = -1; char c; + int tmp; for( std::string::size_type i = wd.size(); i > 0; --i ) { //COUT << "i="<<i<<endl; c = wd[i-1]; if( '#' == c ) continue; - ret += mult * int4hexchar( c ); + if( -1 == (tmp = int4hexchar( c )) ) return -1; + ret += mult * tmp; mult = mult * 16; } return ret; @@ -226,29 +243,29 @@ size_t expandVarsInline( const EntityMap & src, std::string & buffer ) { - //CERR << "expandVars(["<<buffer<<"])"<<std::endl; + using std::string; + //CERR << "environment::expand_vars(["<<buffer<<"])"<<std::endl; if( buffer.size() < 2 ) return 0; - EntityMap::const_iterator entit, entet = src.end(); - - std::string::size_type posA = 0, posB = 0; - static const char vardelim = '$'; - posA = buffer.find( vardelim ); - if( std::string::npos == posA ) - { + string::size_type posA = 0, posB = 0; + static const char vardelim = '$'; + posA = buffer.find( vardelim ); + if( string::npos == posA ) + { return 0; } static const char opener = '{'; static const char closer = '}'; + string tmpvar; size_t count = 0; - static const std::string allowable_chars = + static const string allowable_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_?"; // ^^^^^^ why is '?' in the list? So that eshell can support the shell-ish $? var :/. char atc; posA = buffer.size() - 1; bool slashmode = false; - std::string varname; - for( ; posA >= 0 && posA != std::string::npos; posA-- ) + EntityMap::const_iterator srcit, srcet = src.end(); + for( ; posA >= 0 && posA != string::npos; posA-- ) { atc = buffer[posA]; if( atc != vardelim ) @@ -263,11 +280,12 @@ } posB = buffer.find_first_not_of( allowable_chars, posA+1 ); // find first non-variablename char if( posB != posA +1 ) posB -= 1; - if( posB == std::string::npos ) + if( posB == string::npos ) { posB = buffer.size() -1; } - + + tmpvar.clear(); if( posB == posA + 1 ) // ${VAR} or $F, hopefully { atc = buffer[posB]; @@ -275,12 +293,12 @@ { // $NONBRACED_VAR posB = buffer.find_first_not_of( allowable_chars, posB ); - varname = buffer.substr( posA + 1, posB ); - //CERR << "nonbraced var? ["<<varname<<"]\n"; - // varname += atc; - // if( varname.find_first_of( allowable_chars ) != 0 ) + tmpvar = buffer.substr( posA + 1, posB ); + //CERR << "nonbraced var? ["<<tmpvar<<"]\n"; + // tmpvar += atc; + // if( tmpvar.find_first_of( allowable_chars ) != 0 ) // { - // varname = string(); + // tmpvar = string(); // } } @@ -291,29 +309,28 @@ while( atc != closer && posB <= maxpos ) { // extract variable-name part: atc = buffer[++posB]; - if ( atc != closer ) varname += atc; + if ( atc != closer ) tmpvar += atc; } } } else { // extract variable-name part: - varname = buffer.substr( posA+1 /*skip '$'*/, posB-posA ); + tmpvar = buffer.substr( posA+1 /*skip '$'*/, posB-posA ); } - //CERR << "expandVars(): varname=["<<varname<<"]"<<endl; - if( varname.empty() ) continue; - - entit = src.find( varname ); - if( entet == entit ) continue;// don't expand unknown vars to empty strings. - varname = (*entit).second; - //CERR << "expandVars(): expanded varname=["<<varname<<"]"<<endl; + //CERR << "expand_vars(): tmpvar=["<<tmpvar<<"]"<<endl; + if( tmpvar.empty() ) continue; + srcit = src.find( tmpvar ); // don't expand unknown vars to empty strings. + if( srcet == srcit ) continue; + tmpvar = (*srcit).second; + //CERR << "expand_vars(): expanded tmpvar=["<<tmpvar<<"]"<<endl; ++count; buffer.erase( posA, posB - posA +1 ); - buffer.insert( posA, varname.c_str() ); - varname.clear(); + buffer.insert( posA, tmpvar.c_str() ); } return count; } -} } // P::StringTool +}} // P::StringTool + |