Debian Codegraph @

common/mlpcre/PCRE.ml

raw
1 (* Bindings for Perl-compatible Regular Expressions.
2 * Copyright (C) 2017 Red Hat Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *)
18
19 (* Lightweight bindings for the PCRE library. *)
20
21 exception Error of string * int
22
23 type regexp
24
25 external compile : ?caseless:bool -> ?dotall:bool -> ?extended:bool -> ?multiline:bool -> string -> regexp = "guestfs_int_pcre_compile"
26 external matches : ?offset:int -> regexp -> string -> bool = "guestfs_int_pcre_matches"
27 external sub : int -> string = "guestfs_int_pcre_sub"
28 external subi : int -> int * int = "guestfs_int_pcre_subi"
29
30 let rec replace ?(global = false) patt subst subj =
31 if not (matches patt subj) then
32 (* Return original string unchanged if patt doesn't match. *)
33 subj
34 else (
35 (* If patt matches "yyyy" in the original string then we have
36 * the following situation, where "xxxx" is the part of the
37 * original string before the match, and "zzzz..." is the
38 * part after the match:
39 * "xxxxyyyyzzzzzzzzzzzzz"
40 * ^ ^
41 * i1 i2
42 *)
43 let i1, i2 = subi 0 in
44 let xs = String.sub subj 0 i1 (* "xxxx", part before the match *) in
45 let zs = String.sub subj i2 (String.length subj - i2) (* after *) in
46
47 (* If the global flag was set, we want to continue substitutions
48 * in the rest of the string.
49 *)
50 let zs = if global then replace ~global patt subst zs else zs in
51
52 xs ^ subst ^ zs
53 )
54
55 let rec split patt subj =
56 if not (matches patt subj) then
57 subj, ""
58 else (
59 (* If patt matches "yyyy" in the original string then we have
60 * the following situation, where "xxxx" is the part of the
61 * original string before the match, and "zzzz..." is the
62 * part after the match:
63 * "xxxxyyyyzzzzzzzzzzzzz"
64 * ^ ^
65 * i1 i2
66 *)
67 let i1, i2 = subi 0 in
68 let xs = String.sub subj 0 i1 (* "xxxx", part before the match *) in
69 let zs = String.sub subj i2 (String.length subj - i2) (* after *) in
70 xs, zs
71 )
72
73 and nsplit ?(max = 0) patt subj =
74 if max < 0 then
75 invalid_arg "PCRE.nsplit: max parameter should not be negative";
76
77 (* If we reached the limit, OR if the pattern does not match the string
78 * at all, return the rest of the string as a single element list.
79 *)
80 if max = 1 || not (matches patt subj) then
81 [subj]
82 else (
83 let s1, s2 = split patt subj in
84 let max = if max = 0 then 0 else max - 1 in
85 s1 :: nsplit ~max patt s2
86 )
87
88 let () =
89 Callback.register_exception "PCRE.Error" (Error ("", 0))