package core_extended

  1. Overview
  2. Docs
Extra components that are not as closely vetted or as stable as Core

Install

Dune Dependency

Authors

Maintainers

Sources

core_extended-v0.16.0.tar.gz
sha256=ccbb4e47e76edfd0b8aa5c2d3722e8813102c624851beec0cedd77ba13c4ccfb

doc/src/core_extended.delimited_kernel/parse_state.ml.html

Source file parse_state.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
open Core
module Row_buffer = Append_only_buffer

exception Bad_csv_formatting of string list * string

module Step = struct
  type t =
    | Row_start
    | Field_start
    | In_unquoted_field
    | In_quoted_field
    | In_quoted_field_after_quote
end

open Step

module Config = struct
  type 'a t =
    { sep : char
    ; quote : char
    ; use_quoting : bool
    ; strip : bool
    ; f : line_number:int -> 'a -> string Row_buffer.t -> 'a
    ; fields_used : int array option
    }

  let create ~sep ~quote ~strip ~f ~fields_used =
    let fields_used =
      match fields_used with
      | None -> None
      | Some fields_used as x
        when Array.is_sorted_strictly fields_used ~compare:Int.ascending -> x
      | Some fields_used ->
        Some
          (Array.of_list
             (List.dedup_and_sort (Array.to_list fields_used) ~compare:Int.ascending))
    in
    (match quote with
     | `Using c when Char.equal c sep ->
       invalid_arg
         "Delimited_kernel.Parse_state.create: cannot use the same character for [sep] \
          and [quote]"
     | `Using (('\r' | '\n') as c) ->
       invalid_arg
         (sprintf "Delimited_kernel.Parse_state.create: invalid [quote] character %C" c)
     | _ -> ());
    (match sep with
     | ('\r' | '\n') as c ->
       invalid_arg
         (sprintf "Delimited_kernel.Parse_state.create: invalid [sep] character %C" c)
     | _ -> ());
    { sep
    ; quote =
        (match quote with
         | `Using char -> char
         | `No_quoting -> '"')
    ; use_quoting =
        (match quote with
         | `Using _ -> true
         | `No_quoting -> false)
    ; strip
    ; f
    ; fields_used
    }
  ;;
end

module State = struct
  type 'a t =
    { field_buffer : string
    ; row_buffer : string list
    ; current_field : int
    ; current_line_number : int (** The current line number *)
    ; row_line_number : int (** The line number of the beginning of the current row *)
    ; acc : 'a
    ; step : Step.t
    ; finish : bool
    }

  let create ~init ~start_line_number =
    { acc = init
    ; step = Row_start
    ; field_buffer = ""
    ; row_buffer = []
    ; current_field = 0
    ; row_line_number = start_line_number
    ; current_line_number = start_line_number
    ; finish = false
    }
  ;;

  let acc { acc; _ } = acc
  let set_acc t acc = { t with acc }
end

type 'a t =
  { config : 'a Config.t
  ; state : 'a State.t
  }

let acc t = State.acc t.state
let set_acc t acc = { t with state = State.set_acc t.state acc }

let create
      ?(strip = false)
      ?(sep = ',')
      ?(quote = `Using '"')
      ?(start_line_number = 1)
      ~fields_used
      ~init
      ~f
      ()
  =
  { config = Config.create ~sep ~quote ~strip ~f ~fields_used
  ; state = State.create ~init ~start_line_number
  }
;;

module Char_kind = struct
  type t =
    | Backslash_r
    | Newline
    | Sep
    | Quote
    | Whitespace
    | Normal

  let of_char (t : _ Config.t) c =
    let open Char.Replace_polymorphic_compare in
    match c with
    | '\r' -> Backslash_r
    | '\n' -> Newline
    | _ when c = t.quote && t.use_quoting -> Quote
    | _ when c = t.sep -> Sep
    | _ when Char.is_whitespace c -> Whitespace
    | _ -> Normal
  ;;
end

module Mutable_state = struct
  (* We don't capture state [step] in here to avoid having to mutate the record at every
     single iteration *)
  type 'a t =
    { field_buffer : Buffer.t
    ; row_buffer : string Row_buffer.t
    ; config : 'a Config.t
    ; mutable current_field : int
    ; mutable enqueue : bool (* cache for should_enqueue *)
    ; mutable current_line_number : int
    ; mutable row_line_number : int
    ; mutable acc : 'a
    }

  let row_length t = Row_buffer.length t.row_buffer

  (* To reduce the number of allocations, we keep an array [fields_used] of the field
     indexes we care about. [current_field] is the position of the parser within the
     input row, and [next_field_index] is an index into the [fields_used] array
     indicating the next field that we need to store.

     If [fields_used] is None, we need to store every field.
  *)
  let should_enqueue fields_used state =
    match fields_used with
    | None -> true
    | Some array ->
      let next_field_index = row_length state in
      next_field_index < Array.length array
      && array.(next_field_index) = state.current_field
  ;;

  let create ~(config : 'a Config.t) ~(state : 'a State.t) =
    let field_buffer = Buffer.create (String.length state.field_buffer) in
    Buffer.add_string field_buffer state.field_buffer;
    let row_buffer = Row_buffer.of_list state.row_buffer in
    let state =
      { field_buffer
      ; row_buffer
      ; current_field = state.current_field
      ; enqueue = false
      ; config
      ; row_line_number = state.row_line_number
      ; current_line_number = state.current_line_number
      ; acc = state.acc
      }
    in
    if should_enqueue config.fields_used state then state.enqueue <- true;
    state
  ;;

  let emit_char t c = if t.enqueue then Buffer.add_char t.field_buffer c

  let emit_field state =
    if state.enqueue
    then (
      Row_buffer.append
        state.row_buffer
        (if state.config.strip
         then Shared.strip_buffer state.field_buffer
         else Buffer.contents state.field_buffer);
      Buffer.clear state.field_buffer);
    state.current_field <- state.current_field + 1;
    state.enqueue <- should_enqueue state.config.fields_used state
  ;;

  let emit_row state =
    let acc =
      state.config.f ~line_number:state.row_line_number state.acc state.row_buffer
    in
    state.acc <- acc;
    Row_buffer.lax_clear state.row_buffer;
    state.current_field <- 0;
    state.enqueue <- should_enqueue state.config.fields_used state;
    state.current_line_number <- state.current_line_number + 1;
    state.row_line_number <- state.current_line_number
  ;;

  let freeze ~step t : 'a State.t =
    { acc = t.acc
    ; step
    ; field_buffer = Buffer.contents t.field_buffer
    ; row_buffer = Row_buffer.to_list t.row_buffer
    ; current_field = t.current_field
    ; current_line_number = t.current_line_number
    ; row_line_number = t.row_line_number
    ; finish = false
    }
  ;;

  let incr_line_number t = t.current_line_number <- t.current_line_number + 1
end

let input_aux ~get t ~pos ~len input =
  if t.state.finish
  then
    raise_s
      [%message
        "Delimited.Expert.Parse_state.input: Cannot feed more input to a state that has \
         already been finalized"];
  let state = Mutable_state.create ~config:t.config ~state:t.state in
  let feed_one c step =
    match step, Char_kind.of_char t.config c with
    | _, Backslash_r -> step
    | (Row_start | Field_start), Quote -> In_quoted_field
    | (Row_start | Field_start), Sep ->
      Mutable_state.emit_field state;
      Field_start
    | (Row_start | Field_start), Newline ->
      Mutable_state.emit_field state;
      Mutable_state.emit_row state;
      Row_start
    | (Row_start | Field_start), (Normal | Whitespace) ->
      Mutable_state.emit_char state c;
      In_unquoted_field
    | In_unquoted_field, Sep ->
      Mutable_state.emit_field state;
      Field_start
    | In_unquoted_field, Newline ->
      Mutable_state.emit_field state;
      Mutable_state.emit_row state;
      Row_start
    | In_unquoted_field, (Whitespace | Normal) ->
      Mutable_state.emit_char state c;
      step
    | In_unquoted_field, Quote ->
      Mutable_state.emit_char state c;
      step
    | In_quoted_field, Quote -> In_quoted_field_after_quote
    | In_quoted_field, Newline ->
      Mutable_state.emit_char state c;
      Mutable_state.incr_line_number state;
      step
    | In_quoted_field, (Normal | Sep | Whitespace) ->
      Mutable_state.emit_char state c;
      step
    | In_quoted_field_after_quote, Quote ->
      (* doubled quote *)
      Mutable_state.emit_char state c;
      In_quoted_field
    | In_quoted_field_after_quote, _ when Char.equal c '0' ->
      Mutable_state.emit_char state '\000';
      In_quoted_field
    | In_quoted_field_after_quote, Sep ->
      Mutable_state.emit_field state;
      Field_start
    | In_quoted_field_after_quote, Newline ->
      Mutable_state.emit_field state;
      Mutable_state.emit_row state;
      Row_start
    | In_quoted_field_after_quote, Whitespace ->
      step
    | In_quoted_field_after_quote, Normal ->
      failwithf
        "In_quoted_field_after_quote looking at '%c' (line_number=%d)"
        c
        state.current_line_number
        ()
  in
  let loop_bound = len + pos in
  let rec loop i step =
    if i >= loop_bound
    then step
    else (
      let c = get input i in
      let step = feed_one c step in
      loop (i + 1) step)
  in
  let step = loop pos t.state.step in
  let state = Mutable_state.freeze ~step state in
  { t with state }
;;

let input t ?(pos = 0) ?len input =
  let len =
    match len with
    | None -> Bytes.length input - pos
    | Some len -> len
  in
  if len < 0 || pos < 0 || pos + len > Bytes.length input
  then invalid_arg "Delimited_kernel.Parse_state.input: index out of bound";
  input_aux ~get:Bytes.unsafe_get t ~pos ~len input
;;

let input_string t ?(pos = 0) ?len input =
  let len =
    match len with
    | None -> String.length input - pos
    | Some len -> len
  in
  if len < 0 || pos < 0 || pos + len > String.length input
  then invalid_arg "Delimited_kernel.Parse_state.input_string: index out of bound";
  input_aux ~get:String.unsafe_get t ~pos ~len input
;;

let current_line_number t = t.state.current_line_number

let at_beginning_of_row t =
  match t.state.step with
  | Row_start -> true
  | Field_start | In_quoted_field | In_quoted_field_after_quote | In_unquoted_field ->
    false
;;

let finish ({ config; state } as t) =
  if t.state.finish
  then t
  else (
    let state = Mutable_state.create ~config ~state in
    (match t.state.step with
     | Row_start -> ()
     | Field_start ->
       Mutable_state.emit_field state;
       Mutable_state.emit_row state
     | In_unquoted_field | In_quoted_field_after_quote ->
       Mutable_state.emit_field state;
       Mutable_state.emit_row state
     | In_quoted_field ->
       raise (Bad_csv_formatting (t.state.row_buffer, t.state.field_buffer)));
    let state = { (Mutable_state.freeze ~step:t.state.step state) with finish = true } in
    { t with state })
;;
OCaml

Innovation. Community. Security.