Blame SOURCES/0010-common-mlpcre-add-offset-flag-for-PCRE.matches.patch

da373f
From 83c76b6c610df17e0b9bfd9cd11deb43ebc40411 Mon Sep 17 00:00:00 2001
3efd08
From: Pino Toscano <ptoscano@redhat.com>
3efd08
Date: Tue, 19 Feb 2019 10:50:01 +0100
3efd08
Subject: [PATCH] common/mlpcre: add offset flag for PCRE.matches
3efd08
3efd08
This way it is possible to change where the matching start, instead of
3efd08
always assuming it is the beginning.
3efd08
3efd08
(cherry picked from commit 0ed2e5c14a302d15fd3b75ee2c1cb808a06cb746)
3efd08
---
3efd08
 common/mlpcre/PCRE.ml       |  2 +-
3efd08
 common/mlpcre/PCRE.mli      |  5 ++++-
3efd08
 common/mlpcre/pcre-c.c      | 16 +++++++++++++---
3efd08
 common/mlpcre/pcre_tests.ml | 11 ++++++++---
3efd08
 4 files changed, 26 insertions(+), 8 deletions(-)
3efd08
3efd08
diff --git a/common/mlpcre/PCRE.ml b/common/mlpcre/PCRE.ml
3efd08
index b054928f9..33074af1c 100644
3efd08
--- a/common/mlpcre/PCRE.ml
3efd08
+++ b/common/mlpcre/PCRE.ml
3efd08
@@ -23,7 +23,7 @@ exception Error of string * int
3efd08
 type regexp
3efd08
 
3efd08
 external compile : ?anchored:bool -> ?caseless:bool -> ?dotall:bool -> ?extended:bool -> ?multiline:bool -> string -> regexp = "guestfs_int_pcre_compile_byte" "guestfs_int_pcre_compile"
3efd08
-external matches : regexp -> string -> bool = "guestfs_int_pcre_matches"
3efd08
+external matches : ?offset:int -> regexp -> string -> bool = "guestfs_int_pcre_matches"
3efd08
 external sub : int -> string = "guestfs_int_pcre_sub"
3efd08
 external subi : int -> int * int = "guestfs_int_pcre_subi"
3efd08
 
3efd08
diff --git a/common/mlpcre/PCRE.mli b/common/mlpcre/PCRE.mli
3efd08
index eacb6fd90..e10d512fc 100644
3efd08
--- a/common/mlpcre/PCRE.mli
3efd08
+++ b/common/mlpcre/PCRE.mli
3efd08
@@ -62,7 +62,7 @@ val compile : ?anchored:bool -> ?caseless:bool -> ?dotall:bool -> ?extended:bool
3efd08
     See pcreapi(3) for details of what they do.
3efd08
     All flags default to false. *)
3efd08
 
3efd08
-val matches : regexp -> string -> bool
3efd08
+val matches : ?offset:int -> regexp -> string -> bool
3efd08
 (** Test whether the regular expression matches the string.  This
3efd08
     returns true if the regexp matches or false otherwise.
3efd08
 
3efd08
@@ -71,6 +71,9 @@ val matches : regexp -> string -> bool
3efd08
     or the thread/program exits.  You can call {!sub} to return
3efd08
     these substrings.
3efd08
 
3efd08
+    The [?offset] flag is used to change the start of the search,
3efd08
+    which by default is at the beginning of the string (position 0).
3efd08
+
3efd08
     This can raise {!Error} if PCRE returns an error. *)
3efd08
 
3efd08
 val sub : int -> string
3efd08
diff --git a/common/mlpcre/pcre-c.c b/common/mlpcre/pcre-c.c
da373f
index 7dbba5857..ec3a6f00d 100644
3efd08
--- a/common/mlpcre/pcre-c.c
3efd08
+++ b/common/mlpcre/pcre-c.c
da373f
@@ -133,6 +133,15 @@ is_Some_true (value v)
3efd08
     Bool_val (Field (v, 0)) /* Some true */;
3efd08
 }
3efd08
 
3efd08
+static int
3efd08
+Optint_val (value intv, int defval)
3efd08
+{
3efd08
+  if (intv == Val_int (0))      /* None */
3efd08
+    return defval;
3efd08
+  else                          /* Some int */
3efd08
+    return Int_val (Field (intv, 0));
3efd08
+}
3efd08
+
3efd08
 value
3efd08
 guestfs_int_pcre_compile (value anchoredv, value caselessv, value dotallv,
3efd08
                           value extendedv, value multilinev,
da373f
@@ -177,9 +186,9 @@ guestfs_int_pcre_compile_byte (value *argv, int argn)
3efd08
 }
3efd08
 
3efd08
 value
3efd08
-guestfs_int_pcre_matches (value rev, value strv)
3efd08
+guestfs_int_pcre_matches (value offsetv, value rev, value strv)
3efd08
 {
3efd08
-  CAMLparam2 (rev, strv);
3efd08
+  CAMLparam3 (offsetv, rev, strv);
3efd08
   pcre *re = Regexp_val (rev);
3efd08
   struct last_match *m, *oldm;
3efd08
   size_t len = caml_string_length (strv);
da373f
@@ -217,7 +226,8 @@ guestfs_int_pcre_matches (value rev, value strv)
3efd08
     caml_raise_out_of_memory ();
3efd08
   }
3efd08
 
3efd08
-  m->r = pcre_exec (re, NULL, m->subject, len, 0, 0, m->vec, veclen);
3efd08
+  m->r = pcre_exec (re, NULL, m->subject, len, Optint_val (offsetv, 0), 0,
3efd08
+                    m->vec, veclen);
3efd08
   if (m->r < 0 && m->r != PCRE_ERROR_NOMATCH) {
3efd08
     int ret = m->r;
3efd08
     free_last_match (m);
3efd08
diff --git a/common/mlpcre/pcre_tests.ml b/common/mlpcre/pcre_tests.ml
3efd08
index 346019c40..3e5981107 100644
3efd08
--- a/common/mlpcre/pcre_tests.ml
3efd08
+++ b/common/mlpcre/pcre_tests.ml
3efd08
@@ -30,9 +30,9 @@ let compile ?(anchored = false) ?(caseless = false)
3efd08
           patt;
3efd08
   PCRE.compile ~anchored ~caseless ~dotall ~extended ~multiline patt
3efd08
 
3efd08
-let matches re str =
3efd08
-  eprintf "PCRE.matches %s ->%!" str;
3efd08
-  let r = PCRE.matches re str in
3efd08
+let matches ?(offset = 0) re str =
3efd08
+  eprintf "PCRE.matches %s, %d ->%!" str offset;
3efd08
+  let r = PCRE.matches ~offset re str in
3efd08
   eprintf " %b\n%!" r;
3efd08
   r
3efd08
 
3efd08
@@ -103,6 +103,11 @@ let () =
3efd08
     assert (subi 1 = (2, 3));
3efd08
     assert (subi 2 = (3, 3));
3efd08
 
3efd08
+    assert (matches ~offset:5 re0 "aaabcabc" = true);
3efd08
+    assert (sub 0 = "ab");
3efd08
+
3efd08
+    assert (matches ~offset:5 re0 "aaabcbaac" = false);
3efd08
+
3efd08
     assert (replace re0 "dd" "abcabcaabccca" = "ddcabcaabccca");
3efd08
     assert (replace ~global:true re0 "dd" "abcabcaabccca" = "ddcddcddccca");
3efd08
 
3efd08
-- 
da373f
2.18.4
3efd08