boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

101-string-procs.scm (3106B)


      1 ; make-string, string, string-ref, string-set!, substring, string-append,
      2 ; string-copy, string-copy!, string-fill!, string->list, list->string.
      3 
      4 ;; --- make-string ---------------------------------------------------
      5 (define ms (make-string 4 #\x))
      6 (if (= 4 (string-length ms))      0 (sys-exit 1))
      7 (if (eq? #\x (string-ref ms 0))   0 (sys-exit 2))
      8 (if (eq? #\x (string-ref ms 3))   0 (sys-exit 3))
      9 ;; default fill is #\space (32)
     10 (define ms2 (make-string 3))
     11 (if (eq? #\space (string-ref ms2 0)) 0 (sys-exit 4))
     12 
     13 ;; --- string (variadic) --------------------------------------------
     14 (define s1 (string #\a #\b #\c))
     15 (if (= 3 (string-length s1))         0 (sys-exit 5))
     16 (if (eq? #\b (string-ref s1 1))      0 (sys-exit 6))
     17 (if (= 0 (string-length (string)))   0 (sys-exit 7))   ; empty string
     18 
     19 ;; --- string-ref / string-set! -------------------------------------
     20 (define m (make-string 5 #\.))
     21 (string-set! m 2 #\X)
     22 (if (eq? #\X (string-ref m 2)) 0 (sys-exit 8))
     23 (if (eq? #\. (string-ref m 0)) 0 (sys-exit 9))
     24 (if (eq? #\. (string-ref m 4)) 0 (sys-exit 10))
     25 
     26 ;; --- substring -----------------------------------------------------
     27 (define sub (substring "abcdef" 1 4))
     28 (if (= 3 (string-length sub))    0 (sys-exit 11))
     29 (if (eq? #\b (string-ref sub 0)) 0 (sys-exit 12))
     30 (if (eq? #\d (string-ref sub 2)) 0 (sys-exit 13))
     31 ;; full-range substring
     32 (if (string=? "abcdef" (substring "abcdef" 0 6)) 0 (sys-exit 14))
     33 
     34 ;; --- string-append ------------------------------------------------
     35 (define a (string-append "foo" "bar" "baz"))
     36 (if (= 9 (string-length a))     0 (sys-exit 15))
     37 (if (eq? #\f (string-ref a 0))  0 (sys-exit 16))
     38 (if (eq? #\z (string-ref a 8))  0 (sys-exit 17))
     39 (if (= 0 (string-length (string-append))) 0 (sys-exit 18))
     40 
     41 ;; --- string-copy --------------------------------------------------
     42 (define c (string-copy "hello"))
     43 (if (= 5 (string-length c))     0 (sys-exit 19))
     44 (if (eq? #\h (string-ref c 0))  0 (sys-exit 20))
     45 ;; with start / end
     46 (define c2 (string-copy "hello" 1 4))
     47 (if (= 3 (string-length c2))    0 (sys-exit 21))
     48 (if (eq? #\e (string-ref c2 0)) 0 (sys-exit 22))
     49 (if (eq? #\l (string-ref c2 2)) 0 (sys-exit 23))
     50 
     51 ;; --- string-copy! -------------------------------------------------
     52 (define dst (make-string 6 #\.))
     53 (string-copy! dst 1 "abc")
     54 (if (eq? #\. (string-ref dst 0)) 0 (sys-exit 24))
     55 (if (eq? #\a (string-ref dst 1)) 0 (sys-exit 25))
     56 (if (eq? #\c (string-ref dst 3)) 0 (sys-exit 26))
     57 (if (eq? #\. (string-ref dst 5)) 0 (sys-exit 27))
     58 
     59 ;; --- string-fill! -------------------------------------------------
     60 (define f (make-string 4 #\.))
     61 (string-fill! f #\Q)
     62 (if (eq? #\Q (string-ref f 0))  0 (sys-exit 28))
     63 (if (eq? #\Q (string-ref f 3))  0 (sys-exit 29))
     64 
     65 ;; --- string->list / list->string ----------------------------------
     66 (if (equal? (list #\a #\b #\c) (string->list "abc")) 0 (sys-exit 30))
     67 (if (= 3 (length (string->list "abc")))              0 (sys-exit 31))
     68 
     69 (define rt (list->string (list #\h #\i)))
     70 (if (= 2 (string-length rt))    0 (sys-exit 32))
     71 (if (eq? #\h (string-ref rt 0)) 0 (sys-exit 33))
     72 (if (eq? #\i (string-ref rt 1)) 0 (sys-exit 34))
     73 
     74 (sys-exit 35)