Commit ff63cd53 authored by gerd's avatar gerd

Optimizations in the 'replace_matched_substrings' function.


git-svn-id: https://godirepo.camlcity.org/svn/lib-xstr/[email protected] e4cd5726-45db-0310-8eb3-84e3bb775810
parent ae9fb33d
(* $Id: xstr_match.ml,v 1.5 1999/07/06 00:47:53 gerd Exp $
(* $Id: xstr_match.ml,v 1.6 1999/07/06 21:29:33 gerd Exp $
* ----------------------------------------------------------------------
* String matching
*)
......@@ -461,63 +461,82 @@ let replace_matched_substrings ml rl fl s =
let replace_at k =
if limit >= 0 & !n_repl >= limit then
raise Limit_exceeded;
List.iter
(fun v ->
v.found <- false;
v.begun <- false)
resl;
if match_string_at ml' s k then begin
(* interpret rl *)
let repltext =
List.map
(fun r ->
match r with
ReplaceLiteral s -> RLit s
| ReplaceVar v ->
if v.found then
RRegion (v.from, v.len)
else
RLit ""
| ReplaceFunction f ->
begin try
RLit (f ())
with
Not_found ->
raise Not_found
| Match_failure (_,_,_) ->
raise Not_found
end)
rl
in
let amount = all.len in
incr n_repl;
repltext, amount
[], (-1)
else begin
List.iter
(fun v ->
v.found <- false;
v.begun <- false)
resl;
if match_string_at ml' s k then begin
(* interpret rl *)
try
let repltext =
List.map
(fun r ->
match r with
ReplaceLiteral s -> RLit s
| ReplaceVar v ->
if v.found then
RRegion (v.from, v.len)
else
RLit ""
| ReplaceFunction f ->
begin try
RLit (f ())
with
Not_found ->
raise Not_found
| Match_failure (_,_,_) ->
raise Not_found
end)
rl
in
let amount = all.len in
incr n_repl;
repltext, amount
with
Not_found -> [], (-1)
end
else [], (-1)
end
else raise Not_found
in
let l = String.length s in
let ml'fc = first_character ml' in
let rec left_to_right trans k_gapstart k =
if k <= l then begin
(* Note k<=l: this criterion could be much better *)
let trans', k_gapstart', k' =
try
let repltext, amount = replace_at k in
repltext @ [RRegion(k_gapstart, k-k_gapstart)] @ trans,
k + amount,
(if amount=0 then k+1 else k+amount)
with
Not_found ->
trans, k_gapstart, k+1
| Limit_exceeded ->
trans, k_gapstart, l+1
in
left_to_right trans' k_gapstart' k'
end
let rec ltor k =
if k < (l-1) then begin
if not (member_of_set (Char.code s.[k]) ml'fc ) then begin
if not (member_of_set (Char.code s.[k+1]) ml'fc ) then begin
ltor (k+2)
end
else try_match trans k_gapstart (k+1)
end
else try_match trans k_gapstart k
end
else
if k <= l then
(* Note k<=l: this criterion could be much better *)
try_match trans k_gapstart k
else
RRegion(k_gapstart, k-k_gapstart-1) :: trans
in
ltor k
and try_match trans k_gapstart k =
let repltext, amount = replace_at k in
if amount >= 0 then begin
left_to_right
(repltext @ [RRegion(k_gapstart, k-k_gapstart)] @ trans)
(k + amount)
(if amount=0 then k+1 else k+amount)
end
else
RRegion(k_gapstart, k-k_gapstart-1) :: trans
left_to_right trans k_gapstart (l+1)
in
let with_anchors () =
......@@ -654,6 +673,9 @@ let mknegset s =
* History:
*
* $Log: xstr_match.ml,v $
* Revision 1.6 1999/07/06 21:29:33 gerd
* Optimizations in the 'replace_matched_substrings' function.
*
* Revision 1.5 1999/07/06 00:47:53 gerd
* Added optimization 'first_character'.
* Now 'run' raises an exception in the case of a success, and
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment