summaryrefslogtreecommitdiff
path: root/lib/string.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string.fnl')
-rw-r--r--lib/string.fnl19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/string.fnl b/lib/string.fnl
index 1696a64..6538827 100644
--- a/lib/string.fnl
+++ b/lib/string.fnl
@@ -12,11 +12,22 @@
(table.insert result (must (luna.utf8.sub str i 1))))
result)
-(fn split [str]
- (accumulate [res [] v _ (str:gmatch "%S+")]
+(fn split [str delimiter]
+ (if (empty? str)
+ []
(do
- (table.insert res v)
- res)))
+ (local result {})
+ (local len (# str))
+ (var cursor 1)
+ (var (start end) (str:find delimiter))
+ (while start
+ (when (< cursor start)
+ (table.insert result (str:sub cursor (- start 1))))
+ (set cursor (+ end 1))
+ (set (start end) (str:find delimiter cursor)))
+ (when (<= cursor len)
+ (table.insert result (str:sub cursor len)))
+ result)))
(fn ends-with [str end]
(= (string.sub str (- (# end))) end))