Skip to content

Commit

Permalink
Qt-LGPL-exception-1.1: Order match from longest to shortest
Browse files Browse the repository at this point in the history
The alternation operator (|) is commutative in the POSIX spec, which
has [1]:

> If the pattern permits a variable number of matching characters and
> thus there is more than one such sequence starting at that point,
> the longest such sequence is matched.

Testing with a few engines, here are some that match POSIX:

  $ echo abc | grep -Eo '|a|ab'
  ab
  $ python -c 'import regex; print(regex.match("|a|ab", "abc", regex.POSIX).group(0))'
  ab  # Background in https://bitbucket.org/mrabarnett/mrab-regex/issues/150
  $ psql -Atc "select substring('abc' from '|a|ab')"
  ab

The docs for PostgreSQL are somewhat complicated, but for two or more
branches connected by the | operator they are always greedy [2].

Here are some engines that prefer the left-most branch:

  $ python -c 'import re; print(re.match("|a|ab", "abc").group(0))'
  $ python -c 'import regex; print(regex.match("|a|ab", "abc").group(0))'
  $ node -e 'console.log("abc".match(/|a|ab/)[0])'  # [3]
  $ ruby -e 'print "abc".match(/|a|ab/); print "\n"'  # [4]

Go's stdlib provides both left-most-branch [5] and longest-match [6]
implementations.

So the old order was compliant with POSIX EREs (as referenced in
schema/ListedLicense.xsd), but this commit sorts the branches for
longest-match-first for compatibility with engines that break POSIX
and prefer the left-most branch.

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_01_02
[2]: https://www.postgresql.org/docs/10/static/functions-matching.html#POSIX-MATCHING-RULES
[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-or
[4]: https://ruby-doc.org/core-2.5.0/Regexp.html#class-Regexp-label-Alternation
     Although these docs don't have much to say on longest-match
     vs. left-most branch.
[5]: https://golang.org/pkg/regexp/#Compile
[6]: https://golang.org/pkg/regexp/#CompilePOSIX
  • Loading branch information
wking committed Mar 25, 2018
1 parent 8fc9a4b commit b6b27fd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/exceptions/Qt-LGPL-exception-1.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<notes>Used with the LGPL-2.1, which is mentioned explicitly in the exception text.</notes>
<text>
<titleText>
<p><alt name="nokia" match="|Nokia|The Qt Company">The Qt Company</alt> Qt LGPL Exception version 1.1</p>
<p><alt name="nokia" match="The Qt Company|Nokia|">The Qt Company</alt> Qt LGPL Exception version 1.1</p>
</titleText>
<p>As an additional permission to the GNU Lesser General Public
License version 2.1, the object code form of a "work that
Expand Down

0 comments on commit b6b27fd

Please sign in to comment.