8000 re_replace: backslash escaping changed · Issue #24 · SWI-Prolog/packages-pcre · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

re_replace: backslash escaping changed #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
spl opened this issue Jun 1, 2022 · 3 comments
Open

re_replace: backslash escaping changed #24

spl opened this issue Jun 1, 2022 · 3 comments

Comments

@spl
Copy link
spl commented Jun 1, 2022

In 6c70176...fe4775d (which I derived from SWI-Prolog/swipl-devel@4c14056), something change in how re_replace worked. I think it has to do with the backslash escaping.

Before the above commit range, this worked:

?- re_replace('[|]'/g, '\\\\0', '|', Out).
Out = "\\|".

After the above commit range, Out changed:

?- re_replace('[|]'/g, '\\\\0', '|', Out).
Out = "\\0".

In order to get back to the first Out, I can add \\:

?- re_replace('[|]'/g, '\\\\\\0', '|', Out).
Out = "\\|".

Fortunately, I can also use $0 instead of \0, which works both before and after the above commit range:

?- re_replace('[|]'/g, '\\$0', '|', Out).
Out = "\\|".
@JanWielemaker
Copy link
Member

@kamahen, was this change intended? I found some compatibility page at https://www.regular-expressions.info/replacebackref.html, which claims that \1-\9 is supported by most major languages (doesn't talk about \0). It is deprecated in Perl.

@kamahen
Copy link
Member
kamahen commented Jun 1, 2022

library(pcre) does not use PCRE2's replacement API; it continues to do things in the same way as the old code that used PCRE1. The handling of \1-\9 is in Prolog code, in particular string_escape//1 at

%! string_escape(-Codes)// is nondet.

I did make some changes in how replacements are handled - in particular, there were some bugs that I fixed (e.g., see Issue #9 and Issue #5). There was also a problem with "\" escaping ... this seems to be the relevant commit: 49e811c ... it's possible that I don't have enough test cases, so I'll put together a PR that adds something like what @spl has pointed out and - if it indicates a bug rather than a bug that's been fixed - I'll come up with a fix. But it could take me a few days to do this (I'm preoccupied with some other things) -- I hope that's OK.

@spl
Copy link
Author
spl commented Jun 1, 2022

No problem. The last example I wrote works before and after, so I've already changed our code to use that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0