Browse thread
[Caml-list] Better option to read a file
-
Agustín_Valverde
- Pietro Abate
- Christoph Bauer
- Jean-Christophe Filliatre
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...> |
| Subject: | Re: [Caml-list] Better option to read a file |
Agustín Valverde writes:
> Second:
>
> let rec unir c ac = unir ac^(Char.escaped c);;
>
> let leer2 fl =
> let form = ref "" in
> let c = ref '-' in
> let arch = open_in fl in
> (try
> (while true do (c := input_char arch); (if !c != '\n' then (form
> := unir !c !form) else ()) done)
> with End_of_file -> close_in arch);
> !form;;
Note that this function is very inefficient: you are indeed building a
lot of intermediate strings with "unir", resulting in a quadratic
space (even if the final string occupies linear space).
Using a buffer as suggested by Christoph is clearly better (the module
Buffer from ocaml standard library is doubling its internal string
buffer as needed, without you to worry about it).
> I also have a parser to convert the string, could I to improve these
> functions merging them with the parser in some way?
The use of ocamllex in combination with a buffer is both easy and
efficient. I sketch it:
======================================================================
{
open Lexing
let buf = Buffer.create 1024
}
rule read = parse
| "\\n" { Buffer.add_char buf '\n'; read lexbuf }
| "\\t" { Buffer.add_char buf '\t'; read lexbuf }
| "\\\\" { Buffer.add_char buf '\\'; read lexbuf }
| _ { Buffer.add_string buf (lexeme lexbuf); read lexbuf }
| eof { Buffer.contents buf }
{
let read_file f =
let cin = open_in f in
let lb = from_channel cin in
let s = read lb in
close_in cin;
s
}
======================================================================
--
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners