From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@postgresql.org
Subject: pgsql: Implement lookbehind constraints in our regular-expression engin
Date: Fri, 30 Oct 2015 23:14:38 +0000
Message-ID: <E1ZsIsY-0006z6-6T@gemulon.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>
Implement lookbehind constraints in our regular-expression engine.
A lookbehind constraint is like a lookahead constraint in that it consumes
no text; but it checks for existence (or nonexistence) of a match *ending*
at the current point in the string, rather than one *starting* at the
current point. This is a long-requested feature since it exists in many
other regex libraries, but Henry Spencer had never got around to
implementing it in the code we use.
Just making it work is actually pretty trivial; but naive copying of the
logic for lookahead constraints leads to code that often spends O(N^2) time
to scan an N-character string, because we have to run the match engine
from string start to the current probe point each time the constraint is
checked. In typical use-cases a lookbehind constraint will be written at
the start of the regex and hence will need to be checked at every character
--- so O(N^2) work overall. To fix that, I introduced a third copy of the
core DFA matching loop, paralleling the existing longest() and shortest()
loops. This version, matchuntil(), can suspend and resume matching given
a couple of pointers' worth of storage space. So we need only run it
across the string once, stopping at each interesting probe point and then
resuming to advance to the next one.
I also put in an optimization that simplifies one-character lookahead and
lookbehind constraints, such as "(?=x)" or "(?<!\w)", into AHEAD and BEHIND
constraints, which already existed in the engine. This avoids the overhead
of the LACON machinery entirely for these rather common cases.
The net result is that lookbehind constraints run a factor of three or so
slower than Perl's for multi-character constraints, but faster than Perl's
for one-character constraints ... and they work fine for variable-length
constraints, which Perl gives up on entirely. So that's not bad from a
competitive perspective, and there's room for further optimization if
anyone cares. (In reality, raw scan rate across a large input string is
probably not that big a deal for Postgres usage anyway; so I'm happy if
it's linear.)
Branch
------
master
Details
-------
http://git.postgresql.org/pg/commitdiff/12c9a04008870c283931d6b3b648ee21bbc2cfda
Modified Files
--------------
doc/src/sgml/func.sgml | 20 ++++-
src/backend/regex/README | 8 +-
src/backend/regex/re_syntax.n | 15 +++-
src/backend/regex/regc_lex.c | 29 +++++-
src/backend/regex/regc_nfa.c | 43 +++++++++
src/backend/regex/regcomp.c | 152 +++++++++++++++++++++++++------
src/backend/regex/rege_dfa.c | 162 ++++++++++++++++++++++++++++++---
src/backend/regex/regexec.c | 104 ++++++++++++++++++---
src/backend/regex/regexport.c | 2 +-
src/backend/regex/regprefix.c | 2 +-
src/include/regex/regex.h | 2 +-
src/include/regex/regguts.h | 20 +++--
src/test/regress/expected/regex.out | 169 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/regex.sql | 35 ++++++++
14 files changed, 690 insertions(+), 73 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Implement lookbehind constraints in our regular-expression engin
In-Reply-To: <E1ZsIsY-0006z6-6T@gemulon.postgresql.org>
References: <E1ZsIsY-0006z6-6T@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox