1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Xreplace bug

Discussion in 'Plugins' started by CarlM, Feb 27, 2016.

  1. CarlM

    Joined:
    Jan 16, 2009
    Messages:
    45
    Likes Received:
    0
    My 4utils64.dll is dated 2-4-16. My TCC is v19.02.37 x64

    The following line:
    SET record_new=%@XREPLACE[jjjj,\r\n\r\n,%record_new]
    produces, where every jjjj was:
    rCRLFrCRLF
    It should produce
    CRLFCRLF
     
  2. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    It looks like \n produces both a CR and an LF, and \r produces a literal 'r'.

    Dave C.
     
  3. CarlM

    Joined:
    Jan 16, 2009
    Messages:
    45
    Likes Received:
    0
    Mmmm, good thought.
    I tested it, and you're right:
    SET record_new=%@XREPLACE[jjjj,\n\n,%record_new]
    works correctly.
    However, the code I use to set up the substitution is:
    SET record_new=%@XREPLACE[\r\n\r\n,jjjj,%record]
    which has been working.
    If I change it to test your theory, to be:
    SET record_new=%@XREPLACE[\n\n,jjjj,%record]
    then it does not find any CRLR to substitute.

    So Xreplace has a different meaning for \r and \n depending on whether it is the source or the target of the substitution.
    Ugh!
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,935
    Likes Received:
    30
    Hmmm! @XREPLACE and @REREPLACE seem to work the same ... maybe a bug in Oniguruma or an oversight. I'll look into it presently.
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,935
    Likes Received:
    30
    New 4UTILS on lucky.syr.edu. That was easy. See before/after below. I recall some collaboration with Rex on XREPLACE/REREPLACE. If they are still similar, than the necessary change is obvious. Here's what WAS in 4UTILS; it needed to be split into two cases.:
    Code:
    if ( *r == L'n' ) // insert newline
    {
       *p++ = L'\r';
       *p++ = L'\n';
       q++;
       continue;
    }
    
    BEFORE:

    Code:
    v:\> set z=1jjjj2jjjj3jjjj4
    
    v:\> set zz=%@xreplace[jjjj,"\r\n\r\n",%z]
    
    v:\> set z
    1jjjj2jjjj3jjjj4
    
    v:\> set zz
    1r
    r
    2r
    r
    3r
    r
    4
    AFTER:

    Code:
    p:\4utils\release> set z=1jjjj2jjjj3jjjj4
    
    p:\4utils\release> set zz=%@xreplace[jjjj,"\r\n\r\n",%z]
    
    p:\4utils\release> set z
    1jjjj2jjjj3jjjj4
    
    p:\4utils\release> set zz
    1
    
    2
    
    3
    
    4
     
  6. CarlM

    Joined:
    Jan 16, 2009
    Messages:
    45
    Likes Received:
    0
    Vince,
    I've done regex in Perl (albeit a while ago).
    Does Rereplace *require* the use of back reference expressions? Can I do ordinary regex in it?
     
  7. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,935
    Likes Received:
    30
    @XREPLACE doesn't *require* back-references (\1, \2, ...). Apparently, neither does @REREPLACE.

    Code:
    v:\> echo %@rereplace[a,e,bat]
    bet
    
    v:\> echo %@xreplace[a,e,bat]
    bet
     
  8. CarlM

    Joined:
    Jan 16, 2009
    Messages:
    45
    Likes Received:
    0
    Then what is the difference between Rereplace and Xreplace?
     
  9. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,935
    Likes Received:
    30
    I don't know if there are any differences. There may be subtle ones. @XREPLACE predates @REREPLACE. I never bothered to remove it from 4UTILS when @REREPLACE arrived.
     
  10. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,935
    Likes Received:
    30
    Having looked at the code for @XREPLACE, I think there are two features (????) that @REREPLACE doesn't have.

    You can limit the number of replacements that are made to N by prefixing <pattern> with N$. And, if any of the parameters are the name of an environment variable prefixed with '@', @XREPLACE will de-reference the envvar. If I recall correctly, that was an attempt to get around strings with troublesome characters in them. Examples:

    Code:
    v:\> echo %@xreplace[3$i,o,iiiiii]
    oooiii
    
    v:\> echo %@xreplace[i,o,@windir]
    C:\Wondows
     

Share This Page