From 10f62a91583812611acfc3a16e01eddb8e1a33a9 Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Fri, 13 Apr 2018 00:24:49 +0300 Subject: [PATCH] Support (?i) in regexps Related to #79. --- README.md | 6 +++--- graphql/accessor_general.lua | 17 +++++++++++++++-- test/local/space_pcre.result | 2 +- test/local/space_pcre.test.lua | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7acd211..f0baf39 100644 --- a/README.md +++ b/README.md @@ -101,13 +101,13 @@ make test * tarantool, * lulpeg, * >=tarantool/avro-schema-2.0-71-gfea0ead, - * >=tarantool/shard-1.1-91-gfa88bf8 (optional), - * lrexlib-pcre (optional). + * >=tarantool/shard-1.1-91-gfa88bf8 (but < 2.0) (optional), + * lrexlib-pcre2 or lrexlib-pcre (optional). * For test (additionally to 'for use'): * python 2.7, * virtualenv, * luacheck, - * >=tarantool/shard-1.1-92-gec1a27e. + * >=tarantool/shard-1.1-92-gec1a27e (but < 2.0). * For building apidoc (additionally to 'for use'): * ldoc. diff --git a/graphql/accessor_general.lua b/graphql/accessor_general.lua index 3d9703b..f948a0a 100644 --- a/graphql/accessor_general.lua +++ b/graphql/accessor_general.lua @@ -9,7 +9,11 @@ local json = require('json') local avro_schema = require('avro_schema') local utils = require('graphql.utils') local clock = require('clock') -local rex = utils.optional_require('rex_pcre') +local rex, is_pcre2 = utils.optional_require('rex_pcre2'), true +if rex == nil then + -- fallback to libpcre + rex, is_pcre2 = utils.optional_require('rex_pcre'), false +end -- XXX: consider using [1] when it will be mature enough; -- look into [2] for the status. @@ -777,8 +781,17 @@ local function match_using_re(obj, pcre) assert(rex ~= nil, 'we should not pass over :compile() ' .. 'with a query contains PCRE matching when there are '.. 'no lrexlib-pcre (rex_pcre) module present') + -- emulate behaviour of (?i) on libpcre (libpcre2 supports it) + local cfg + if not is_pcre2 then + local cnt + re, cnt = re:gsub('^%(%?i%)', '') + if cnt > 0 then + cfg = 'i' + end + end -- XXX: compile re once - local re = rex.new(re) + local re = rex.new(re, cfg) if not re:match(obj[field_name]) then return false end diff --git a/test/local/space_pcre.result b/test/local/space_pcre.result index bdc5963..781f3c6 100644 --- a/test/local/space_pcre.result +++ b/test/local/space_pcre.result @@ -12,7 +12,7 @@ QUERY VARIABLES --- middle_name_re: ich$ -first_name_re: ^I +first_name_re: (?i)^i ... RESULT diff --git a/test/local/space_pcre.test.lua b/test/local/space_pcre.test.lua index 433d715..beed9e8 100755 --- a/test/local/space_pcre.test.lua +++ b/test/local/space_pcre.test.lua @@ -83,7 +83,7 @@ local function run_queries(gql_wrapper) utils.show_trace(function() local variables_1_1 = { - first_name_re = '^I', + first_name_re = '(?i)^i', middle_name_re = 'ich$', } local result = gql_query_1:execute(variables_1_1)