Skip to content

Commit

Permalink
added string.lstripped() and string.ltrimmed()
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Feb 7, 2024
1 parent d33aed4 commit 386a391
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 150 deletions.
79 changes: 79 additions & 0 deletions compiler/res/prog8lib/shared_string_functions.p8
Original file line number Diff line number Diff line change
@@ -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
}

}
64 changes: 2 additions & 62 deletions compiler/res/prog8lib/string.p8
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 0-terminated string manipulation routines.

%import shared_string_functions

string {
%option no_symbol_prefixing, ignore_unused

Expand Down Expand Up @@ -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 {
%asm {{
cmp #'0'
Expand Down
65 changes: 2 additions & 63 deletions compiler/res/prog8lib/virtual/string.p8
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
; 0-terminated string manipulation routines. For the Virtual Machine target.

%import shared_string_functions

string {
%option ignore_unused

Expand Down Expand Up @@ -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 {
return character>='0' and character<='9'
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/TestCallgraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlin.io.path.readText
class TestCallgraph: FunSpec({
test("testGraphForEmptySubs") {
val sourcecode = """
%import string
%import conv
main {
sub start() {
}
Expand All @@ -37,7 +37,7 @@ class TestCallgraph: FunSpec({
graph.importedBy.size shouldBe 1
val toplevelModule = result.compilerAst.toplevelModule
val importedModule = graph.imports.getValue(toplevelModule).single()
importedModule.name shouldBe "string"
importedModule.name shouldBe "conv"
val importedBy = graph.importedBy.getValue(importedModule).single()
importedBy.name.startsWith("on_the_fly_test") shouldBe true

Expand All @@ -61,7 +61,7 @@ class TestCallgraph: FunSpec({

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

Expand Down
18 changes: 12 additions & 6 deletions docs/source/libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,22 +292,28 @@ Provides string manipulation routines.
Returns uppercased PETSCII character.

``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)``
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)``
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)``
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)``
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)``
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)``
Returns boolean if the character is a numerical digit 0-9
Expand Down
1 change: 1 addition & 0 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Compiler:

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
- fix the problems in atari target, and flesh out its libraries.
- c128 target: make syslib more complete (missing kernal routines)?
Expand Down
44 changes: 29 additions & 15 deletions examples/test.p8
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@

main {
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 name2 = "hello \r\n"
str name2 = "hello"
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"

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)
; foo(name2)
}

sub foo (str s2) {
Expand Down

0 comments on commit 386a391

Please sign in to comment.