ref: eb34795e1fc39c4681ad98f0daa57ffe9040c59e
parent: 199e8c38edcfd24d13a5209dced69cf0c9b496d0
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 14 10:39:45 EST 2015
Add dirname.myr
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -22,6 +22,7 @@
dial.myr \
die.myr \
dir.myr \
+ dirname.myr \
endian.myr \
env.myr \
errno.myr \
@@ -112,7 +113,7 @@
OBJ=$(STDSRC:.myr=.o) $(SYSSRC:.myr=.o) $(STDASMSRC:.s=.o) $(SYSASMSRC:.s=.o)
USE=$(STDSRC:.myr=.use) $(SYSSRC:.myr=.use) $(STDLIB)
-.PHONY: clean
+.PHONY: clean check test
clean:
rm -f $(OBJ)
rm -f $(USE)
--- a/libstd/bldfile
+++ b/libstd/bldfile
@@ -28,6 +28,7 @@
cstrconv.myr
dial.myr
die.myr
+ dirname.myr
endian.myr
env.myr
errno.myr
--- /dev/null
+++ b/libstd/dirname.myr
@@ -1,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)
+ ;;
+}