caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alex Baretta <alex@barettadeit.com>
To: Ocaml <caml-list@inria.fr>
Subject: Str library for channels?
Date: Thu, 24 Mar 2005 16:09:41 +0100	[thread overview]
Message-ID: <4242D835.4060301@barettadeit.com> (raw)

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/>


                 reply	other threads:[~2005-03-24 15:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4242D835.4060301@barettadeit.com \
    --to=alex@barettadeit.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).