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

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