* Str library for channels?
@ 2005-03-24 15:09 Alex Baretta
0 siblings, 0 replies; only message in thread
From: Alex Baretta @ 2005-03-24 15:09 UTC (permalink / raw)
To: Ocaml
I have been confronted with the following little problem: finding the
first occurrence of a string matching a given regular expression in an
indefinitely long file. My solution was based on ocamllex. Here it is.
let expr = ... as expr
rule find = parse
| ['\000'-'\255'] { find lexbuf }
| expr { print_string expr; copy lexbuf }
and copy = shortest
| ([^'\n']* '\n') as line { print_string line; copy lexbuf }
| ([^'\n']* as line) eof { print_string line }
{ find (Lexing.from_channel stdin) }
I think that using ocamllex for such a minimal task is rather
cumbersome. What I would like to do is the following:
let re = Str.regexp ...
let () = Pervasives.seek_in (Str.seek_forward re stdin)
let () = copy_channel stdin stdout
Where the following is defined somewhere.
let copy_channel ?(s=String.create 1024) in_ch out_ch =
let length = String.length s in
let rec loop bytes_read =
output out_ch s 0 bytes_read;
loop (input in_ch s 0 length)
in loop (input in_ch s 0 length)
Str does not support scanning files. Is this a limitation in the API or
in the regexp engine? Could Str be extended to handle files as well as
strings?
Alex
--
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. +39 02 370 111 55
fax. +39 02 370 111 54
Our technology:
The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>
The FreerP Project
<http://www.freerp.org/>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2005-03-24 15:09 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-24 15:09 Str library for channels? Alex Baretta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox