forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilename.res
353 lines (332 loc) · 8.93 KB
/
filename.res
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
/* ************************************************************************ */
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/* ************************************************************************ */
let generic_quote = (quotequote, s) => {
let l = String.length(s)
let b = Buffer.create(l + 20)
Buffer.add_char(b, '\'')
for i in 0 to l - 1 {
if String.get(s, i) == '\'' {
Buffer.add_string(b, quotequote)
} else {
Buffer.add_char(b, String.get(s, i))
}
}
Buffer.add_char(b, '\'')
Buffer.contents(b)
}
/* This function implements the Open Group specification found here:
[[1]] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html
In step 1 of [[1]], we choose to return "." for empty input.
(for compatibility with previous versions of OCaml)
In step 2, we choose to process "//" normally.
Step 6 is not implemented: we consider that the [suffix] operand is
always absent. Suffixes are handled by [chop_suffix] and [chop_extension].
*/
let generic_basename = (is_dir_sep, current_dir_name, name) => {
let rec find_end = n =>
if n < 0 {
String.sub(name, 0, 1)
} else if is_dir_sep(name, n) {
find_end(n - 1)
} else {
find_beg(n, n + 1)
}
and find_beg = (n, p) =>
if n < 0 {
String.sub(name, 0, p)
} else if is_dir_sep(name, n) {
String.sub(name, n + 1, p - n - 1)
} else {
find_beg(n - 1, p)
}
if name == "" {
current_dir_name
} else {
find_end(String.length(name) - 1)
}
}
/* This function implements the Open Group specification found here:
[[2]] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html
In step 6 of [[2]], we choose to process "//" normally.
*/
let generic_dirname = (is_dir_sep, current_dir_name, name) => {
let rec trailing_sep = n =>
if n < 0 {
String.sub(name, 0, 1)
} else if is_dir_sep(name, n) {
trailing_sep(n - 1)
} else {
base(n)
}
and base = n =>
if n < 0 {
current_dir_name
} else if is_dir_sep(name, n) {
intermediate_sep(n)
} else {
base(n - 1)
}
and intermediate_sep = n =>
if n < 0 {
String.sub(name, 0, 1)
} else if is_dir_sep(name, n) {
intermediate_sep(n - 1)
} else {
String.sub(name, 0, n + 1)
}
if name == "" {
current_dir_name
} else {
trailing_sep(String.length(name) - 1)
}
}
module Unix = {
let current_dir_name = "."
let parent_dir_name = ".."
let dir_sep = "/"
let is_dir_sep = (s, i) => String.get(s, i) == '/'
let is_relative = n => String.length(n) < 1 || String.get(n, 0) != '/'
let is_implicit = n =>
is_relative(n) &&
((String.length(n) < 2 || String.sub(n, 0, 2) != "./") &&
(String.length(n) < 3 || String.sub(n, 0, 3) != "../"))
let check_suffix = (name, suff) =>
String.length(name) >= String.length(suff) &&
String.sub(name, String.length(name) - String.length(suff), String.length(suff)) == suff
let temp_dir_name = try Sys.getenv("TMPDIR") catch {
| Not_found => "/tmp"
}
let quote = x => generic_quote("'\\''", x)
let basename = x => generic_basename(is_dir_sep, current_dir_name, x)
let dirname = x => generic_dirname(is_dir_sep, current_dir_name, x)
}
module Win32 = {
let current_dir_name = "."
let parent_dir_name = ".."
let dir_sep = "\\"
let is_dir_sep = (s, i) => {
let c = String.get(s, i)
c == '/' || (c == '\\' || c == ':')
}
let is_relative = n =>
(String.length(n) < 1 || String.get(n, 0) != '/') &&
((String.length(n) < 1 || String.get(n, 0) != '\\') &&
(String.length(n) < 2 || String.get(n, 1) != ':'))
let is_implicit = n =>
is_relative(n) &&
((String.length(n) < 2 || String.sub(n, 0, 2) != "./") &&
((String.length(n) < 2 || String.sub(n, 0, 2) != ".\\") &&
((String.length(n) < 3 || String.sub(n, 0, 3) != "../") &&
(String.length(n) < 3 || String.sub(n, 0, 3) != "..\\"))))
let check_suffix = (name, suff) =>
String.length(name) >= String.length(suff) && {
let s = String.sub(name, String.length(name) - String.length(suff), String.length(suff))
String.lowercase_ascii(s) == String.lowercase_ascii(suff)
}
let temp_dir_name = try Sys.getenv("TEMP") catch {
| Not_found => "."
}
let quote = s => {
let l = String.length(s)
let b = Buffer.create(l + 20)
Buffer.add_char(b, '"')
let rec loop = i =>
if i == l {
Buffer.add_char(b, '"')
} else {
switch String.get(s, i) {
| '"' => loop_bs(0, i)
| '\\' => loop_bs(0, i)
| c =>
Buffer.add_char(b, c)
loop(i + 1)
}
}
and loop_bs = (n, i) =>
if i == l {
Buffer.add_char(b, '"')
add_bs(n)
} else {
switch String.get(s, i) {
| '"' =>
add_bs(2 * n + 1)
Buffer.add_char(b, '"')
loop(i + 1)
| '\\' => loop_bs(n + 1, i + 1)
| _ =>
add_bs(n)
loop(i)
}
}
and add_bs = n =>
for _j in 1 to n {
Buffer.add_char(b, '\\')
}
loop(0)
Buffer.contents(b)
}
let has_drive = s => {
let is_letter = param =>
switch param {
| 'A' .. 'Z' | 'a' .. 'z' => true
| _ => false
}
String.length(s) >= 2 && (is_letter(String.get(s, 0)) && String.get(s, 1) == ':')
}
let drive_and_path = s =>
if has_drive(s) {
(String.sub(s, 0, 2), String.sub(s, 2, String.length(s) - 2))
} else {
("", s)
}
let dirname = s => {
let (drive, path) = drive_and_path(s)
let dir = generic_dirname(is_dir_sep, current_dir_name, path)
drive ++ dir
}
let basename = s => {
let (_drive, path) = drive_and_path(s)
generic_basename(is_dir_sep, current_dir_name, path)
}
}
module Cygwin = {
let current_dir_name = "."
let parent_dir_name = ".."
let dir_sep = "/"
let is_dir_sep = Win32.is_dir_sep
let is_relative = Win32.is_relative
let is_implicit = Win32.is_implicit
let check_suffix = Win32.check_suffix
let temp_dir_name = Unix.temp_dir_name
let quote = Unix.quote
let basename = x => generic_basename(is_dir_sep, current_dir_name, x)
let dirname = x => generic_dirname(is_dir_sep, current_dir_name, x)
}
let (
current_dir_name,
parent_dir_name,
dir_sep,
is_dir_sep,
is_relative,
is_implicit,
check_suffix,
temp_dir_name,
quote,
basename,
dirname,
) = switch Sys.os_type {
| "Win32" => (
Win32.current_dir_name,
Win32.parent_dir_name,
Win32.dir_sep,
Win32.is_dir_sep,
Win32.is_relative,
Win32.is_implicit,
Win32.check_suffix,
Win32.temp_dir_name,
Win32.quote,
Win32.basename,
Win32.dirname,
)
| "Cygwin" => (
Cygwin.current_dir_name,
Cygwin.parent_dir_name,
Cygwin.dir_sep,
Cygwin.is_dir_sep,
Cygwin.is_relative,
Cygwin.is_implicit,
Cygwin.check_suffix,
Cygwin.temp_dir_name,
Cygwin.quote,
Cygwin.basename,
Cygwin.dirname,
)
| _ => /* normally "Unix" */
(
Unix.current_dir_name,
Unix.parent_dir_name,
Unix.dir_sep,
Unix.is_dir_sep,
Unix.is_relative,
Unix.is_implicit,
Unix.check_suffix,
Unix.temp_dir_name,
Unix.quote,
Unix.basename,
Unix.dirname,
)
}
let concat = (dirname, filename) => {
let l = String.length(dirname)
if l == 0 || is_dir_sep(dirname, l - 1) {
dirname ++ filename
} else {
dirname ++ (dir_sep ++ filename)
}
}
let chop_suffix = (name, suff) => {
let n = String.length(name) - String.length(suff)
if n < 0 {
invalid_arg("Filename.chop_suffix")
} else {
String.sub(name, 0, n)
}
}
let extension_len = name => {
let rec check = (i0, i) =>
if i < 0 || is_dir_sep(name, i) {
0
} else if String.get(name, i) == '.' {
check(i0, i - 1)
} else {
String.length(name) - i0
}
let rec search_dot = i =>
if i < 0 || is_dir_sep(name, i) {
0
} else if String.get(name, i) == '.' {
check(i, i - 1)
} else {
search_dot(i - 1)
}
search_dot(String.length(name) - 1)
}
let extension = name => {
let l = extension_len(name)
if l == 0 {
""
} else {
String.sub(name, String.length(name) - l, l)
}
}
let chop_extension = name => {
let l = extension_len(name)
if l == 0 {
invalid_arg("Filename.chop_extension")
} else {
String.sub(name, 0, String.length(name) - l)
}
}
let remove_extension = name => {
let l = extension_len(name)
if l == 0 {
name
} else {
String.sub(name, 0, String.length(name) - l)
}
}
let current_temp_dir_name = ref(temp_dir_name)
let set_temp_dir_name = s => current_temp_dir_name := s
let get_temp_dir_name = () => current_temp_dir_name.contents