aboutsummaryrefslogtreecommitdiffstats
path: root/csc/strings.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-07-03 14:37:10 -0700
committerRose Hogenson <rhogenson@posteo.net>2022-07-03 14:37:10 -0700
commitbcb098e8eb18637b6999789585a38ff9168de9d5 (patch)
treefe2bee9fcc5cac8c2f8362c836901f7a3643cb03 /csc/strings.csc
parentWrite closure conversion. (diff)
downloadchromatopelma-bcb098e8eb18637b6999789585a38ff9168de9d5.tar.zst
Add a diff library.
I was hesitant to add a diff library, but it was surprisingly easy. A straightforward application of dynamic programming.
Diffstat (limited to 'csc/strings.csc')
-rw-r--r--csc/strings.csc23
1 files changed, 21 insertions, 2 deletions
diff --git a/csc/strings.csc b/csc/strings.csc
index 737c91b..0259697 100644
--- a/csc/strings.csc
+++ b/csc/strings.csc
@@ -1,14 +1,17 @@
(define-library (csc strings)
(export
+ contains
find
join
not-found-error?
prefix?
+ split
str-quote)
(import (scheme base)
(only (scheme case-lambda) case-lambda)
(only (scheme write) write)
- (only (csc list) intercalate))
+ (only (csc list) intercalate)
+ (only (csc loop) loop))
(begin
@@ -42,5 +45,21 @@
(else (loop (+ 1 i))))))))
+ (define (contains s substr)
+ (if (string=? "" substr)
+ #t
+ (guard (e ((not-found-error? e) #f))
+ (find substr s)
+ #t)))
+
+
(define (join sep . strings)
- (apply string-append (intercalate sep strings)))))
+ (apply string-append (intercalate sep strings)))
+
+
+ (define (split s sep)
+ (loop for s* = s then (string-copy s* (+ i (string-length sep)))
+ for i = (guard (e ((not-found-error? e) (string-length s*)))
+ (find sep s*))
+ collect (substring s* 0 i)
+ while (< i (string-length s*))))))