|
|
5ce0c7 |
From 83c76b6c610df17e0b9bfd9cd11deb43ebc40411 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
|
|
|
5ce0c7 |
index 7dbba5857..ec3a6f00d 100644
|
|
|
46b2f6 |
--- a/common/mlpcre/pcre-c.c
|
|
|
46b2f6 |
+++ b/common/mlpcre/pcre-c.c
|
|
|
5ce0c7 |
@@ -133,6 +133,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,
|
|
|
5ce0c7 |
@@ -177,9 +186,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);
|
|
|
5ce0c7 |
@@ -217,7 +226,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 |
--
|
|
|
5ce0c7 |
2.18.4
|
|
|
46b2f6 |
|