Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Install erlang
run: sudo apt update && sudo apt install -y erlang
- uses: erlef/setup-beam@v1
with:
otp-version: "28.0.2"
rebar3-version: "3.24.0"
- name: Verify OTP
run: erl -noshell -eval 'io:format("~s~n",[erlang:system_info(otp_release)]), halt().'
- name: Install mond
run: cargo install --git https://github.com/benjaminjellis/mond.git --tag 0.0.9 bahn
run: cargo install --git https://github.com/benjaminjellis/mond.git --tag 0.0.12 bahn
- name: Check formatting
run: bahn format --check
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion bahn.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "std"
version = "0.0.7"
version = "0.0.8"
min_mond_version = "0.0.7"

[dependencies]
68 changes: 68 additions & 0 deletions src/bit_array.mond
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(use result [Result])
(use order [Order])

;;; BitArray is a sequence of binary data of any length.
(pub extern type BitArray)

;;; Converts a UTF-8 String into a BitArray.
(pub extern let from_string ~ (String -> BitArray) mond_bit_array_helpers/identity)

;;; Returns the number of bits in a bit array.
(pub extern let bit_size ~ (BitArray -> Int) erlang/bit_size)

;;; Returns the number of bytes in a bit array.
(pub extern let byte_size ~ (BitArray -> Int) erlang/byte_size)

;;; Pads a bit array with zeros so that it is a whole number of bytes.
(pub extern let pad_to_bytes ~ (BitArray -> BitArray) mond_bit_array_helpers/pad_to_bytes)

;;; Creates a new bit array by joining two bit arrays.
(pub let append {first second}
(concat [first second]))

;;; Extracts a sub-section of a bit array.
;;; Position and length are in bits.
;;; Negative length slices relative to the end of the input.
(pub extern let slice ~ (BitArray -> Int -> Int -> Result BitArray Unit) mond_bit_array_helpers/slice)

;;; Tests whether a bit array is valid UTF-8.
(pub let is_utf8 {bits}
(match (to_string bits)
(Ok _) ~> True
(Error _) ~> False))

;;; Converts a bit array to a UTF-8 string.
;;; Returns Error Unit for invalid UTF-8.
(pub extern let to_string ~ (BitArray -> Result String Unit) mond_bit_array_helpers/bit_array_to_string)

;;; Creates a new bit array by joining multiple bit arrays.
(pub extern let concat ~ ((List BitArray) -> BitArray) mond_bit_array_helpers/concat)

;;; Encodes a BitArray into base64.
;;; If input is not byte-aligned it is padded with zero bits first.
(pub extern let base64_encode ~ (BitArray -> Bool -> String) mond_bit_array_helpers/base64_encode)

;;; Decodes a base64 string into a BitArray.
(pub extern let base64_decode ~ (String -> Result BitArray Unit) mond_bit_array_helpers/base64_decode)

;;; Encodes a BitArray into URL-safe base64.
(pub extern let base64_url_encode ~ (BitArray -> Bool -> String) mond_bit_array_helpers/base64_url_encode)

;;; Decodes a URL-safe base64 string into a BitArray.
(pub extern let base64_url_decode ~ (String -> Result BitArray Unit) mond_bit_array_helpers/base64_url_decode)

;;; Encodes a BitArray into lowercase base16.
;;; If input is not byte-aligned it is padded with zero bits first.
(pub extern let base16_encode ~ (BitArray -> String) mond_bit_array_helpers/base16_encode)

;;; Decodes a base16 string into a BitArray.
(pub extern let base16_decode ~ (String -> Result BitArray Unit) mond_bit_array_helpers/base16_decode)

;;; Converts a BitArray to printable decimal-byte syntax.
(pub extern let inspect ~ (BitArray -> String) mond_bit_array_helpers/inspect)

;;; Compare two bit arrays as sequences of bytes.
(pub extern let compare ~ (BitArray -> BitArray -> Order) mond_bit_array_helpers/compare)

;;; Checks whether the first BitArray starts with the second one.
(pub extern let starts_with ~ (BitArray -> BitArray -> Bool) mond_bit_array_helpers/starts_with)
85 changes: 85 additions & 0 deletions src/filepath.mond
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
(use string)
(use list)
(use result [Result])
(use option [Option])
(pub extern let is_windows ~ (Unit -> Bool) mond_filepath_helpers/is_windows)

(let relative {path}
(match (= (string/slice path 0 1) "/")
True ~> (relative (string/drop_start path 1))
False ~> path))

(pub let join {left right}
(remove_trailing_slash
(match left right
_ "/" ~> left
"" _ ~> (relative right)
"/" candidate if (= (string/slice candidate 0 1) "/") ~> candidate
"/" _ ~> (string/concat left right)
_ _ ~> (string/concat
(string/concat (remove_trailing_slash left) "/")
(relative right)))))

(pub let remove_trailing_slash {path}
(match (string/ends_with path "/")
True ~> (string/drop_end path 1)
False ~> path))

(pub let split_unix {path}
(|> (match (string/split path "/")
[""] ~> []
["" | rest] ~> (list/append ["/"] rest)
rest ~> rest)
(list/filter (f {x} -> (!= x "")))))

;;; Warning there is currently no windows support
(pub let split {path}
(split_unix path))

;;; Get the base name of a path, i.e. the last path segment.
;;; Returns an empty string for the root path "/".
(pub let base_name {path}
(match (= path "/")
True ~> ""
False ~> (let [segments (split path)
last_index (- (list/length segments) 1)]
(match (list/nth last_index segments)
(Some name) ~> name
None ~> ""))))

(pub let extension {path}
(let [file (base_name path)]
(match (string/split file ".")
["" _] ~> (Error ())
[_ ext] ~> (Ok ext)
[_ | rest] ~> (list/last rest)
_ ~> (Error ()))))

(pub let strip_extension {path}
(match (extension path)
(Ok ext) ~> (string/drop_end path (+ (string/length ext) 1))
(Error _) ~> path))

(let get_directory_name {path acc segment}
(match path
["/" | rest] ~> (get_directory_name rest (string/concat acc segment) "/")
[first | rest] ~> (get_directory_name
rest
acc
(string/concat segment first))
[] ~> acc))

(pub let directory_name {path}
(let [path (remove_trailing_slash path)
graphemes (string/to_graphemes path)]
(match graphemes
["/" | rest] ~> (get_directory_name rest "/" "")
_ ~> (get_directory_name graphemes "" ""))))

;; fn permission_to_integer(permission: Permission) -> Int {
;; case permission {
;; Read -> 0o4
;; Write -> 0o2
;; Execute -> 0o1
;; }
;; }
Loading
Loading