package tablecloth-native

  1. Overview
  2. Docs

Source file TableclothString.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
module Comparator = TableclothComparator

type t = string

include TableclothComparator.Make (struct
  type nonrec t = t

  let compare = compare
end)

let initialize length ~f = Base.List.init length ~f |> Base.String.of_char_list

let repeat t ~count = Base.List.init count ~f:(fun _ -> t) |> Base.String.concat

let fromChar = Base.String.of_char

let from_char = fromChar

let fromArray characters =
  let open Base in
  Array.to_list characters |> String.of_char_list


let from_array = fromArray

let fromList = Base.String.of_char_list

let from_list = fromList

let length = String.length

let isEmpty t = length t = 0

let is_empty = isEmpty

let get = Base.String.get

let getAt a ~index =
  if index >= 0 && index < length a
  then Some (Base.String.get a index)
  else None


let get_at = getAt

let ( .?[] ) (string : string) (index : int) : char option = getAt string ~index

let uncons (s : string) : (char * string) option =
  match s with
  | "" ->
      None
  | s ->
      Some (s.[0], String.sub s 1 (String.length s - 1))


let dropLeft (s : string) ~(count : int) : string =
  Base.String.drop_prefix s count


let drop_left = dropLeft

let dropRight (s : string) ~(count : int) : string =
  Base.String.drop_suffix s count


let drop_right = dropRight

let split t ~(on : string) : string list =
  Str.split_delim (Str.regexp_string on) t


let startsWith t ~prefix = Base.String.is_prefix ~prefix t

let starts_with = startsWith

let endsWith t ~suffix = Base.String.is_suffix ~suffix t

let ends_with = endsWith

let toLowercase (s : string) : string = String.lowercase_ascii s

let to_lowercase = toLowercase

let toUppercase (s : string) : string = String.uppercase_ascii s

let to_uppercase = toUppercase

let uncapitalize (s : string) : string = String.uncapitalize_ascii s

let capitalize (s : string) : string = String.capitalize_ascii s

let isCapitalized (s : string) : bool = s = String.capitalize_ascii s

let is_capitalized = isCapitalized

let includes t ~substring : bool = Base.String.is_substring t ~substring

let reverse = Base.String.rev

let slice ?(to_ = 0) str ~from = String.sub str from (to_ - from)

let indexOf haystack needle =
  Base.String.Search_pattern.index
    ~pos:0
    ~in_:haystack
    (Base.String.Search_pattern.create needle)


let index_of = indexOf

let indexOfRight haystack needle =
  Base.String.Search_pattern.index_all
    ~may_overlap:false
    ~in_:haystack
    (Base.String.Search_pattern.create needle)
  |> Base.List.last


let index_of_right = indexOfRight

let insertAt t ~(index : int) ~(value : string) : string =
  let length = length t in
  (* Handle overflow *)
  let index = if index > length then length else index in
  (* Negative is an index from the end *)
  let index = if index < 0 then length + index else index in
  (* Handle case where it's still less than zero *)
  let index = if index < 0 then 0 else index in
  let startCount = index in
  let endCount = length - index in
  let start = dropRight ~count:endCount t in
  let end_ = dropLeft ~count:startCount t in
  String.concat "" [ start; value; end_ ]


let insert_at = insertAt

let toArray string = Base.String.to_list string |> Array.of_list

let to_array = toArray

let toList = Base.String.to_list

let to_list = toList

let trim string = Base.String.strip string

let trimLeft string = Base.String.lstrip string

let trim_left = trimLeft

let trimRight string = Base.String.rstrip string

let trim_right = trimRight

let padLeft string targetLength ~with_ =
  if length string >= targetLength
  then string
  else
    let paddingLength = targetLength - length string in
    let count = paddingLength / length with_ in
    let padding = slice (repeat with_ ~count) ~from:0 ~to_:paddingLength in
    padding ^ string


let pad_left = padLeft

let padRight string targetLength ~with_ =
  if length string >= targetLength
  then string
  else
    let paddingLength = targetLength - length string in
    let count = paddingLength / length with_ in
    let padding = slice (repeat with_ ~count) ~from:0 ~to_:paddingLength in
    string ^ padding


let pad_right = padRight

let forEach = Base.String.iter

let for_each = forEach

let fold s ~initial ~f = Base.String.fold s ~init:initial ~f

let equal = Base.String.equal

let compare = Base.String.compare