aboutsummaryrefslogtreecommitdiffstats
path: root/strings.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-09 08:40:09 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-09 08:40:09 -0800
commit3ff7aac2d2eb2cbf2f854793fc0d7bc6f1f7d927 (patch)
treecef8ca0e77c40a70daaca40af25572437d563105 /strings.csc
downloadchromatopelma-3ff7aac2d2eb2cbf2f854793fc0d7bc6f1f7d927.tar.zst
Initial commit.
Not sure if everything here will be needed eventually, but we have a working bytecode interpreter. Next I will write the linker, then the core compiler, and finish with the macro expander.
Diffstat (limited to 'strings.csc')
-rw-r--r--strings.csc42
1 files changed, 42 insertions, 0 deletions
diff --git a/strings.csc b/strings.csc
new file mode 100644
index 0000000..963565c
--- /dev/null
+++ b/strings.csc
@@ -0,0 +1,42 @@
+(define-library (csc strings)
+ (export
+ str-find
+ str-not-found-error?
+ str-prefix?
+ str-quote)
+ (import (scheme base)
+ (scheme case-lambda)
+ (only (scheme write) write))
+ (begin
+
+
+ (define str-prefix?
+ (let ((str-prefix?' (lambda (prefix str start)
+ (and (<= (string-length prefix) (- (string-length str) start))
+ (string=? prefix (substring str start (+ start (string-length prefix))))))))
+ (case-lambda
+ ((prefix str) (str-prefix?' prefix str 0))
+ ((prefix str start) (str-prefix?' prefix str start)))))
+
+
+ (define (str-quote s)
+ (let ((out (open-output-string)))
+ (write s out)
+ (get-output-string out)))
+
+
+ (define-record-type <str-not-found-error>
+ (make-str-not-found-error)
+ str-not-found-error?)
+
+
+ (define str-find
+ (let ((str-find' (lambda (match str start end)
+ (let loop ((i start))
+ (cond ((>= i end) (raise (make-str-not-found-error)))
+ ((str-prefix? match str i) i)
+ (else (loop (+ 1 i))))))))
+ (case-lambda
+ ((match str) (str-find' match str 0 (string-length str)))
+ ((match str start) (str-find' match str start (string-length str)))
+ ((match str start end) (str-find' match str start end)))))))