Changeset 15077

Show
Ignore:
Timestamp:
10.03.2008 15:18:07 (11 years ago)
Author:
mdewsnip
Message:

Oh dear... there were two pretty big problems with the replace() function: it wouldn't work correctly replacing strings longer than one character, and it wouldn't replace a match at the start of the string.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/lib/text_t.cpp

    r14909 r15077  
    311311    text_t::iterator next_toreplace = findword(text_begin, text_end, toreplace); 
    312312 
    313     // Grab the string up to it 
    314     temp_text = substr(text_begin, next_toreplace); 
    315  
    316     // Add the new string onto the end 
    317     if (new_text.empty()) 
    318     { 
    319       new_text.append(temp_text); 
    320     } 
     313    // We've found a match 
     314    if (next_toreplace != text_end) 
     315    { 
     316      new_text.append(substr(text_begin, next_toreplace)); 
     317      new_text.append(replacement); 
     318      count++; 
     319      text_begin = next_toreplace + toreplace.size(); 
     320    } 
     321    // We haven't found a match 
    321322    else 
    322323    { 
    323       new_text.append(replacement + temp_text); 
    324     } 
    325  
    326     // Finally, we need to move the current pointer up to the new position 
    327     text_begin = next_toreplace + 1; 
    328     count++; 
     324      new_text.append(substr(text_begin, text_end)); 
     325      text_begin = text_end; 
     326    } 
    329327  } 
    330328