git.annexia.org
/
ocaml-csv.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
bde8fd1
)
Fixed a fairly grave bug in Csv.print_readable - one of those surprised it ever worke...
author
rich
<rich>
Fri, 21 Apr 2006 12:44:42 +0000
(12:44 +0000)
committer
rich
<rich>
Fri, 21 Apr 2006 12:44:42 +0000
(12:44 +0000)
csv.ml
patch
|
blob
|
history
diff --git
a/csv.ml
b/csv.ml
index
efb1bc0
..
82d8d02
100644
(file)
--- a/
csv.ml
+++ b/
csv.ml
@@
-1,6
+1,6
@@
(* csv.ml - comma separated values parser
*
(* csv.ml - comma separated values parser
*
- * $Id: csv.ml,v 1.
9 2006-02-23 15:24:25
rich Exp $
+ * $Id: csv.ml,v 1.
10 2006-04-21 12:44:42
rich Exp $
*)
(* The format of CSV files:
*)
(* The format of CSV files:
@@
-40,6
+40,8
@@
* subset of it into a matrix.
*)
* subset of it into a matrix.
*)
+open Printf
+
type t = string list list
exception Bad_CSV_file of string
type t = string list list
exception Bad_CSV_file of string
@@
-369,18
+371,18
@@
let save_out_readable chan csv =
match csv with
| [] -> []
match csv with
| [] -> []
- | r
:: _
->
- let
n = List.length r
in
- let lengths
= List.map (List.map String.length) csv
in
+ | r
ow1 :: rest
->
+ let
lengths_row1 = List.map String.length row1
in
+ let lengths
_rest = List.map (List.map String.length) rest
in
let max2rows r1 r2 =
let max2rows r1 r2 =
- let rp = List.combine r1 r2 in
+ let rp =
+ try List.combine r1 r2
+ with
+ Invalid_argument "List.combine" ->
+ failwith "Csv.save_out_readable: internal error: length r1 = %d, length r2 = %d" (List.length r1) (List.length r2) in
List.map (fun ((a : int), (b : int)) -> max a b) rp
in
List.map (fun ((a : int), (b : int)) -> max a b) rp
in
- let rec repeat x = function
- | 0 -> []
- | i -> x :: repeat x (i-1)
- in
- List.fold_left max2rows (repeat 0 n) lengths in
+ List.fold_left max2rows lengths_row1 lengths_rest in
(* Print out each cell at the correct width. *)
let rec repeat f = function
(* Print out each cell at the correct width. *)
let rec repeat f = function
@@
-393,9
+395,17
@@
let save_out_readable chan csv =
output_string chan cell;
output_char chan '\n'
| row -> (* Other. *)
output_string chan cell;
output_char chan '\n'
| row -> (* Other. *)
- let row = List.combine widths row in
+ (* Pair up each cell with its max width. *)
+ let row =
+ let rec loop = function
+ | ([], _) -> []
+ | (_, []) -> failwith "Csv.save_out_readable: internal error"
+ | (cell :: cells, width :: widths) ->
+ (cell, width) :: loop (cells, widths)
+ in
+ loop (row, widths) in
List.iter (
List.iter (
- fun (
width, cell
) ->
+ fun (
cell, width
) ->
output_string chan cell;
let n = String.length cell in
repeat (fun () -> output_char chan ' ') (width - n + 1)
output_string chan cell;
let n = String.length cell in
repeat (fun () -> output_char chan ' ') (width - n + 1)