Boost logo

Boost Users :

From: Daniel Frey (yg-boost-users_at_[hidden])
Date: 2002-07-09 04:11:50


Hi,

I have a container (map) with identifiers and value and would like to
replace variables in a string. The function to perform the replacement I
wrote needs a const_cast, which makes me a bit nervous. Is this
necessary? Is there a better way to implement it?

Here's the code:

// The global variable container;
map< string, string > variables;

const string resolv_variables( string s )
{
    // Variables (...${identifier}...) are matched by:
    static const boost::regex find( "(?:^|.*[^\\\\])(\\$\\{" // start
                                    "([a-zA-Z_][a-zA-Z0-9_]*)" // identifier
                                    "\\}).*" ); // end

    boost::smatch what;
    while( boost::regex_match( s, what, find ) ) {
        const string::iterator begin =
            const_cast< string::iterator >( what[ 1 ].first );

        const string::iterator end =
            const_cast< string::iterator >( what[ 1 ].second );

        const string identifier = what[ 2 ];

        s = s.replace( begin, end, variables[ identifier ] );
    }

    return s;
}

Regards, Daniel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net