| 1 |
(* Test 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 |
open Printf |
| 20 |
|
| 21 |
let compile ?(caseless = false) ?(dotall = false) |
| 22 |
?(extended = false) ?(multiline = false) |
| 23 |
patt = |
| 24 |
eprintf "PCRE.compile%s%s%s%s %s\n%!" |
| 25 |
(if caseless then " ~caseless:true" else "") |
| 26 |
(if dotall then " ~dotall:true" else "") |
| 27 |
(if extended then " ~extended:true" else "") |
| 28 |
(if multiline then " ~multiline:true" else "") |
| 29 |
patt; |
| 30 |
PCRE.compile ~caseless ~dotall ~extended ~multiline patt |
| 31 |
|
| 32 |
let matches ?(offset = 0) re str = |
| 33 |
eprintf "PCRE.matches %s, %d ->%!" str offset; |
| 34 |
let r = PCRE.matches ~offset re str in |
| 35 |
eprintf " %b\n%!" r; |
| 36 |
r |
| 37 |
|
| 38 |
let replace ?(global = false) patt subst subj = |
| 39 |
eprintf "PCRE.replace global:%b <patt> %s %s ->%!" global subst subj; |
| 40 |
let r = PCRE.replace ~global patt subst subj in |
| 41 |
eprintf " %s\n%!" r; |
| 42 |
r |
| 43 |
|
| 44 |
let split patt subj = |
| 45 |
eprintf "PCRE.split <patt> %s ->%!" subj; |
| 46 |
let s1, s2 = PCRE.split patt subj in |
| 47 |
eprintf " (%s, %s)\n%!" s1 s2; |
| 48 |
(s1, s2) |
| 49 |
|
| 50 |
let nsplit ?(max = 0) patt subj = |
| 51 |
eprintf "PCRE.nsplit%s <patt> %s ->%!" |
| 52 |
(if max = 0 then "" else sprintf " ~max:%d" max) |
| 53 |
subj; |
| 54 |
let ss = PCRE.nsplit ~max patt subj in |
| 55 |
eprintf " [%s]\n%!" (String.concat "; " ss); |
| 56 |
ss |
| 57 |
|
| 58 |
let sub i = |
| 59 |
eprintf "PCRE.sub %d ->%!" i; |
| 60 |
let r = PCRE.sub i in |
| 61 |
eprintf " %s\n%!" r; |
| 62 |
r |
| 63 |
|
| 64 |
let subi i = |
| 65 |
eprintf "PCRE.subi %d ->%!" i; |
| 66 |
let i1, i2 = PCRE.subi i in |
| 67 |
eprintf " (%d, %d)\n%!" i1 i2; |
| 68 |
(i1, i2) |
| 69 |
|
| 70 |
let () = |
| 71 |
try |
| 72 |
let re0 = compile "a+b" in |
| 73 |
let re1 = compile "(a+)b" in |
| 74 |
let re2 = compile "(a+)(b*)" in |
| 75 |
let re3 = compile ~caseless:true "[^a-z0-9_]" in |
| 76 |
let ws = compile "\\s+" in |
| 77 |
|
| 78 |
assert (matches re0 "ccaaabbbb" = true); |
| 79 |
assert (sub 0 = "aaab"); |
| 80 |
|
| 81 |
assert (matches re0 "aaa" = false); |
| 82 |
|
| 83 |
assert (matches re0 "xyz" = false); |
| 84 |
|
| 85 |
assert (matches re0 "aaabc" = true); |
| 86 |
assert (sub 0 = "aaab"); |
| 87 |
|
| 88 |
assert (matches re1 "ccaaabb" = true); |
| 89 |
assert (sub 1 = "aaa"); |
| 90 |
assert (sub 0 = "aaab"); |
| 91 |
|
| 92 |
assert (matches re2 "ccabbc" = true); |
| 93 |
assert (sub 1 = "a"); |
| 94 |
assert (sub 2 = "bb"); |
| 95 |
assert (sub 0 = "abb"); |
| 96 |
|
| 97 |
assert (matches re2 "ccac" = true); |
| 98 |
assert (sub 1 = "a"); |
| 99 |
assert (sub 2 = ""); |
| 100 |
assert (sub 0 = "a"); |
| 101 |
assert (subi 0 = (2, 3)); |
| 102 |
assert (subi 1 = (2, 3)); |
| 103 |
assert (subi 2 = (3, 3)); |
| 104 |
|
| 105 |
assert (matches ~offset:5 re0 "aaabcabc" = true); |
| 106 |
assert (sub 0 = "ab"); |
| 107 |
|
| 108 |
assert (matches ~offset:5 re0 "aaabcbaac" = false); |
| 109 |
|
| 110 |
assert (replace re0 "dd" "abcabcaabccca" = "ddcabcaabccca"); |
| 111 |
assert (replace ~global:true re0 "dd" "abcabcaabccca" = "ddcddcddccca"); |
| 112 |
|
| 113 |
(* This example copies a usage from customize/firstboot.ml |
| 114 |
* "\xc2\xa3" is utf-8 for the GBP sign. Ideally PCRE would |
| 115 |
* recognize that this is a single character, however doing that |
| 116 |
* would involve passing the PCRE_UTF8 flag when compiling |
| 117 |
* patterns, and that could be problematic if PCRE was built |
| 118 |
* without Unicode support (XXX). |
| 119 |
*) |
| 120 |
assert (replace ~global:true re3 "-" "this is a\xc2\xa3FUNNY.name?" |
| 121 |
(* = "this-is-a-FUNNY-name-" if UTF-8 worked *) |
| 122 |
= "this-is-a--FUNNY-name-"); |
| 123 |
|
| 124 |
(* This also tests PCRE.split since that is used by nsplit. *) |
| 125 |
assert (nsplit ~max:1 ws "a b c" = [ "a b c" ]); |
| 126 |
assert (nsplit ~max:2 ws "a b c" = [ "a"; "b c" ]); |
| 127 |
assert (nsplit ~max:3 ws "a b c" = [ "a"; "b"; "c" ]); |
| 128 |
assert (nsplit ~max:10 ws "a b c" = [ "a"; "b"; "c" ]); |
| 129 |
assert (nsplit ws "the cat sat on \t\t the mat." = |
| 130 |
[ "the"; "cat"; "sat"; "on"; "the"; "mat." ]); |
| 131 |
assert (nsplit ~max:5 ws "the cat sat on \t\t the mat." = |
| 132 |
[ "the"; "cat"; "sat"; "on"; "the mat." ]); |
| 133 |
assert (nsplit ws " the " = [ ""; "the"; "" ]); |
| 134 |
assert (nsplit ws "the " = [ "the"; "" ]); |
| 135 |
assert (nsplit ws " the" = [ ""; "the" ]); |
| 136 |
assert (nsplit ws " \t the" = [ ""; "the" ]); |
| 137 |
with |
| 138 |
| Not_found -> |
| 139 |
failwith "one of the PCRE.sub functions unexpectedly raised Not_found" |
| 140 |
| PCRE.Error (msg, 0) -> |
| 141 |
failwith (sprintf "PCRE error: %s" msg) |
| 142 |
| PCRE.Error (msg, code) -> |
| 143 |
failwith (sprintf "PCRE error: %s (PCRE error code %d)" msg code) |
| 144 |
|
| 145 |
(* Run some out of range [sub] and [subi] calls to ensure an exception |
| 146 |
* is thrown. |
| 147 |
*) |
| 148 |
let () = |
| 149 |
let re2 = compile "(a+)(b*)" in |
| 150 |
ignore (matches re2 "ccac"); |
| 151 |
(try ignore (sub 3) with Not_found -> ()); |
| 152 |
(try ignore (sub (-1)) with Invalid_argument _ -> ()); |
| 153 |
(try ignore (subi 3) with Not_found -> ()); |
| 154 |
(try ignore (subi (-1)) with Invalid_argument _ -> ()) |
| 155 |
|
| 156 |
(* Compile some bad regexps and check that an exception is thrown. |
| 157 |
* It would be nice to check the error message is right but |
| 158 |
* that involves dealing with language and future changes of |
| 159 |
* PCRE error codes. |
| 160 |
*) |
| 161 |
let () = |
| 162 |
List.iter ( |
| 163 |
fun patt -> |
| 164 |
let msg, code = |
| 165 |
try ignore (PCRE.compile patt); assert false |
| 166 |
with PCRE.Error (m, c) -> m, c in |
| 167 |
eprintf "patt: %s -> exception: %s (%d)\n%!" patt msg code |
| 168 |
) [ "("; ")"; "+"; "*"; "(abc" ] |
| 169 |
|
| 170 |
let () = Gc.compact () |