Added more string functions

This commit is contained in:
Justin Ethier 2015-07-28 21:34:55 -04:00
parent 05f6d53fd4
commit 4c0ce77892
2 changed files with 42 additions and 2 deletions

5
TODO
View file

@ -7,6 +7,11 @@ Working TODO list. should start creating issues for these to get them out of her
- self-hosting, there are a lot of accumulated TODO's that need to be addressed
- adding r7rs support
- vectors - add test cases from r7rs
- strings - finish adding functions, then add test cases
- review other sections from the report
- improved error handling:
- param count checks
if a primitive is called directly, shouldn't it be possible to check arg count?

View file

@ -40,8 +40,6 @@
boolean=?
symbol=?
Cyc-obj=?
make-string
string
vector
vector-append
vector-copy
@ -49,8 +47,17 @@
vector-fill!
vector->list
vector->string
make-string
string
string-copy
string-copy!
string-fill!
my-string->list
string->vector
; TODO:
;string-upcase
;string-downcase
;string-foldcase
make-parameter
current-output-port
current-input-port
@ -217,6 +224,7 @@
(define (vector->string vec . opts)
(let ((lst (apply vector->list (cons vec opts))))
(list->string lst)))
;; TODO: change to string->list
(define (my-string->list str . opts)
(letrec ((len (string-length str))
(start (if (> (length opts) 0) (car opts) 0))
@ -232,6 +240,33 @@
(define (string->vector str . opts)
(list->vector
(string->list str)))
(define (string-copy str . opts)
(letrec ((len (string-length str))
(start (if (> (length opts) 0) (car opts) 0))
(end (if (> (length opts) 1) (cadr opts) len)))
(substring str start end)))
(define (string-copy! to at from . opts)
(letrec ((len (string-length from))
(start (if (> (length opts) 0) (car opts) 0))
(end (if (> (length opts) 1) (cadr opts) len))
(loop (lambda (i-at i-from)
(cond
((= i-from end) to)
(else
(string-set! to i-at (string-ref from i-from))
(loop (+ i-at 1) (+ i-from 1)))))))
(loop at start)))
(define (string-fill! str fill . opts)
(letrec ((len (string-length str))
(start (if (> (length opts) 0) (car opts) 0))
(end (if (> (length opts) 1) (cadr opts) len))
(loop (lambda (i)
(cond
((= i end) str)
(else
(string-set! str i fill)
(loop (+ i 1)))))))
(loop start)))
(define (vector-append . vecs)
(list->vector
(apply append (map vector->list vecs))))