aboutsummaryrefslogtreecommitdiff
path: root/lib/csv
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
committerSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
commit6b377719c10d5ab3343fd5221f99a4a21008e25a (patch)
treea7c1e9a820d339a2f161af3e09cf9e3161286796 /lib/csv
Initial commitmain
Diffstat (limited to 'lib/csv')
-rw-r--r--lib/csv/csv.ml30
-rw-r--r--lib/csv/dataType.ml21
-rw-r--r--lib/csv/dataType.mli8
-rwxr-xr-xlib/csv/dune6
4 files changed, 65 insertions, 0 deletions
diff --git a/lib/csv/csv.ml b/lib/csv/csv.ml
new file mode 100644
index 0000000..db7329d
--- /dev/null
+++ b/lib/csv/csv.ml
@@ -0,0 +1,30 @@
+open StdLabels
+
+type t = int
+
+let column_of_char = function
+ | 'A' .. 'Z' as c -> Char.code c - (Char.code 'A' - 1)
+ | 'a' .. 'z' as c -> Char.code c - (Char.code 'a' - 1)
+ | c -> raise (Invalid_argument ("column: " ^ Char.escaped c))
+
+let column_of_string : string -> int =
+ fun s ->
+ String.fold_left s ~init:0 ~f:(fun value c -> (value * 26) + column_of_char c)
+
+(** Accumulate the remaining for the successives divisions in a list. *)
+let rec _to_char ~b i =
+ if i > 0 then
+ let res = i mod 26 in
+ let res = if res = 0 then 26 else res in
+
+ let c = char_of_int @@ (res + 64) in
+ (* The modulo is accumulated in the list head, which is the expected
+ sequence *)
+ let b = c :: b in
+
+ _to_char ~b @@ ((i - res) / 26)
+ else b
+
+let column_to_string i =
+ let res = _to_char ~b:[] i in
+ List.to_seq res |> String.of_seq
diff --git a/lib/csv/dataType.ml b/lib/csv/dataType.ml
new file mode 100644
index 0000000..c582b9c
--- /dev/null
+++ b/lib/csv/dataType.ml
@@ -0,0 +1,21 @@
+let match_date = Re.Str.regexp {|[0-9]+/[0-9]+/[0-9]+|}
+
+type t =
+ | Null
+ | Error of string
+ | Content of string
+ | Integer of int
+ | Float of float
+
+let to_string = function
+ | Null -> ""
+ | Error s -> s
+ | Integer i -> string_of_int i
+ | Float f -> string_of_float f
+ | Content c -> (
+ match String.starts_with ~prefix:"0" c with
+ | false -> c
+ | true ->
+ (* If the string is a date, do not escape it *)
+ if Re.Str.string_match match_date c 0 then c
+ else String.concat "" [ "=\""; c; "\"" ])
diff --git a/lib/csv/dataType.mli b/lib/csv/dataType.mli
new file mode 100644
index 0000000..ebb8bc7
--- /dev/null
+++ b/lib/csv/dataType.mli
@@ -0,0 +1,8 @@
+type t =
+ | Null
+ | Error of string
+ | Content of string
+ | Integer of int
+ | Float of float
+
+val to_string : t -> string
diff --git a/lib/csv/dune b/lib/csv/dune
new file mode 100755
index 0000000..b0f4a72
--- /dev/null
+++ b/lib/csv/dune
@@ -0,0 +1,6 @@
+(library
+ (name importCSV)
+ (libraries
+ re
+ )
+)