summaryrefslogtreecommitdiff
path: root/lib/std/strstrip.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/strstrip.myr')
-rw-r--r--lib/std/strstrip.myr43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/std/strstrip.myr b/lib/std/strstrip.myr
new file mode 100644
index 0000000..a3d54fa
--- /dev/null
+++ b/lib/std/strstrip.myr
@@ -0,0 +1,43 @@
+use "types.use"
+use "utf.use"
+use "chartype.use"
+
+pkg std =
+ const strstrip : (str : byte[:] -> byte[:])
+ const strfstrip : (str : byte[:] -> byte[:])
+ const strrstrip : (str : byte[:] -> byte[:])
+;;
+
+/* strip blanks from both head and tail of str */
+const strstrip = {str
+ -> strrstrip(strfstrip(str))
+}
+
+/* strip forward on str */
+const strfstrip = {str
+ var c
+
+ for c = decode(str); isblank(c); c = decode(str)
+ str = str[charlen(c):]
+ ;;
+ -> str
+
+}
+
+/* strip reverse on str */
+const strrstrip = {str
+ var i
+ var end
+
+ /* scan backwards for start of utf8 char */
+ end = str.len
+ for i = str.len; i != 0; i--
+ if str[i - 1] & 0x80 == 0 || str[i-1] & 0xc0 != 0x80
+ if !isspace(decode(str[i-1:]))
+ break
+ ;;
+ end = i - 1
+ ;;
+ ;;
+ -> str[:end]
+}