From 796add0ee2999f7434bf10f0275ed89ce254dd49 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 13 Dec 2023 00:28:34 +0100 Subject: [PATCH] add string.isspace and string.isprint --- compiler/res/prog8lib/string.p8 | 46 ++++++++++++++++++++++--- compiler/res/prog8lib/virtual/string.p8 | 10 +++++- docs/source/libraries.rst | 8 ++++- docs/source/todo.rst | 5 +++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index ad9d0e2d4..6bc4cdded 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -384,7 +384,7 @@ fail clc ; yes, no match found, return with c=0 }} } - asmsub isdigit(ubyte character @A) -> bool @Pc { + asmsub isdigit(ubyte petsciichar @A) -> bool @Pc { %asm {{ cmp #'0' bcs + @@ -398,7 +398,7 @@ fail clc ; yes, no match found, return with c=0 }} } - asmsub isupper(ubyte character @A) -> bool @Pc { + asmsub isupper(ubyte petsciichar @A) -> bool @Pc { ; shifted petscii has 2 ranges that contain the upper case letters... %asm {{ cmp #97 @@ -418,7 +418,7 @@ _yes sec }} } - asmsub islower(ubyte character @A) -> bool @Pc { + asmsub islower(ubyte petsciichar @A) -> bool @Pc { %asm {{ cmp #'a' bcs + @@ -432,7 +432,7 @@ _yes sec }} } - asmsub isletter(ubyte character @A) -> bool @Pc { + asmsub isletter(ubyte petsciichar @A) -> bool @Pc { %asm {{ jsr islower bcs + @@ -440,4 +440,42 @@ _yes sec + rts }} } + + asmsub isspace(ubyte petsciichar @A) -> bool @Pc { + %asm {{ + cmp #32 + beq + + cmp #13 + beq + + cmp #9 + beq + + cmp #10 + beq + + cmp #141 + beq + + cmp #160 + beq + + clc + rts ++ sec + rts + }} + } + + asmsub isprint(ubyte petsciichar @A) -> bool @Pc { + %asm {{ + cmp #160 + bcc + + rts ++ cmp #32 + bcs + + rts ++ cmp #128 + bcc + + clc + rts ++ sec + rts + }} + } } diff --git a/compiler/res/prog8lib/virtual/string.p8 b/compiler/res/prog8lib/virtual/string.p8 index d01155434..487df01ce 100644 --- a/compiler/res/prog8lib/virtual/string.p8 +++ b/compiler/res/prog8lib/virtual/string.p8 @@ -190,4 +190,12 @@ string { sub isletter(ubyte character) -> bool { return islower(character) or isupper(character) } - } + + sub isspace(ubyte character) -> bool { + return character in [32, 13, 9, 10, 141, 160] + } + + sub isprint(ubyte character) -> bool { + return character>=32 and character<=127 or character>=160 + } +} diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index f67310fce..187db6543 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -275,7 +275,13 @@ Provides string manipulation routines. Returns boolean if the character is a numerical digit 0-9 ``islower (char)``, ``isupper (char)``, ``isletter (char)`` - Returns boolean if the character is a shifted-PETSCII lowercase letter, uppercase letter, or any letter. + Returns true if the character is a shifted-PETSCII lowercase letter, uppercase letter, or any letter, respectively. + +``isspace (char)`` + Returns true if the PETSCII character is a whitespace (tab, space, return, and shifted versions) + +``isprint (char)`` + Returns true if the PETSCII character is a "printable" character (space or any visible symbol) ``startswith (string, prefix) -> bool`` Returns true if string starts with prefix, otherwise false diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 1e3191a92..b6a57bf39 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,6 +2,11 @@ TODO ==== +- optimize if-else expressions whose condition returns the boolean status in a status register to use a branch opcode instead of a comparison against 0 +- fix "can't use Z or N flags as return 'values'" in 6502 codegen? + +- merge branch optimize-st for some optimizations regardign SymbolTable use + - [on branch: call-pointers] allow calling a subroutine via a pointer variable (indirect JSR, optimized form of callfar()) modify programs (shell, paint) that now use callfar