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/write_intf.ml.html

Source file write_intf.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
open Core

module type To_string = sig
  type t

  val to_string : t -> string
end

module type M = sig
  (** Used to describe a way to create a single row of a CSV
      from a given type.
  *)
  type -'a t

  val empty : 'a t
  val column : ('a -> string) -> header:string -> 'a t
  val column_m : (module To_string with type t = 'a) -> header:string -> 'a t

  (** [default] is printed in place of [None], and if not supplied
      is the empty string.
  *)
  val column_opt : ?default:string -> ('a -> string) -> header:string -> 'a option t

  val column_m_opt
    :  ?default:string
    -> (module To_string with type t = 'a)
    -> header:string
    -> 'a option t

  (** [default] is used for every column in the case of [None]. *)
  val optional : ?default:string -> 'a t -> 'a option t

  val of_list : 'a t list -> 'a t
  val append : 'a t -> 'a t -> 'a t
  val contra_map : 'b t -> f:('a -> 'b) -> 'a t
  val map_headers : 'a t -> f:(string -> string) -> 'a t
  val headers : 'a t -> string list
  val to_columns : 'a t -> 'a -> string list

  (** Convert a list of ['a] to a CSV document in a string. *)
  val to_string
    :  ?quote:char
    -> ?sep:char
    -> ?line_breaks:[ `Unix | `Windows ] (** default is [`Windows] *)
    -> write_header:bool
    -> 'a t
    -> 'a list
    -> string

  (** Open for prefix operators useful for using with Fields.to_list.

      e.g.
      {[
        let csv =
          Delimited.Write.Fields_O.(
            Fields.to_list
              ~a_string:!!Fn.id
              ~a_date:!!Date.to_string
              ~mv:!>Long_short.csv)
          |> Delimited.Write.of_list
      ]}
  *)
  module Fields_O : sig
    (** Create a single column from a field of a record. *)
    val ( !! ) : ('a -> string) -> ('b, 'a) Field.t -> 'b t

    (** Nest a builder in a field of a record.

        Column headers will be prefixed with the name of the field.
    *)
    val ( !> ) : 'a t -> ('b, 'a) Field.t -> 'b t
  end

  module O : sig
    val ( <<| ) : 'b t -> ('a -> 'b) -> 'a t
    val ( <> ) : 'a t -> 'a t -> 'a t
  end

  module By_row : sig
    type row = string list

    (** Prints a valid csv file to a given channel.
        The [line_breaks] arg can be used to override the default line ending
        of "\r\n" (DOS/Windows line endings).
        Example ~line_breaks:`Unix to get *nix line endings
    *)
    val output_lines
      :  ?quote:char
      -> ?sep:char
      -> ?line_breaks:[ `Windows | `Unix ]
      -> Out_channel.t
      -> row list
      -> unit

    (** Convert one CSV line to a string. *)
    val line_to_string : ?quote:char -> ?sep:char -> row -> string
  end

  module Expert : sig
    (** Escape the a CSV field if need be.*)
    val maybe_escape_field : ?quote:char -> ?sep:char -> string -> string

    (** Escape a CSV (even if doesn't have any characters that require escaping).*)
    val escape_field : ?quote:char -> string -> string

    (** Get the escaped length of one quoted field (without the quotes). Returns
        None if the field doesn't need to be escaped. *)
    val quote_len : quote:char -> sep:char -> pos:int -> len:int -> string -> int option

    (** Copy and escapes the content of a field over from one string to
        another. This does not put the quotes in.*)
    val quote_blit
      :  quote:char
      -> src:string
      -> dst:Bytes.t
      -> src_pos:int
      -> dst_pos:int
      -> len:int
      -> int
  end

  (** Wraps [Stdio.Out_channel] for writing CSVs one line at a time. *)
  module Out_channel : sig
    type 'a write = 'a t
    type 'a t

    val create
      :  ?quote:char
      -> ?sep:char
      -> ?line_breaks:[ `Unix | `Windows ] (** default is [`Windows] *)
      -> write_header:bool
      -> 'a write
      -> Out_channel.t
      -> 'a t

    val channel : 'a t -> Out_channel.t
    val output_row : 'a t -> 'a -> unit
  end
end
OCaml

Innovation. Community. Security.