mirror of
https://github.com/irmen/prog8.git
synced 2024-11-22 15:33:02 +00:00
added string.strip() and string.trim() and l/r variants.
fixed memsizer for pointers-to-ubyte.
This commit is contained in:
parent
26ed231f61
commit
24944ad49e
@ -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
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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'
|
||||
|
@ -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'
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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.
|
||||
|
||||
...
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user