Skip to content

Commit

Permalink
Add support for some control characters in Re.Perl (#227)
Browse files Browse the repository at this point in the history
* Add support for some control characters in Re.Perl
  • Loading branch information
glondu authored Aug 18, 2023
1 parent b261cf1 commit 146bad9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
(#219)
* Add support for `DOTALL` flag in `Re.Pcre.regexp` (#225)
* Add support for named groups (#223)
* Add support for some control characters in `Re.Perl` (#227)

1.10.4 (27-Apr-2022)
--------------------
Expand Down
24 changes: 23 additions & 1 deletion lib/perl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ let parse multiline dollar_endonly dotall ungreedy s =
end else if accept '\\' then begin
(* XXX
- Back-references
- \cx (control-x), \e, \f, \n, \r, \t, \xhh, \ddd
- \cx (control-x), \ddd
*)
if eos () then raise Parse_error;
match get () with
Expand Down Expand Up @@ -161,6 +161,21 @@ let parse multiline dollar_endonly dotall ungreedy s =
Re.eos
| 'G' ->
Re.start
| 'e' ->
Re.char '\x1b'
| 'f' ->
Re.char '\x0c'
| 'n' ->
Re.char '\n'
| 'r' ->
Re.char '\r'
| 't' ->
Re.char '\t'
| 'x' ->
let c1 = hexdigit () in
let c2 = hexdigit () in
let code = c1 * 16 + c2 in
Re.char (char_of_int code)
| 'a'..'z' | 'A'..'Z' ->
raise Parse_error
| '0'..'9' ->
Expand All @@ -173,6 +188,13 @@ let parse multiline dollar_endonly dotall ungreedy s =
'*' | '+' | '?' | '{' | '\\' -> raise Parse_error
| c -> Re.char c
end
and hexdigit () =
if eos () then raise Parse_error;
match get () with
'0'..'9' as d -> Char.code d - Char.code '0'
| 'a'..'f' as d -> Char.code d - Char.code 'a' + 10
| 'A'..'F' as d -> Char.code d - Char.code 'A' + 10
| _ -> raise Parse_error
and integer () =
if eos () then None else
match get () with
Expand Down

0 comments on commit 146bad9

Please sign in to comment.