added string.lstripped() and string.ltrimmed()

This commit is contained in:
Irmen de Jong 2024-02-07 23:06:01 +01:00
parent d33aed4ed5
commit 386a391fd9
7 changed files with 129 additions and 150 deletions

View File

@ -0,0 +1,79 @@
string {
; the string functions shared across compiler targets
%option merge, no_symbol_prefixing, ignore_unused
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 = string.length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or string.isprint(cx16.r1L) and not string.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 (destructive)
cx16.r0 = lstripped(s)
if cx16.r0 != s
void string.copy(cx16.r0, s)
}
sub lstripped(str s) -> str {
; -- returns pointer to first non-whitespace and non-visible character at the start of the string (non-destructive lstrip)
if s[0]==0
return s
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until cx16.r1L==0 or string.isprint(cx16.r1L) and not string.isspace(cx16.r1L)
return s+cx16.r0L
}
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 = string.length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or not string.isspace(cx16.r1L)
s[cx16.r0L+1] = 0
}
sub ltrim(str s) {
; -- gets rid of whitespace characters at the start of the string (destructive)
cx16.r0 = ltrimmed(s)
if cx16.r0 != s
void string.copy(cx16.r0, s)
}
sub ltrimmed(str s) -> str {
; -- return pointer to first non-whitespace character at the start of the string (non-destructive ltrim)
if s[0]==0
return s
cx16.r0L = 255
do {
cx16.r0L++
cx16.r1L = s[cx16.r0L]
} until not string.isspace(cx16.r1L)
return s+cx16.r0L
}
}

View File

@ -1,5 +1,7 @@
; 0-terminated string manipulation routines. ; 0-terminated string manipulation routines.
%import shared_string_functions
string { string {
%option no_symbol_prefixing, ignore_unused %option no_symbol_prefixing, ignore_unused
@ -386,68 +388,6 @@ 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 { asmsub isdigit(ubyte petsciichar @A) -> bool @Pc {
%asm {{ %asm {{
cmp #'0' cmp #'0'

View File

@ -1,5 +1,7 @@
; 0-terminated string manipulation routines. For the Virtual Machine target. ; 0-terminated string manipulation routines. For the Virtual Machine target.
%import shared_string_functions
string { string {
%option ignore_unused %option ignore_unused
@ -184,69 +186,6 @@ 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 { sub isdigit(ubyte character) -> bool {
return character>='0' and character<='9' return character>='0' and character<='9'
} }

View File

@ -22,7 +22,7 @@ import kotlin.io.path.readText
class TestCallgraph: FunSpec({ class TestCallgraph: FunSpec({
test("testGraphForEmptySubs") { test("testGraphForEmptySubs") {
val sourcecode = """ val sourcecode = """
%import string %import conv
main { main {
sub start() { sub start() {
} }
@ -37,7 +37,7 @@ class TestCallgraph: FunSpec({
graph.importedBy.size shouldBe 1 graph.importedBy.size shouldBe 1
val toplevelModule = result.compilerAst.toplevelModule val toplevelModule = result.compilerAst.toplevelModule
val importedModule = graph.imports.getValue(toplevelModule).single() val importedModule = graph.imports.getValue(toplevelModule).single()
importedModule.name shouldBe "string" importedModule.name shouldBe "conv"
val importedBy = graph.importedBy.getValue(importedModule).single() val importedBy = graph.importedBy.getValue(importedModule).single()
importedBy.name.startsWith("on_the_fly_test") shouldBe true importedBy.name.startsWith("on_the_fly_test") shouldBe true
@ -61,7 +61,7 @@ class TestCallgraph: FunSpec({
test("reference to empty sub") { test("reference to empty sub") {
val sourcecode = """ val sourcecode = """
%import string %import conv
main { main {
sub start() { sub start() {
uword xx = &empty uword xx = &empty
@ -78,7 +78,7 @@ class TestCallgraph: FunSpec({
graph.importedBy.size shouldBe 1 graph.importedBy.size shouldBe 1
val toplevelModule = result.compilerAst.toplevelModule val toplevelModule = result.compilerAst.toplevelModule
val importedModule = graph.imports.getValue(toplevelModule).single() val importedModule = graph.imports.getValue(toplevelModule).single()
importedModule.name shouldBe "string" importedModule.name shouldBe "conv"
val importedBy = graph.importedBy.getValue(importedModule).single() val importedBy = graph.importedBy.getValue(importedModule).single()
importedBy.name.startsWith("on_the_fly_test") shouldBe true importedBy.name.startsWith("on_the_fly_test") shouldBe true

View File

@ -292,22 +292,28 @@ Provides string manipulation routines.
Returns uppercased PETSCII character. Returns uppercased PETSCII character.
``strip (string)`` ``strip (string)``
Gets rid of whitespace and other non-visible characters at the edges of the string. Gets rid of whitespace and other non-visible characters at the edges of the string. (destructive)
``rstrip (string)`` ``rstrip (string)``
Gets rid of whitespace and other non-visible characters at the end of the string. Gets rid of whitespace and other non-visible characters at the end of the string. (destructive)
``lstrip (string)`` ``lstrip (string)``
Gets rid of whitespace and other non-visible characters at the start of the string. Gets rid of whitespace and other non-visible characters at the start of the string. (destructive)
``lstripped (string) -> str``
Returns pointer to first non-whitespace and non-visible character at the start of the string (non-destructive lstrip)
``trim (string)`` ``trim (string)``
Gets rid of whitespace characters at the edges of the string. Gets rid of whitespace characters at the edges of the string. (destructive)
``rtrim (string)`` ``rtrim (string)``
Gets rid of whitespace characters at the end of the string. Gets rid of whitespace characters at the end of the string. (destructive)
``ltrim (string)`` ``ltrim (string)``
Gets rid of whitespace characters at the start of the string. Gets rid of whitespace characters at the start of the string. (destructive)
``ltrimmed (string) -> str``
Returns pointer to first non-whitespace character at the start of the string (non-destructive ltrim)
``isdigit (char)`` ``isdigit (char)``
Returns boolean if the character is a numerical digit 0-9 Returns boolean if the character is a numerical digit 0-9

View File

@ -54,6 +54,7 @@ Compiler:
Libraries: Libraries:
- conv: the routines could return the address of conv.string_out, and/or there could be versions that take the address of a different buffer and use it instead.
- once kernal rom v47 is released, remove most of the workarounds in cx16 floats.parse_f() . Prototype parse routine in examples/cx16/floatparse.p8 - once kernal rom v47 is released, remove most of the workarounds in cx16 floats.parse_f() . Prototype parse routine in examples/cx16/floatparse.p8
- fix the problems in atari target, and flesh out its libraries. - fix the problems in atari target, and flesh out its libraries.
- c128 target: make syslib more complete (missing kernal routines)? - c128 target: make syslib more complete (missing kernal routines)?

View File

@ -6,24 +6,38 @@
main { main {
sub start() { sub start() {
str namestring = petscii:"The Quick Brown Fox Jumps Over The Lazy Dog\n0123456789!#$%^&*()-=_+[]{};:'<>,./?\n"
str namestring2 = petscii:"The Quick Brown Fox Jumps Over The Lazy Dog\n0123456789!#$%^&*()-=_+[]{};:'<>,./?\n"
txt.petscii2scr_str(namestring2)
for cx16.r0L in 0 to len(namestring) {
txt.print_ubhex(namestring[cx16.r0L], false)
txt.spc()
txt.print_ubhex(namestring2[cx16.r0L], false)
txt.nl()
}
txt.nl()
sys.exit(1)
str name1 = "" str name1 = ""
str name2 = "hello \r\n" str name2 = "hello"
str name3 = " \n\rhello" str name3 = " \n\rhello"
str name4 = " \n\r\xa0\xa0\xff\xffhello\x02\x02\x02 \n " str name4 = " \x02\x02\x02\n\r\xa0\xa0\xff\xffhello"
foo(name2) txt.chrout('[')
txt.print(string.ltrimmed(name1))
txt.print("]\n")
txt.chrout('[')
txt.print(string.ltrimmed(name2))
txt.print("]\n")
txt.chrout('[')
txt.print(string.ltrimmed(name3))
txt.print("]\n")
txt.chrout('[')
txt.print(string.ltrimmed(name4))
txt.print("]\n\n")
txt.chrout('[')
txt.print(string.lstripped(name1))
txt.print("]\n")
txt.chrout('[')
txt.print(string.lstripped(name2))
txt.print("]\n")
txt.chrout('[')
txt.print(string.lstripped(name3))
txt.print("]\n")
txt.chrout('[')
txt.print(string.lstripped(name4))
txt.print("]\n")
; foo(name2)
} }
sub foo (str s2) { sub foo (str s2) {