package fpath-sexp0

  1. Overview
  2. Docs

Source file fsegment.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
(*********************************************************************************)
(*  fpath-base: Extending [Fpath] to use alongside [Sexplib0] and/or [Base]      *)
(*  SPDX-FileCopyrightText: 2023-2025 Mathieu Barbin <mathieu.barbin@gmail.com>  *)
(*  SPDX-License-Identifier: MIT                                                 *)
(*********************************************************************************)

open! Stdlib_compat

type t = string

let compare = String.compare
let equal = String.equal
let sexp_of_t t = Sexplib0.Sexp.Atom t
let hash = String.hash
let seeded_hash = String.seeded_hash
let dir_sep_char = Fpath.dir_sep.[0]

let no_dir_sep =
  if Char.equal dir_sep_char '/'
  then fun c -> not (Char.equal c '/')
  else fun [@coverage off] c -> not (Char.equal c '/' || Char.equal c dir_sep_char)
;;

let invariant t =
  String.length t > 0
  && String.for_all (fun c -> no_dir_sep c && not (Char.equal c '\000')) t
;;

let to_string t = t

let of_string s =
  if invariant s then Ok s else Error (`Msg (Printf.sprintf "invalid file segment %S" s))
;;

let v t =
  match of_string t with
  | Ok t -> t
  | Error (`Msg s) -> invalid_arg ("Fsegment.v: " ^ s)
;;

let dot = v "."
let dot_dot = v ".."
let dot_git = v ".git"
let dot_hg = v ".hg"