diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index 18fcf1069..c9480b4bc 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -190,4 +190,46 @@ _found sty P8ZP_SCRATCH_B1 }} } + asmsub lower(uword st @AY) { + ; Lowercases the petscii string in-place. + ; (for efficiency, non-letter characters > 128 will also not be left intact, + ; but regular text doesn't usually contain those characters anyway.) + %asm {{ + sta P8ZP_SCRATCH_W1 + sty P8ZP_SCRATCH_W1+1 + ldy #0 +- lda (P8ZP_SCRATCH_W1),y + beq _done + and #$7f + cmp #97 + bcc + + cmp #123 + bcs + + and #%11011111 ++ sta (P8ZP_SCRATCH_W1),y + iny + bne - +_done rts + }} + } + + asmsub upper(uword st @AY) { + ; Uppercases the petscii string in-place. + %asm {{ + sta P8ZP_SCRATCH_W1 + sty P8ZP_SCRATCH_W1+1 + ldy #0 +- lda (P8ZP_SCRATCH_W1),y + beq _done + cmp #65 + bcc + + cmp #91 + bcs + + ora #%00100000 ++ sta (P8ZP_SCRATCH_W1),y + iny + bne - +_done rts + }} + } } diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index cddb38054..a538f1663 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -173,6 +173,12 @@ Provides string manipulation routines. Often you don't have to call this explicitly and can just write ``string1 = string2`` but this function is useful if you're dealing with addresses for instance. +``lower(string)`` + Lowercases the petscii-string in place. + +``upper(string)`` + Uppercases the petscii-string in place. + floats ------ diff --git a/examples/test.p8 b/examples/test.p8 index 449f6bfa3..6ec24835e 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -12,11 +12,23 @@ main { str s2 = "12345 ABCDEF..UVWXYZ ()!@#$%;:&*()-=[]<>\xff\xfa\xeb\xc0\n" str s3 = "12345 \x61\x62\x63\x64\x65\x66..\x75\x76\x77\x78\x79\x7a ()!@#$%;:&*()-=[]<>\xff\xfa\xeb\xc0\n" - lower(s1) - lower(s2) - lower(s3) - txt.lowercase() + + txt.print(s1) + txt.print(s2) + txt.print(s3) + + string.lower(s1) + string.lower(s2) + string.lower(s3) + txt.print(s1) + txt.print(s2) + txt.print(s3) + + string.upper(s1) + string.upper(s2) + string.upper(s3) + txt.print(s1) txt.print(s2) txt.print(s3) @@ -24,49 +36,5 @@ main { txt.nl() } - asmsub lower(uword st @AY) clobbers(X) { - %asm {{ - sta P8ZP_SCRATCH_W1 - sty P8ZP_SCRATCH_W1+1 - ldy #0 -- lda (P8ZP_SCRATCH_W1),y - beq _done - and #$7f - tax - and #%11100000 - cmp #%01100000 - bne + - txa - and #%11011111 - tax -+ txa - sta (P8ZP_SCRATCH_W1),y - iny - bne - -_done rts - }} - } - asmsub upper(uword st @AY) clobbers(X) { - %asm {{ - sta P8ZP_SCRATCH_W1 - sty P8ZP_SCRATCH_W1+1 - ldy #0 -- lda (P8ZP_SCRATCH_W1),y - beq _done - and #$7f - tax - and #%11100000 - cmp #%01100000 - bne + - txa - and #%11011111 - tax -+ txa - sta (P8ZP_SCRATCH_W1),y - iny - bne - -_done rts - }} - } }