Skip to content
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

Fix for 31456 -- replace with optional matching group fails when there is no match #31493

Closed
wants to merge 7 commits into from

Conversation

tlienart
Copy link
Contributor

@tlienart tlienart commented Mar 27, 2019

This PR suggests a fix for #31456 .

I don't know enough about Julia's regex system to know if that one line could have negative side-effects so I apologise in advance if it's a dumb idea.

Edit: added tests for the respective cases.

base/regex.jl Outdated
@@ -363,6 +363,9 @@ macro s_str(string) SubstitutionString(string) end
replace_err(repl) = error("Bad replacement string: $repl")

function _write_capture(io, re, group)
# in the case of an optional group that doesn't match, don't write anything
# e.g. replace("The fox.", r"fox(es)?" => s"bus\1")
isa(re.match_data, Ptr{Nothing}) && return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right --- isn't match_data always a Ptr?

We might have to modify substring_length_bynumber to return 0 in this error case, but I'm not sure whether that's the best way to do it.

Copy link
Contributor Author

@tlienart tlienart Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok yes. Looking at the PCRE2 doc for substring (https://code.woboq.org/qt5/qtbase/src/3rdparty/pcre2/src/pcre2_substring.c.html) L315 onwards, it returns a PCRE2_ERROR_UNSET code (-55) when it can't find a match.

So locally I did the following:

  1. modify substring_length_bynumber as suggested with
function substring_length_bynumber(match_data, number)
    s = Base.RefValue{Csize_t}()
    rc = ccall((:pcre2_substring_length_bynumber_8, PCRE_LIB), Cint,
          (Ptr{Cvoid}, UInt32, Ref{Csize_t}), match_data, number, s)
    if rc < 0
        rc == -55 && return 0
        error("PCRE error: $(err_message(rc))")
    end
    convert(Int, s[])
end
  1. modify _write_capture so that it doesn't go further when it has a length of 0 (otherwise the next PCRE function fails):
function _write_capture(io, re, group)
    len = substring_length_bynumber(re.match_data, group)
    len == 0 && return
    ensureroom(io, len+1)
    PCRE.substring_copy_bynumber(re.match_data, group,
        pointer(io.data, io.ptr), len+1)
    io.ptr += len
    io.size = max(io.size, io.ptr - 1)
end

And:

julia> replace("The quick fox quickly.", r"(quick)?\sfox(es)?\s(run)?" => s"\1 bus\2 \3")
"The quick bus quickly."

Should I try that in the PR or should I close it and let someone who knows more about PCRE take over? I've added that to the PR, feel free to close the PR if you think that's not the right approach, happy for someone more competent to do it (or tell me how it should be done)

Comment on lines +159 to +160
# PCRE2_ERROR_UNSET see https://code.woboq.org/qt5/qtbase/src/3rdparty/pcre2/src/pcre2_substring.c.html
rc == -55 && return 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# PCRE2_ERROR_UNSET see https://code.woboq.org/qt5/qtbase/src/3rdparty/pcre2/src/pcre2_substring.c.html
rc == -55 && return 0
rc == ERROR_UNSET && return 0

@tlienart
Copy link
Contributor Author

tlienart commented Apr 7, 2021

closing in favour of the updated #40391 (sorry for opening a new one).

@tlienart tlienart closed this Apr 7, 2021
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

Successfully merging this pull request may close these issues.

3 participants