Fix conversion of SIMILAR TO regexes for character classes
authorMichael Paquier <michael@paquier.xyz>
Tue, 27 May 2025 23:59:28 +0000 (08:59 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 27 May 2025 23:59:28 +0000 (08:59 +0900)
commit9481d1614c2ac57b638e2238cca0942019a47068
tree9eea62769d801dcd83fa1dbf8d25943f5d2deea8
parent4f49456094dff6cb0b735f9aef46c33be4ae209a
Fix conversion of SIMILAR TO regexes for character classes

The code that translates SIMILAR TO pattern matching expressions to
POSIX-style regular expressions did not consider that square brackets
can be nested.  For example, in an expression like [[:alpha:]%_], the
logic replaced the placeholders '_' and '%' but it should not.

This commit fixes the conversion logic by tracking the nesting level of
square brackets marking character class areas, while considering that
in expressions like []] or [^]] the first closing square bracket is a
regular character.  Multiple tests are added to show how the conversions
should or should not apply applied while in a character class area, with
specific cases added for all the characters converted outside character
classes like an opening parenthesis '(', dollar sign '$', etc.

Author: Laurenz Albe <laurenz.albe@cybertec.at>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: http://postgr.es/m/16ab039d1af455652bdf4173402ddda145f2c73b.camel@cybertec.at
Backpatch-through: 13
src/backend/utils/adt/regexp.c
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql