[MUD-Dev] New Here

Mike szii at sziisoft.com
Fri Jun 20 13:13:04 CEST 2003

On Thu, 2003-06-19 at 21:16, MIKE MacMartin wrote:

> The prompt I'm looking for is /C$N/r>/x ... which, before the
> colour substitution (/ is the colour escape, $ is the string
> escape), flips the order of the > and the name ($N is the acting
> character's name).

> Here is the code I've got written (using C++ and std::string):

>   string CCharacter::expand(string fmt, bool victim) {
>     string::size_type pos = 0;
>     while ((pos = fmt.find('$', pos + 1)), pos != string::npos) {
>       fmt.insert(pos + 2, text(fmt[pos+1], victim));
>       fmt.erase(pos, 2);
>     }
>     return fmt;
>   }

Welcome to the list.  =)

Now on to your question...

This may be a style thing, but...

Do this.  Change your while from

    while ((pos = fmt.find('$', pos + 1)), pos != string::npos) {

    while (pos != string::npos) { // string::npos is -1
      pos = fmt.find('$',pos + 1);
      if (pos != string:: npos) {
        // work the string in here

You may (possibly) be running into a compiler optimisation thing
where it's doing the npos check prior to the = in the while loop.
Either reformatting the code (int equality checks are minimal CPU
usage, esp in the context of your string interations) or turning off
optimisations will expose that.

You could also try compiling without optimisations.

Perhaps adding the "pos" as a character to your

My gut reaction is that you're looping too many times (either by
embedded $ chars in the std::map lookup or because of the while()
condition not correctly triggering.

There's virtually no way you're going to do a string::insert call
that far off unless 1) your 'pos' var is way off or 2) the string is
munged and the pos is correct...giving you the corrupted output.

Unless you invalidate an iterator (which you're not using anyways)
there shouldn't be any issues with a string::find() after a
string::insert().  Of course, that assumes a non-buggy STL
implementation. (ie, some MS)

www.dinkumware.com or similiar recommended.

MUD-Dev mailing list
MUD-Dev at kanga.nu

More information about the mud-dev-archive mailing list