Replacing one string with another



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 04/02/2012 at 16:45, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R13 
    Platform:   Windows  ; Mac  ;  
    Language(s) :     C++  ;

    ---------
    Hey Everyone,

    I am trying to create a function that parses an existing string, looks for a specific string, and then replaces that string with another.

    I found on the forums here a good example of someone doing this..

    Here's that function.

      
      
    //Replace  
    //==========================================//  
    String MyClass::Replace(String orig, String search, String replacement)  
    {  
      String res = orig;  
      
      LONG i = 0;  
      
      while (res.FindFirst(search, &i, i)) {  
      
        res.Delete(i, search.GetLength());  
        res.Insert(i, replacement);  
      }  
      
      return res;  
    }  
      
    

    Now if the search string and the replacement string are the same exact size, this works just fine, but if the search string is for example "X" and the replace string is "[X]", then C4D freezes up.  In fact any time I try to replace a single character with a larger string c4d crashes..  Is there a better way to accomplish this?  Or maybe a way to resize the string without crashing c4d?

    Thanks,

    Shawn



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 00:13, xxxxxxxx wrote:

    In what context are you calling Replace()?
    I tried a rather static example here, which seems to have worked ok.
    Regardless of the actual crash, wouldn't this be a bit faster?

    void Replace1(String &orig, String &search, String &replacement)  
    {  
      LONG i = 0;  
      
      while (orig.FindFirst(search, &i, i)) {  
        orig.Delete(i, search.GetLength());  
        orig.Insert(i, replacement);  
      }  
    }
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 08:14, xxxxxxxx wrote:

    Hi MighT42,

    Thanks for the reply,

    I am calling the function from the Message Function of my object plugin.  When the user presses a button in the attributes manager it calls the function.  I will try to adjust the function to the example you provided to see if it is faster.

    Just out of curiosity, what makes your example faster than the one I provided?  I don't mean that to sound rude, I really don't know.  :)  I would like to understand it better.

    Thanks for your help.

    ~Shawn



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 08:52, xxxxxxxx wrote:

    Hi Shawn,
    as far as I understand (someone correct me, if I'm wrong), in C++ if you pass an object "by value" to a function, the entire object will be copied onto the stack. So upon calling your Replace() I would expect the compiler to copy all three strings onto the stack. Then you create another temporary string on the stack, copy the original string once more, perform the replacement on this temporary string and finally copy the temporary string onto the stack to return from the function.
    In my suggestion the parameters are passed by reference, basically only pointers to the string objects have to be passed to the function and then the replacement is performed directly within the original string. Of course the behaviour is a little different, as your original string gets altered upon calling my Replace1().
    I guess, we're getting a bit offtopic here, but you can find more on this here for example: http://www.cplusplus.com/doc/tutorial/functions2/
    Hope it was understandable,
    Andreas



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 11:03, xxxxxxxx wrote:

    LOL..  I don't mind getting off topic if it helps me understand something better.  :D  Thanks for the explanation.  :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 11:11, xxxxxxxx wrote:

    So when I call that function I just call it like normal?

    Replace(orig, search, replace);



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 11:31, xxxxxxxx wrote:

    okay so from what I've read about pointers it should look like this...

    class declaration

      
    void    Replace(String *orig, String *srch, String *repl);  
    

    definition

      
    void MyClass::Replace(String *orig, String *srch, String *repl)  
    {  
      
      LONG i = 0;  
      
      while (orig->FindFirst(*srch, &i, i)) {  
      
          orig->Delete(i, srch->GetLength());  
          orig->Insert(i, *repl);  
      }  
      
    }  
    

    called function

      
    Replace(&orig, &srch, &repl);  
    

    Does that look right to you?

    Thanks,

    Shawn



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 05/02/2012 at 13:34, xxxxxxxx wrote:

    Your pointer implementation looks correct.
    Although perhaps I wrote a bit misleading, when speaking about passing pointers.
    I really meant it, like I wrote Replace1() in my first post and it's called like this (given all parameters were declared as String)

    Replace1(orig, search, replace);
    

    In C++ it's called reference, when a parameter is defined with an ampersand sign (in C4D API used all over the place). A reference behaves pretty much like a pointer, but has some advantages, mainly references are a bit less error prone than direct pointer usage. Unfortunately it is written in no way different than a call by value, whereas pointer usage is more or less clearly visible.

    Speedwise I don't think there's a difference between reference or pointer implementation. In both cases I'd expect only a pointer to be passed to the function.
    You should really search the web for some more information on parameter passing in C++.


Log in to reply