added string.strip() and string.trim() and l/r variants.

fixed memsizer for pointers-to-ubyte.
This commit is contained in:
Irmen de Jong 2024-02-07 02:09:08 +01:00
parent 26ed231f61
commit 24944ad49e
8 changed files with 203 additions and 29 deletions

View File

@ -23,5 +23,9 @@ class AtariTarget: ICompilationTarget, IStringEncoding by Encoder, IMemSizer {
}
override fun memorySize(arrayDt: DataType, numElements: Int) =
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
if(arrayDt==DataType.UWORD)
numElements // pointer to bytes.
else
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
}

View File

@ -22,5 +22,8 @@ class VMTarget: ICompilationTarget, IStringEncoding by Encoder, IMemSizer {
}
override fun memorySize(arrayDt: DataType, numElements: Int) =
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
if(arrayDt==DataType.UWORD)
numElements // pointer to bytes.
else
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
}

View File

@ -14,5 +14,8 @@ internal object CbmMemorySizer: IMemSizer {
}
override fun memorySize(arrayDt: DataType, numElements: Int) =
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
if(arrayDt==DataType.UWORD)
numElements // pointer to bytes.
else
memorySize(ArrayToElementTypes.getValue(arrayDt)) * numElements
}

View File

@ -386,6 +386,68 @@ fail clc ; yes, no match found, return with c=0
}}
}
sub strip(str s) {
; -- gets rid of whitespace and other non-visible characters at the edges of the string
rstrip(s)
lstrip(s)
}
sub rstrip(str s) {
; -- gets rid of whitespace and other non-visible characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or isprint(cx16.r1L) and not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
}
sub lstrip(str s) {
; -- gets rid of whitespace and other non-visible characters at the start of the string
if s[0]==0
return
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until cx16.r1L==0 or isprint(cx16.r1L) and not isspace(cx16.r1L)
if cx16.r0L>0
copy(s+cx16.r0L, s)
}
sub trim(str s) {
; -- gets rid of whitespace characters at the edges of the string
rtrim(s)
ltrim(s)
}
sub rtrim(str s) {
; -- gets rid of whitespace characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
}
sub ltrim(str s) {
; -- gets rid of whitespace characters at the start of the string
if s[0]==0
return
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until not isspace(cx16.r1L)
if cx16.r0L>0
copy(s+cx16.r0L, s)
}
asmsub isdigit(ubyte petsciichar @A) -> bool @Pc {
%asm {{
cmp #'0'

View File

@ -184,6 +184,69 @@ string {
}
}
sub strip(str s) {
; -- gets rid of whitespace and other non-visible characters at the edges of the string
rstrip(s)
lstrip(s)
}
sub rstrip(str s) {
; -- gets rid of whitespace and other non-visible characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or isprint(cx16.r1L) and not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
}
sub lstrip(str s) {
; -- gets rid of whitespace and other non-visible characters at the start of the string
if s[0]==0
return
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until cx16.r1L==0 or isprint(cx16.r1L) and not isspace(cx16.r1L)
if cx16.r0L>0
copy(s+cx16.r0L, s)
}
sub trim(str s) {
; -- gets rid of whitespace characters at the edges of the string
rtrim(s)
ltrim(s)
}
sub rtrim(str s) {
; -- gets rid of whitespace characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
}
sub ltrim(str s) {
; -- gets rid of whitespace characters at the start of the string
if s[0]==0
return
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until not isspace(cx16.r1L)
if cx16.r0L>0
copy(s+cx16.r0L, s)
}
sub isdigit(ubyte character) -> bool {
return character>='0' and character<='9'
}

View File

@ -290,6 +290,24 @@ Provides string manipulation routines.
``upperchar (char)``
Returns uppercased PETSCII character.
``strip (string)``
Gets rid of whitespace and other non-visible characters at the edges of the string.
``rstrip (string)``
Gets rid of whitespace and other non-visible characters at the end of the string.
``lstrip (string)``
Gets rid of whitespace and other non-visible characters at the start of the string.
``trim (string)``
Gets rid of whitespace characters at the edges of the string.
``rtrim (string)``
Gets rid of whitespace characters at the end of the string.
``ltrim (string)``
Gets rid of whitespace characters at the start of the string.
``isdigit (char)``
Returns boolean if the character is a numerical digit 0-9

View File

@ -1,6 +1,9 @@
TODO
====
&pointervar[x] isn't correct?? (at least in IR)
(after merge in boolean): move all "OperatorXinplace" from expressionGen to AssignmentGen, see if we can get rid of the Result return type.
...

View File

@ -1,37 +1,55 @@
%import textio
%import string
%zeropage basicsafe
%option no_sysinit
main {
sub start() {
ubyte @shared xx=1
uword @shared yy
str name1 = ""
str name2 = "hello \r\n"
str name3 = " \n\rhello"
str name4 = " \n\r\xa0\xa0\xff\xffhello\x02\x02\x02 \n "
ubyte[16] array
array[1] = 1
txt.print("strip:\n")
string.strip(name1)
txt.chrout('[')
txt.print(name1)
txt.print("]\n")
string.strip(name2)
txt.chrout('[')
txt.print(name2)
txt.print("]\n")
string.strip(name3)
txt.chrout('[')
txt.print(name3)
txt.print("]\n")
string.strip(name4)
txt.chrout('[')
txt.print(name4)
txt.print("]\n")
xx += 3
yy += 3
xx -= 3
yy -= 3
txt.print_ub(array[1])
txt.spc()
array[1]++
txt.print_ub(array[1])
txt.spc()
array[1]--
txt.print_ub(array[1])
txt.nl()
txt.print_ub(array[1])
txt.spc()
array[xx]++
txt.print_ub(array[1])
txt.spc()
array[xx]--
txt.print_ub(array[1])
txt.nl()
str tname1 = ""
str tname2 = "hello \r\n"
str tname3 = " \n\r\x09hello"
str tname4 = " \n\x09\x0b\r\xa0\xa0\xff\xffhello\x05\x05\x05 \n "
txt.print("trim:\n")
string.trim(tname1)
txt.chrout('[')
txt.print(tname1)
txt.print("]\n")
string.trim(tname2)
txt.chrout('[')
txt.print(tname2)
txt.print("]\n")
string.trim(tname3)
txt.chrout('[')
txt.print(tname3)
txt.print("]\n")
string.trim(tname4)
txt.chrout('[')
txt.print(tname4)
txt.print("]\n")
}
}