diff options
Diffstat (limited to 'csc/strings.csc')
| -rw-r--r-- | csc/strings.csc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/csc/strings.csc b/csc/strings.csc new file mode 100644 index 0000000..737c91b --- /dev/null +++ b/csc/strings.csc @@ -0,0 +1,46 @@ +(define-library (csc strings) + (export + find + join + not-found-error? + prefix? + str-quote) + (import (scheme base) + (only (scheme case-lambda) case-lambda) + (only (scheme write) write) + (only (csc list) intercalate)) + (begin + + + (define prefix? + (case-lambda + ((prefix str) (prefix? prefix str 0)) + ((prefix str start) + (and (<= (string-length prefix) (- (string-length str) start)) + (string=? prefix (substring str start (+ start (string-length prefix)))))))) + + + (define (str-quote s) + (let ((out (open-output-string))) + (write s out) + (get-output-string out))) + + + (define-record-type <not-found-error> + (make-not-found-error) + not-found-error?) + + + (define find + (case-lambda + ((match str) (find match str 0 (string-length str))) + ((match str start) (find match str start (string-length str))) + ((match str start end) + (let loop ((i start)) + (cond ((>= i end) (raise (make-not-found-error))) + ((prefix? match str i) i) + (else (loop (+ 1 i)))))))) + + + (define (join sep . strings) + (apply string-append (intercalate sep strings))))) |
