boot2

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

102-string-cmp-case.scm (2021B)


      1 ; Variadic string comparisons (= < > <= >=), the -ci variants,
      2 ; case maps, plus string-map and string-for-each.
      3 
      4 ;; --- string=? -----------------------------------------------------
      5 (if (string=? "foo" "foo")          0 (sys-exit 1))
      6 (if (not (string=? "foo" "Foo"))    0 (sys-exit 2))
      7 (if (not (string=? "foo" "bar"))    0 (sys-exit 3))
      8 (if (string=? "ab" "ab" "ab")       0 (sys-exit 4))
      9 (if (not (string=? "ab" "ab" "ba")) 0 (sys-exit 5))
     10 
     11 ;; --- string<? / >? / <=? / >=? -----------------------------------
     12 (if (string<? "abc" "abd")          0 (sys-exit 6))
     13 (if (string<? "abc" "abcd")         0 (sys-exit 7))    ; shorter < its prefix
     14 (if (string<? "a" "b" "c")          0 (sys-exit 8))
     15 (if (not (string<? "a" "b" "b"))    0 (sys-exit 9))
     16 (if (string>? "z" "y" "x")          0 (sys-exit 10))
     17 (if (string<=? "a" "a" "b")         0 (sys-exit 11))
     18 (if (string>=? "z" "z" "a")         0 (sys-exit 12))
     19 
     20 ;; --- string-ci variants (ASCII case fold) ------------------------
     21 (if (string-ci=? "FOO" "foo")        0 (sys-exit 13))
     22 (if (string-ci=? "Foo" "fOO")        0 (sys-exit 14))
     23 (if (not (string-ci=? "foo" "bar"))  0 (sys-exit 15))
     24 (if (string-ci<? "Apple" "BANANA")   0 (sys-exit 16))
     25 (if (string-ci>? "Banana" "apple")   0 (sys-exit 17))
     26 
     27 ;; --- case maps ---------------------------------------------------
     28 (if (string=? "ABC" (string-upcase   "abc"))  0 (sys-exit 18))
     29 (if (string=? "ABC" (string-upcase   "ABC"))  0 (sys-exit 19))
     30 (if (string=? "abc" (string-downcase "ABC"))  0 (sys-exit 20))
     31 (if (string=? "abc" (string-foldcase "ABC"))  0 (sys-exit 21))
     32 ;; non-letters unchanged
     33 (if (string=? "AB1!" (string-upcase "ab1!"))  0 (sys-exit 22))
     34 
     35 ;; --- string-map applies f and returns a fresh string -------------
     36 (if (string=? "HELLO" (string-map char-upcase "Hello")) 0 (sys-exit 23))
     37 
     38 ;; --- string-for-each accumulates side effects --------------------
     39 (define total 0)
     40 (string-for-each (lambda (c) (set! total (+ total c))) "abc")
     41 ;; 97 + 98 + 99 = 294
     42 (if (= 294 total) 0 (sys-exit 24))
     43 
     44 (sys-exit 25)