;;; Case conversion
import bits ; band, bor, bxor import string ; isalpha, strcat, strunpack
; The case conversion functions all behave very similarly, differing only in ; how they modify the ordinal value V of a character to be kept. ; upcase is V & 95, downcase is V | 32, and swapcase is V ^ 32. $casefun(case, op) { :strunpack push 0 ; get characters on stack and initialize accumulator _`case`_loop: swap dup jz _`case`_done dup :isalpha jz _`case`_cat `op` _`case`_cat: :strcat jump _`case`_loop _`case`_done: pop ret }
; returns string S with all alphabetical characters capitalized ; [S] => [S'] ; ; [“”] => [“”] ; [“abc”] => [“ABC”] ; [“123”] => [“123”] ; [“Abc123Def”] => [“ABC123DEF”] upcase: $casefun(upcase, push 0x5F :band)
; returns string S with all alphabetical characters lower-cased ; [S] => [S'] ; ; [“”] => [“”] ; [“ABC”] => [“abc”] ; [“123”] => [“123”] ; [“aBC123dEF”] => [“abc123def”] downcase: $casefun(downcase, push 0x20 :bor)
; returns string S with the case of all alphabetical characters swapped ; [S] => [S'] ; ; [“”] => [“”] ; [“FooBar”] => [“fOObAR”] ; [“Abc123deF”] => [“aBC123DEf”] swapcase: $casefun(swapcase, push 0x20 :bxor)