Boost logo

Boost Users :

From: Duane Murphy (duanemurphy_at_[hidden])
Date: 2002-07-09 10:19:20


--- At Tue, 9 Jul 2002 11:11:50 +0200, Daniel Frey wrote:

>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;
>}

This is shooting from the hip, but it seems like this might need to be:

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

string::const_iterator begin = what[ 1 ].first;

I havent looked at regex to know if this is right. Its just a guess from
knowing how iterators work. ( const iterator != const_iterator )

 ...Duane

-- 
"If tyranny and oppression come to this land, it will be in the
guise of fighting a foreign enemy."              - James Madison

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