summaryrefslogtreecommitdiff
path: root/libstd/dirname.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-02-14 15:39:45 -0800
committerOri Bernstein <ori@eigenstate.org>2015-02-14 15:39:45 -0800
commiteb34795e1fc39c4681ad98f0daa57ffe9040c59e (patch)
tree73b48ac021dad6dffd7945e062a142da8972cc62 /libstd/dirname.myr
parent199e8c38edcfd24d13a5209dced69cf0c9b496d0 (diff)
downloadmc-eb34795e1fc39c4681ad98f0daa57ffe9040c59e.tar.gz
Add dirname.myr
Diffstat (limited to 'libstd/dirname.myr')
-rw-r--r--libstd/dirname.myr37
1 files changed, 37 insertions, 0 deletions
diff --git a/libstd/dirname.myr b/libstd/dirname.myr
new file mode 100644
index 0000000..5e80771
--- /dev/null
+++ b/libstd/dirname.myr
@@ -0,0 +1,37 @@
+use "alloc.use"
+use "slcp.use"
+use "die.use"
+use "sldup.use"
+use "strfind.use"
+use "option.use"
+
+pkg std =
+ const dirname : (p : byte[:] -> byte[:])
+ const basename : (p : byte[:] -> byte[:])
+;;
+
+const dirname = {p
+ match std.strrfind(p, "/")
+ | `std.Some idx:
+ -> std.sldup(p[:idx])
+ | `std.None:
+ -> std.sldup(".")
+ ;;
+}
+
+const basename = {p
+:again
+ if p.len == 0
+ -> std.sldup(".")
+ ;;
+
+ match std.strrfind(p, "/")
+ | `std.Some idx:
+ if idx == p.len - 1
+ goto again
+ ;;
+ -> std.sldup(p[idx+1:])
+ | `std.None:
+ -> std.sldup(p)
+ ;;
+}