Skip to content

Commit

Permalink
refactor(str): improve exception handling (#363)
Browse files Browse the repository at this point in the history
narrow the try/catch to the the parts that are necessary
  • Loading branch information
rgrinberg authored Aug 30, 2024
1 parent 0113a0c commit ae03d02
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions lib/str.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ let compile_regexp s c =
let state = ref None

let string_match re s p =
try
state := Some (exec ~pos:p (Lazy.force re.mtch) s);
match exec ~pos:p (Lazy.force re.mtch) s with
| res ->
state := Some res;
true
with
| Not_found ->
| exception Not_found ->
state := None;
false
;;
Expand All @@ -56,23 +56,21 @@ let string_partial_match re s p =
;;

let search_forward re s p =
try
let res = exec ~pos:p (Lazy.force re.srch) s in
match exec ~pos:p (Lazy.force re.srch) s with
| res ->
state := Some res;
fst (Group.offset res 0)
with
| Not_found ->
| exception Not_found ->
state := None;
raise Not_found
;;

let rec search_backward re s p =
try
let res = exec ~pos:p (Lazy.force re.mtch) s in
match exec ~pos:p (Lazy.force re.mtch) s with
| res ->
state := Some res;
p
with
| Not_found ->
| exception Not_found ->
state := None;
if p = 0 then raise Not_found else search_backward re s (p - 1)
;;
Expand All @@ -93,11 +91,9 @@ let offset_group i =
;;

let group_len i =
try
let b, e = offset_group i in
e - b
with
| Not_found -> 0
match offset_group i with
| b, e -> e - b
| exception Not_found -> 0
;;

let rec repl_length repl p q len =
Expand Down Expand Up @@ -133,13 +129,13 @@ let rec replace orig repl p res q len =
replace orig repl (p + 2) res (q + 1) len
| '0' .. '9' as c ->
let d =
try
let b, e = offset_group (Char.code c - Char.code '0') in
let group = Char.code c - Char.code '0' in
match offset_group group with
| exception Not_found -> 0
| b, e ->
let d = e - b in
if d > 0 then String.blit orig b res q d;
d
with
| Not_found -> 0
in
replace orig repl (p + 2) res (q + d) len
| c ->
Expand Down

0 comments on commit ae03d02

Please sign in to comment.