Skip to content

Commit

Permalink
separate UtfUtil from AbsPath
Browse files Browse the repository at this point in the history
  • Loading branch information
gfngfn committed Sep 7, 2024
1 parent a518f93 commit 8040121
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
25 changes: 2 additions & 23 deletions src-util/absPath.ml
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@

(* TODO: separate UTF-8-related functions to one module *)
let decode_utf8 (str_utf8 : string) : Uchar.t list =
let decoder = Uutf.decoder ~encoding:`UTF_8 (`String(str_utf8)) in
let rec loop (uchacc : Uchar.t Alist.t) =
match Uutf.decode decoder with
| `Await -> assert false
| `End -> Alist.to_list uchacc
| `Malformed(_) -> assert false
| `Uchar(uch) -> loop (Alist.extend uchacc uch)
in
loop Alist.empty


let encode_utf8 (uchs : Uchar.t list) : string =
let buffer = Buffer.create (List.length uchs * 4) in
let encoder = Uutf.encoder `UTF_8 (`Buffer(buffer)) in
uchs |> List.iter (fun uch -> Uutf.encode encoder (`Uchar(uch)) |> ignore);
Uutf.encode encoder `End |> ignore;
Buffer.contents buffer


(** the type for components each of which stand for a single directory. *)
type component = string
[@@deriving show { with_path = false }]
Expand All @@ -34,7 +13,7 @@ type non_normal_component =


let make_non_normal_components (uchs : Uchar.t list) : non_normal_component =
match encode_utf8 uchs with
match UtfUtil.encode_utf8 uchs with
| "" | "." -> Current
| ".." -> Parent
| s -> Component(s)
Expand Down Expand Up @@ -74,7 +53,7 @@ let normalize (ncompos : non_normal_component list) : (component list) option =


let of_string_exn (s : string) : t =
let uchs = decode_utf8 s in
let uchs = UtfUtil.decode_utf8 s in
match uchs with
| [] ->
assert false
Expand Down
11 changes: 3 additions & 8 deletions src-util/myUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ let remains_to_be_implemented msg =
raise (RemainsToBeImplemented(msg))


let string_of_uchar_list (uchs : Uchar.t list) : string =
let buffer = Buffer.create ((List.length uchs) * 4) in
List.iter (fun u -> Uutf.Buffer.add_utf_8 buffer u) uchs;
Buffer.contents buffer
(* TODO: remove this *)
let string_of_uchar_list =
UtfUtil.encode_utf8


let rec range i j =
Expand Down Expand Up @@ -45,10 +44,6 @@ type abs_path = AbsPath.t
[@@deriving show { with_path = false }]


let basename_abs (abspath : abs_path) =
Filename.basename (AbsPath.to_string abspath)


let make_abs_path pathstr =
AbsPath.of_string_exn pathstr

Expand Down
2 changes: 0 additions & 2 deletions src-util/myUtil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ val list_fold_adjacent : ('a -> 'b -> 'b option -> 'b option -> 'a) -> 'a -> 'b

val ( @|> ) : 'a -> ('a -> 'b) -> 'b

val basename_abs : abs_path -> string

val make_abs_path : string -> abs_path

val get_abs_path_string : abs_path -> string
Expand Down
21 changes: 21 additions & 0 deletions src-util/utfUtil.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

(* TODO: consider unifying this module with `InternalText` *)

let decode_utf8 (str_utf8 : string) : Uchar.t list =
let decoder = Uutf.decoder ~encoding:`UTF_8 (`String(str_utf8)) in
let rec loop (uchacc : Uchar.t Alist.t) =
match Uutf.decode decoder with
| `Await -> assert false
| `End -> Alist.to_list uchacc
| `Malformed(_) -> assert false
| `Uchar(uch) -> loop (Alist.extend uchacc uch)
in
loop Alist.empty


let encode_utf8 (uchs : Uchar.t list) : string =
let buffer = Buffer.create (List.length uchs * 4) in
let encoder = Uutf.encoder `UTF_8 (`Buffer(buffer)) in
uchs |> List.iter (fun uch -> Uutf.encode encoder (`Uchar(uch)) |> ignore);
Uutf.encode encoder `End |> ignore;
Buffer.contents buffer

0 comments on commit 8040121

Please sign in to comment.