Source file fpath.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
open Astring
let bytes_unsafe_set = Bytes.unsafe_set
let string_unsafe_get = String.unsafe_get
let err_invalid_seg s = strf "%a: invalid segment" String.dump s
let err_invalid_ext s = strf "%a: invalid extension" String.dump s
let windows = Sys.os_type = "Win32"
let dir_sep_char = if windows then '\\' else '/'
let dir_sep = String.of_char dir_sep_char
let dir_sep_sub = String.sub dir_sep
let not_dir_sep c = c <> dir_sep_char
let dot = "."
let dot_dir = dot ^ dir_sep
let dot_dir_sub = String.sub dot_dir
let dotdot = ".."
let dotdot_dir = dotdot ^ dir_sep
let dotdot_dir_sub = String.sub dotdot_dir
module Windows = struct
let is_unc_path p = String.is_prefix "\\\\" p
let has_drive p = String.exists (Char.equal ':') p
let non_unc_path_start p = match String.find (Char.equal ':') p with
| None -> 0
| Some i -> i + 1
let parse_unc s =
let p = String.sub ~start:2 s in
let not_bslash c = c <> '\\' in
let parse_seg p = String.Sub.span ~min:1 ~sat:not_bslash p in
let ensure_root r = Some (if String.Sub.is_empty r then (s ^ "\\") else s)
in
match parse_seg p with
| (seg1, _) when String.Sub.is_empty seg1 -> None
| (seg1, rest) ->
let seg1_len = String.Sub.length seg1 in
match String.Sub.get_head ~rev:true seg1 with
| '.' when seg1_len = 1 ->
begin match parse_seg (String.Sub.tail rest) with
| (seg, _) when String.Sub.is_empty seg -> None
| (_, rest) -> ensure_root rest
end
| '?' when seg1_len = 1 ->
begin match parse_seg (String.Sub.tail rest) with
| (seg2, _) when String.Sub.is_empty seg2 -> None
| (seg2, rest) ->
if (String.Sub.get_head ~rev:true seg2 = ':')
then (ensure_root rest) else
if not (String.Sub.equal_bytes seg2 (String.sub "UNC"))
then begin
match parse_seg (String.Sub.tail rest) with
| (seg, _) when String.Sub.is_empty seg -> None
| (_, rest) -> ensure_root rest
end else begin
match parse_seg (String.Sub.tail rest) with
| (seg, _) when String.Sub.is_empty seg -> None
| (_, rest) ->
match parse_seg (String.Sub.tail rest) with
| (seg, _) when String.Sub.is_empty seg -> None
| (_, rest) -> ensure_root rest
end
end
| _ ->
begin match parse_seg (String.Sub.tail rest) with
| (seg, _) when String.Sub.is_empty seg -> None
| (_, rest) -> ensure_root rest
end
let sub_split_volume p =
let split_before i = String.sub p ~stop:i, String.sub p ~start:i in
if not (is_unc_path p) then
begin match String.find (Char.equal ':') p with
| None -> String.Sub.empty, String.sub p
| Some i -> split_before (i + 1)
end
else
let bslash ~start = match String.find ~start (Char.equal '\\') p with
| None -> assert false | Some i -> i
in
let i = bslash ~start:2 in
let j = bslash ~start:(i + 1) in
match p.[i-1] with
| '.' when i = 3 -> split_before j
| '?' when i = 3 ->
if p.[j-1] = ':' then split_before j else
if (String.Sub.equal_bytes
(String.sub p ~start:(i + 1) ~stop:j)
(String.sub "UNC"))
then split_before (bslash ~start:((bslash ~start:(j + 1)) + 1))
else split_before (bslash ~start:(j + 1))
| _ -> split_before j
let is_root p =
let _, path = sub_split_volume p in
String.Sub.length path = 1 && String.Sub.get path 0 = dir_sep_char
end
module Posix = struct
let has_volume p = String.is_prefix "//" p
let is_root p = String.equal p dir_sep || String.equal p "//"
end
let is_seg_windows s =
let valid c = c <> '\x00' && c <> dir_sep_char && c <> '/' in
String.for_all valid s
let is_seg_posix s =
let valid c = c <> '\x00' && c <> dir_sep_char in
String.for_all valid s
let is_seg = if windows then is_seg_windows else is_seg_posix
let _split_last_seg p = String.Sub.span ~rev:true ~sat:not_dir_sep p
let _sub_last_seg p = String.Sub.take ~rev:true ~sat:not_dir_sep p
let _sub_last_non_empty_seg p =
let dir, last = _split_last_seg p in
match String.Sub.is_empty last with
| false -> last
| true -> _sub_last_seg (String.Sub.tail ~rev:true dir)
let _split_last_non_empty_seg p =
let (dir, last_seg as r) = _split_last_seg p in
match String.Sub.is_empty last_seg with
| false -> r, true
| true -> _split_last_seg (String.Sub.tail ~rev:true dir), false
let sub_last_seg_windows p = _sub_last_seg (snd (Windows.sub_split_volume p))
let sub_last_seg_posix p = _sub_last_seg (String.sub p)
let sub_last_seg = if windows then sub_last_seg_windows else sub_last_seg_posix
let sub_last_non_empty_seg_windows p =
_sub_last_non_empty_seg (snd (Windows.sub_split_volume p))
let sub_last_non_empty_seg_posix p =
_sub_last_non_empty_seg (String.sub p)
let sub_last_non_empty_seg =
if windows then sub_last_non_empty_seg_windows else
sub_last_non_empty_seg_posix
let is_rel_seg = function "." | ".." -> true | _ -> false
let sub_is_rel_seg seg = match String.Sub.length seg with
| 1 when String.Sub.get seg 0 = '.' -> true
| 2 when String.Sub.get seg 0 = '.' && String.Sub.get seg 1 = '.' -> true
| _ -> false
let sub_is_dir_seg seg = match String.Sub.length seg with
| 0 -> true
| 1 when String.Sub.get seg 0 = '.' -> true
| 2 when String.Sub.get seg 0 = '.' && String.Sub.get seg 1 = '.' -> true
| _ -> false
let segs_of_path p = String.cuts ~sep:dir_sep p
let segs_to_path segs = String.concat ~sep:dir_sep segs
type t = string
let err s = Error (`Msg (strf "%a: invalid path" String.dump s))
let validate_and_collapse_seps p =
let max_idx = String.length p - 1 in
let rec with_buf b last_sep k i =
if i > max_idx then Ok (Bytes.sub_string b 0 k) else
let c = string_unsafe_get p i in
if c = '\x00' then err p else
if c <> dir_sep_char
then (bytes_unsafe_set b k c; with_buf b false (k + 1) (i + 1)) else
if not last_sep
then (bytes_unsafe_set b k c; with_buf b true (k + 1) (i + 1)) else
with_buf b true k (i + 1)
in
let rec try_no_alloc last_sep i =
if i > max_idx then Ok p else
let c = string_unsafe_get p i in
if c = '\x00' then err p else
if c <> dir_sep_char then try_no_alloc false (i + 1) else
if not last_sep then try_no_alloc true (i + 1) else
let b = Bytes.of_string p in
with_buf b true i (i + 1)
in
let start =
if max_idx > 0 then (if p.[0] = dir_sep_char then 1 else 0) else 0
in
try_no_alloc false start
let of_string_windows s =
if s = "" then err s else
let p = String.map (fun c -> if c = '/' then '\\' else c) s in
match validate_and_collapse_seps p with
| Error _ as e -> e
| Ok p as some ->
if Windows.is_unc_path p then
(match Windows.parse_unc p with None -> err s | Some p -> Ok p)
else
match String.find (Char.equal ':') p with
| None -> some
| Some i when i = String.length p - 1 -> err p
| Some _ -> Ok p
let of_string_posix p = if p = "" then err p else validate_and_collapse_seps p
let of_string = if windows then of_string_windows else of_string_posix
let v s = match of_string s with
| Ok p -> p
| Error (`Msg m) -> invalid_arg m
let add_seg p seg =
if not (is_seg seg) then invalid_arg (err_invalid_seg seg);
let sep = if p.[String.length p - 1] = dir_sep_char then "" else dir_sep in
String.concat ~sep [p; seg]
let append_posix p0 p1 =
if p1.[0] = dir_sep_char then p1 else
let sep = if p0.[String.length p0 - 1] = dir_sep_char then "" else dir_sep in
String.concat ~sep [p0; p1]
let append_windows p0 p1 =
if Windows.is_unc_path p1 || Windows.has_drive p1 then p1 else
if p1.[0] = dir_sep_char then p1 else
let sep = if p0.[String.length p0 - 1] = dir_sep_char then "" else dir_sep in
String.concat ~sep [p0; p1]
let append = if windows then append_windows else append_posix
let ( / ) = add_seg
let ( // ) = append
let split_volume_windows p =
let vol, path = Windows.sub_split_volume p in
String.Sub.to_string vol, String.Sub.to_string path
let split_volume_posix p =
if Posix.has_volume p then dir_sep, String.with_range ~first:1 p else "", p
let split_volume = if windows then split_volume_windows else split_volume_posix
let segs_windows p =
let _, path = Windows.sub_split_volume p in
segs_of_path (String.Sub.to_string path)
let segs_posix p =
let segs = segs_of_path p in
if Posix.has_volume p then List.tl segs else segs
let segs = if windows then segs_windows else segs_posix
let is_dir_path p = sub_is_dir_seg (sub_last_seg p)
let is_file_path p = not (is_dir_path p)
let to_dir_path p = add_seg p ""
let filename p = match String.Sub.to_string (sub_last_seg p) with
| "" | "." | ".." -> ""
| filename -> filename
let sub_is_root p = String.Sub.length p = 1 && String.Sub.get p 0 = dir_sep_char
let _split_base p =
let dir, last_seg = _split_last_seg p in
match String.Sub.is_empty dir with
| true -> dot_dir_sub, String.Sub.to_string p
| false ->
match String.Sub.is_empty last_seg with
| false -> dir, String.Sub.to_string last_seg
| true ->
let dir_file = String.Sub.tail ~rev:true dir in
let dir, dir_last_seg = _split_last_seg dir_file in
match String.Sub.is_empty dir with
| true -> dot_dir_sub, String.Sub.to_string p
| false -> dir, String.Sub.(to_string (extend dir_last_seg))
let split_base_windows p =
let vol, path = Windows.sub_split_volume p in
if sub_is_root path then p, dot_dir else
let dir, b = _split_base path in
String.Sub.(base_string (append vol dir)), b
let split_base_posix p =
if Posix.is_root p then p, dot_dir else
let dir, b = _split_base (String.sub p) in
String.Sub.to_string dir, b
let split_base = if windows then split_base_windows else split_base_posix
let base p = snd (split_base p)
let _basename p = match String.Sub.to_string (_sub_last_non_empty_seg p) with
| "." | ".." -> ""
| basename -> basename
let basename_windows p =
let vol, path = Windows.sub_split_volume p in
if sub_is_root path then "" else _basename path
let basename_posix p = if Posix.is_root p then "" else _basename (String.sub p)
let basename p = if windows then basename_windows p else basename_posix p
let _parent p =
let (dir, seg), is_last = _split_last_non_empty_seg p in
let dsep = if is_last then dir_sep_sub else String.Sub.empty in
if sub_is_rel_seg seg then [p; dsep; dotdot_dir_sub] else
if String.Sub.is_empty dir then [dot_dir_sub] else [dir]
let parent_windows p =
let vol, path = Windows.sub_split_volume p in
if sub_is_root path then p else
String.Sub.(base_string @@ concat (vol :: _parent path))
let parent_posix p =
if Posix.is_root p then p else
String.Sub.(base_string @@ concat (_parent (String.sub p)))
let parent = if windows then parent_windows else parent_posix
let rem_empty_seg_windows p =
let vol, path = Windows.sub_split_volume p in
if sub_is_root path then p else
let max = String.Sub.stop_pos path - 1 in
if String.get p max <> dir_sep_char then p else
String.with_index_range p ~last:(max - 1)
let rem_empty_seg_posix p = match String.length p with
| 1 -> p
| 2 ->
if p.[0] <> dir_sep_char && p.[1] = dir_sep_char
then String.of_char p.[0]
else p
| len ->
let max = len - 1 in
if p.[max] <> dir_sep_char then p else
String.with_index_range p ~last:(max - 1)
let rem_empty_seg =
if windows then rem_empty_seg_windows else rem_empty_seg_posix
let normalize_rel_segs segs =
let rec loop acc = function
| "." :: [] -> ("" :: acc)
| "." :: rest -> loop acc rest
| ".." :: rest ->
begin match acc with
| ".." :: _ | [] -> loop (".." :: acc) rest
| seg :: acc ->
match rest with
| [] -> ("" :: acc)
| rest -> loop acc rest
end
| seg :: rest -> loop (seg :: acc) rest
| [] ->
match acc with
| ".." :: _ -> ("" :: acc)
| [] -> [""]
| acc -> acc
in
List.rev (loop [] segs)
let normalize_segs = function
| "" :: segs ->
let rec rem_dotdots = function ".." :: ss -> rem_dotdots ss | ss -> ss in
"" :: (rem_dotdots @@ normalize_rel_segs segs)
| segs ->
match normalize_rel_segs segs with
| [""] -> ["."; ""]
| segs -> segs
let normalize_windows p =
let vol, path = Windows.sub_split_volume p in
let path = String.Sub.to_string path in
let path = segs_to_path @@ normalize_segs (segs_of_path path) in
String.Sub.(to_string (concat [vol; String.sub path]))
let normalize_posix p =
let has_volume = Posix.has_volume p in
let segs = segs_of_path p in
let segs = normalize_segs @@ if has_volume then List.tl segs else segs in
let segs = if has_volume then "" :: segs else segs in
segs_to_path segs
let normalize = if windows then normalize_windows else normalize_posix
let is_prefix prefix p =
if not (String.is_prefix prefix p) then false else
let suff_start = String.length prefix in
if prefix.[suff_start - 1] = dir_sep_char then true else
if suff_start = String.length p then true else
p.[suff_start] = dir_sep_char
let _prefix_last_index p0 p1 =
let l0 = String.length p0 in
let l1 = String.length p1 in
let p0, p1, max = if l0 < l1 then p0, p1, l0 - 1 else p1, p0, l1 - 1 in
let rec loop last_dir_sep i p0 p1 = match i > max || p0.[i] <> p1.[i] with
| false ->
let last_dir_sep = if p0.[i] = dir_sep_char then i else last_dir_sep in
loop last_dir_sep (i + 1) p0 p1
| true ->
if i = 0 then None else
let last = i - 1 in
if last_dir_sep = last then Some last else
match last = max with
| true ->
if l1 = l0 then Some last else
if p1.[i] = dir_sep_char then Some last else
if last_dir_sep <> -1 then Some last_dir_sep else None
| false ->
if last_dir_sep <> -1 then Some last_dir_sep else None
in
loop (-1) 0 p0 p1
let find_prefix_windows p0 p1 = match _prefix_last_index p0 p1 with
| None -> None
| Some i ->
let v0_len = String.Sub.length (fst (Windows.sub_split_volume p0)) in
let v1_len = String.Sub.length (fst (Windows.sub_split_volume p1)) in
let max_vlen = if v0_len > v1_len then v0_len else v1_len in
if i < max_vlen then None else Some (String.with_index_range p0 ~last:i)
let find_prefix_posix p0 p1 = match _prefix_last_index p0 p1 with
| None -> None
| Some 0 when Posix.has_volume p0 || Posix.has_volume p1 -> None
| Some i -> Some (String.with_index_range p0 ~last:i)
let find_prefix = if windows then find_prefix_windows else find_prefix_posix
let rem_prefix prefix p = match is_prefix prefix p with
| false -> None
| true ->
match String.length prefix with
| len when len = String.length p -> None
| len ->
let first = if p.[len] = dir_sep_char then len + 1 else len in
match String.with_index_range p ~first with
| "" -> Some dot_dir
| q -> Some q
let _relativize ~root p =
let root =
let root = normalize root in
if root.[String.length root - 1] = dir_sep_char then root else
root ^ dir_sep
in
let p = normalize p in
let rec walk root p = match root, p with
| (".." :: _, s :: _) when s <> ".." ->
None
| (sr :: root, sp :: (_ :: _ as p)) when sr = sp ->
walk root p
| [""], [""] ->
Some (segs_to_path ["."; ""])
| root, p ->
let segs = List.fold_left (fun acc _ -> dotdot :: acc) p (List.tl root) in
Some (segs_to_path segs)
in
match segs root, segs p with
| ("" :: _, s :: _) when s <> "" -> None
| (s :: _, "" :: _) when s <> "" -> None
| ["."; ""], p ->
Some (segs_to_path p)
| root, p ->
walk root p
let relativize_windows ~root p =
let rvol, root = Windows.sub_split_volume root in
let pvol, p = Windows.sub_split_volume p in
if not (String.Sub.equal_bytes rvol pvol) then None else
let root = String.Sub.to_string root in
let p = String.Sub.to_string p in
_relativize ~root p
let relativize_posix ~root p = _relativize ~root p
let relativize = if windows then relativize_windows else relativize_posix
let is_rooted ~root p = match relativize ~root p with
| None -> false
| Some r -> not (String.equal dotdot r || String.is_prefix dotdot_dir r)
let is_rel_posix p = p.[0] <> dir_sep_char
let is_rel_windows p =
if Windows.is_unc_path p then false else
p.[Windows.non_unc_path_start p] <> dir_sep_char
let is_rel = if windows then is_rel_windows else is_rel_posix
let is_abs p = not (is_rel p)
let is_root = if windows then Windows.is_root else Posix.is_root
let is_current_dir_posix ?(prefix = false) p = match prefix with
| false -> String.equal dot p || String.equal dot_dir p
| true -> String.equal dot p || String.is_prefix dot_dir p
let is_current_dir_windows ?(prefix = false) p =
if Windows.is_unc_path p then false else
let start = Windows.non_unc_path_start p in
match String.length p - start with
| 1 -> p.[start] = '.'
| n when n = 2 || prefix -> p.[start] = '.' && p.[start + 1] = dir_sep_char
| _ -> false
let is_current_dir =
if windows then is_current_dir_windows else is_current_dir_posix
let is_parent_dir_posix ?(prefix = false) p = match prefix with
| false -> String.equal dotdot p || String.equal dotdot_dir p
| true -> String.equal dotdot p || String.is_prefix dotdot_dir p
let is_parent_dir_windows ?(prefix = false) p =
if Windows.is_unc_path p then false else
let start = Windows.non_unc_path_start p in
match String.length p - start with
| 1 -> false
| 2 -> p.[start] = '.' && p.[start + 1] = '.'
| n when n = 3 || prefix ->
p.[start] = '.' && p.[start + 1] = '.' && p.[start + 2] = dir_sep_char
| _ -> false
let is_parent_dir =
if windows then is_parent_dir_windows else is_parent_dir_posix
let is_dotfile p = match basename p with | "" -> false | s -> s.[0] = '.'
let equal = String.equal
let compare = String.compare
let to_string p = p
let pp ppf p = Format.pp_print_string ppf (to_string p)
let dump ppf p = String.dump ppf (to_string p)
type ext = string
let ext_sep_char = '.'
let ext_sep = String.of_char ext_sep_char
let ext_sep_sub = String.Sub.of_char ext_sep_char
let eq_ext_sep c = c = ext_sep_char
let neq_ext_sep c = c <> ext_sep_char
let rec sub_multi_ext seg =
let first_not_sep = String.Sub.drop ~sat:eq_ext_sep seg in
String.Sub.drop ~sat:neq_ext_sep first_not_sep
let sub_single_ext seg =
let name_dot, ext = String.Sub.span ~rev:true ~sat:neq_ext_sep seg in
if String.Sub.exists neq_ext_sep name_dot
then String.Sub.extend ~max:1 ~rev:true ext
else String.Sub.empty
let sub_ext ?(multi = false) seg =
if multi then sub_multi_ext seg else sub_single_ext seg
let sub_get_ext ?multi p = sub_ext ?multi (sub_last_non_empty_seg p)
let get_ext ?multi p = String.Sub.to_string (sub_get_ext ?multi p)
let has_ext e p =
let ext = sub_get_ext ~multi:true p in
if String.Sub.is_empty ext then false else
if not (String.(Sub.is_suffix (sub e) ext)) then false else
if not (String.is_empty e) && e.[0] = ext_sep_char then true else
let dot_index = String.Sub.length ext - String.length e - 1 in
String.Sub.get ext dot_index = ext_sep_char
let mem_ext exts p = List.exists (fun ext -> has_ext ext p) exts
let exists_ext ?(multi = false) p =
let ext = sub_get_ext ~multi p in
if multi then String.Sub.exists eq_ext_sep (String.Sub.tail ext) else
not (String.Sub.is_empty ext)
let add_ext e p =
if String.is_empty e then p else
if not (is_seg e) then invalid_arg (err_invalid_ext e) else
let seg = sub_last_non_empty_seg p in
if sub_is_dir_seg seg then p else
let e_has_dot = e.[0] = ext_sep_char in
let maybe_dot = if e_has_dot then String.Sub.empty else ext_sep_sub in
let has_empty = p.[String.length p - 1] = dir_sep_char in
let maybe_empty = if has_empty then dir_sep_sub else String.Sub.empty in
let seg_end = String.Sub.stop_pos seg - 1 in
let prefix = String.sub_with_index_range ~last:seg_end p in
let path = [prefix; maybe_dot; String.sub e; maybe_empty] in
String.Sub.(base_string (concat path))
let _split_ext ?multi p =
let ext = sub_get_ext ?multi p in
if String.Sub.is_empty ext then p, ext else
let before_ext = String.Sub.start_pos ext - 1 in
if String.Sub.stop_pos ext = String.length p
then String.with_index_range p ~last:before_ext, ext else
let prefix = String.sub_with_index_range p ~last:before_ext in
String.Sub.(base_string (concat [prefix; dir_sep_sub])), ext
let rem_ext ?multi p = fst (_split_ext ?multi p)
let set_ext ?multi e p = add_ext e (rem_ext ?multi p)
let split_ext ?multi p =
let p, ext = _split_ext ?multi p in
p, String.Sub.to_string ext
let ( + ) p e = add_ext e p
let ( -+ ) p e = set_ext e p
type path = t
module Set = struct
include Set.Make (String)
let pp ?sep:(pp_sep = Format.pp_print_cut) pp_elt ppf ps =
let pp_elt elt is_first =
if is_first then () else pp_sep ppf ();
Format.fprintf ppf "%a" pp_elt elt; false
in
ignore (fold pp_elt ps true)
let dump_path = dump
let dump ppf ss =
let pp_elt elt is_first =
if is_first then () else Format.fprintf ppf "@ ";
Format.fprintf ppf "%a" dump_path elt;
false
in
Format.fprintf ppf "@[<1>{";
ignore (fold pp_elt ss true);
Format.fprintf ppf "}@]";
()
let err_empty () = invalid_arg "empty set"
let err_absent p ps =
invalid_arg (strf "%a not in set %a" dump_path p dump ps)
let get_min_elt ps = try min_elt ps with Not_found -> err_empty ()
let min_elt ps = try Some (min_elt ps) with Not_found -> None
let get_max_elt ps = try max_elt ps with Not_found -> err_empty ()
let max_elt ps = try Some (max_elt ps) with Not_found -> None
let get_any_elt ps = try choose ps with Not_found -> err_empty ()
let choose ps = try Some (choose ps) with Not_found -> None
let get p ps = try find p ps with Not_found -> err_absent p ps
let find p ps = try Some (find p ps) with Not_found -> None
let of_list = List.fold_left (fun acc s -> add s acc) empty
end
module Map = struct
include Map.Make (String)
let err_empty () = invalid_arg "empty map"
let err_absent s = invalid_arg (strf "%s is not bound in map" s)
let get_min_binding m = try min_binding m with Not_found -> err_empty ()
let min_binding m = try Some (min_binding m) with Not_found -> None
let get_max_binding m = try max_binding m with Not_found -> err_empty ()
let max_binding m = try Some (max_binding m) with Not_found -> None
let get_any_binding m = try choose m with Not_found -> err_empty ()
let choose m = try Some (choose m) with Not_found -> None
let get k s = try find k s with Not_found -> err_absent k
let find k m = try Some (find k m) with Not_found -> None
let dom m = fold (fun k _ acc -> Set.add k acc) m Set.empty
let of_list bs = List.fold_left (fun m (k,v) -> add k v m) empty bs
let pp ?sep:(pp_sep = Format.pp_print_cut) pp_binding ppf (m : 'a t) =
let pp_binding k v is_first =
if is_first then () else pp_sep ppf ();
pp_binding ppf (k, v); false
in
ignore (fold pp_binding m true)
let dump pp_v ppf m =
let pp_binding k v is_first =
if is_first then () else Format.fprintf ppf "@ ";
Format.fprintf ppf "@[<1>(@[%a@],@ @[%a@])@]" dump k pp_v v;
false
in
Format.fprintf ppf "@[<1>{";
ignore (fold pp_binding m true);
Format.fprintf ppf "}@]";
()
end
type set = Set.t
type 'a map = 'a Map.t