ocaml: phase 6 Filename module + Char.compare/equal/escaped (+7 tests, 569 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Filename module (forward-slash only, no Windows-separator detection): basename '/foo/bar/baz.ml' = 'baz.ml' dirname '/foo/bar/baz.ml' = '/foo/bar' extension 'baz.tar.gz' = '.gz' chop_extension 'hello.ml' = 'hello' concat 'a' 'b' = 'a/b' is_relative 'a/b' = true current_dir_name = '.', parent_dir_name = '..', dir_sep = '/' Char additions: equal a b = (a = b) compare a b = code(a) - code(b) escaped '\n' = '\\n' (likewise t, r, \\, ")
This commit is contained in:
@@ -474,6 +474,69 @@
|
||||
let is_alnum c = is_alpha c || is_digit c
|
||||
let is_whitespace c =
|
||||
c = \" \" || c = \"\\t\" || c = \"\\n\" || c = \"\\r\"
|
||||
let equal a b = a = b
|
||||
let compare a b = _char_code a - _char_code b
|
||||
let escaped c =
|
||||
if c = \"\\n\" then \"\\\\n\"
|
||||
else if c = \"\\t\" then \"\\\\t\"
|
||||
else if c = \"\\r\" then \"\\\\r\"
|
||||
else if c = \"\\\\\" then \"\\\\\\\\\"
|
||||
else if c = \"\\\"\" then \"\\\\\\\"\"
|
||||
else c
|
||||
end ;;
|
||||
|
||||
module Filename = struct
|
||||
(* Minimal Filename: basename / dirname / extension / concat /
|
||||
chop_suffix. Forward-slash only — doesn't try to detect
|
||||
Windows-style separators. *)
|
||||
let _last_slash s =
|
||||
let n = _string_length s in
|
||||
let rec aux i =
|
||||
if i < 0 then -1
|
||||
else if _string_get s i = \"/\" then i
|
||||
else aux (i - 1)
|
||||
in
|
||||
aux (n - 1)
|
||||
|
||||
let basename s =
|
||||
let i = _last_slash s in
|
||||
if i < 0 then s
|
||||
else _string_sub s (i + 1) (_string_length s - i - 1)
|
||||
|
||||
let dirname s =
|
||||
let i = _last_slash s in
|
||||
if i < 0 then \".\"
|
||||
else if i = 0 then \"/\"
|
||||
else _string_sub s 0 i
|
||||
|
||||
let extension s =
|
||||
let b = basename s in
|
||||
let n = _string_length b in
|
||||
let rec aux i =
|
||||
if i < 0 then \"\"
|
||||
else if _string_get b i = \".\" then
|
||||
_string_sub b i (n - i)
|
||||
else aux (i - 1)
|
||||
in
|
||||
aux (n - 1)
|
||||
|
||||
let chop_extension s =
|
||||
let ext = extension s in
|
||||
let nx = _string_length ext in
|
||||
if nx = 0 then s
|
||||
else _string_sub s 0 (_string_length s - nx)
|
||||
|
||||
let concat a b =
|
||||
if _string_length a = 0 then b
|
||||
else if _string_get a (_string_length a - 1) = \"/\" then a ^ b
|
||||
else a ^ \"/\" ^ b
|
||||
|
||||
let is_relative s =
|
||||
_string_length s = 0 || _string_get s 0 <> \"/\"
|
||||
|
||||
let current_dir_name = \".\"
|
||||
let parent_dir_name = \"..\"
|
||||
let dir_sep = \"/\"
|
||||
end ;;
|
||||
|
||||
module Int = struct
|
||||
|
||||
Reference in New Issue
Block a user