1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-05-29 04:41:30 +00:00

Add scrstring module

This commit is contained in:
Karol Stasiak 2019-10-31 17:29:05 +01:00
parent 939431aaf9
commit 2c52b98beb
7 changed files with 173 additions and 3 deletions

View File

@ -42,7 +42,7 @@
* [`stdlib` module](stdlib/stdlib.md)
* [`string` module](stdlib/string.md)
* [`string` and `scrstring` modules](stdlib/string.md)
* [`stdio` module](stdlib/stdio.md)

View File

@ -44,7 +44,7 @@
* [`stdlib` module](stdlib/stdlib.md)
* [`string` module](stdlib/string.md)
* [`string` and `scrstring` modules](stdlib/string.md)
* [`stdio` module](stdlib/stdio.md)

View File

@ -32,3 +32,16 @@ Sets `errno`.
#### `void strzappendchar(pointer buffer, byte char)`
Modifies the given null-terminated buffer by appending a null-terminated string or a single character respectively.
## scrstring
The `scrstring` module automatically imports the `string` and `err` modules.
It contains functions for handling strings in the screen encoding with the same semantics as the functions from the string module.
#### `byte scrstrzlen(pointer str)`
#### `sbyte scrstrzcmp(pointer str1, pointer str2)`
#### `void scrstrzcopy(pointer dest, pointer src)`
#### `word scrstrz2word(pointer str)`
#### `void scrstrzappend(pointer buffer, pointer str)`
#### `void scrstrzappendchar(pointer buffer, byte char)`

61
include/scrstring.mfk Normal file
View File

@ -0,0 +1,61 @@
import string
#if NULLCHAR_SAME
alias scrstrzlen = strzlen
alias scrstrzcmp = strzcmp
alias scrstrzcopy = strzcopy
alias scrstrzappendchar = strzappendchar
alias scrstrz2word = strz2word
alias scrstrzappend = strzappend
#else
#if ARCH_I80
import scrstring_fastpointers
#else
import scrstring_fastindices
#endif
void scrstrzappend(pointer buffer, pointer str) {
scrstrzcopy(buffer + scrstrzlen(buffer), str)
}
void scrstrzappendchar(pointer buffer, byte char) {
buffer += scrstrzlen(buffer)
buffer[0] = char
buffer[1] = nullchar_scr
}
word scrstrz2word(pointer str) {
byte i
byte char
word next
word result
result = 0
i = 0
errno = err_ok
while true {
char = str[i]
if char == nullchar_scr {
if i == 0 {
errno = err_numberformat
}
return result
}
if '0'scr <= char <= '0'scr + 9 {
next = result * 10
next += char - '0'scr
if next < result {
errno = err_range
}
result = next
} else {
errno = err_numberformat
return result
}
i += 1
}
}
#endif

View File

@ -0,0 +1,41 @@
#if NULLCHAR_SAME
#error The module scrstring_fastindices should not be included directly
#endif
byte scrstrzlen(pointer str) {
byte index
index = 0
while str[index] != nullchar_scr {
index += 1
}
return index
}
sbyte scrstrzcmp(pointer str1, pointer str2) {
byte i1
byte i2
i1 = 0
i2 = 0
while true {
if str1[i1] != str2[i2] {
if str1[i1] < str2[i2] { return -1 }
return 1
}
if str1[i1] == nullchar_scr {
return 0
}
i1 += 1
i2 += 1
}
}
void scrstrzcopy(pointer dest, pointer src) {
byte i
byte c
i = 0
do {
c = src[i]
dest[i] = c
i += 1
} while c != nullchar_scr
}

View File

@ -0,0 +1,55 @@
#if NULLCHAR_SAME
#error The module scrstring_fastpointers should not be included directly
#endif
#pragma zilog_syntax
byte scrstrzlen(pointer str) {
pointer end
end = str
while end[0] != nullchar_scr {
end += 1
}
return lo(end - str)
}
sbyte scrstrzcmp(pointer str1, pointer str2) {
while true {
if str1[0] == nullchar_scr {
if str2[0] == nullchar_scr {
return 0
} else {
return -1
}
} else if str1[0] != str2[0] {
if str1[0] < str2[0] { return -1 }
return 1
}
str1 += 1
str2 += 1
}
}
#if CPUFEATURE_Z80 || CPUFEATURE_8080
void scrstrzcopy(pointer dest, pointer src) {
asm {
? LD HL,(src)
? LD DE,(dest)
___scrstrzcopy_loop:
LD A,(HL)
LD (DE),A
? CP nullchar_scr
? JP NZ, ___scrstrzcopy_loop
}
}
#else
void scrstrzcopy(pointer dest, pointer src) {
byte c
do {
c = src[0]
dest[0] = c
src += 1
dest += 1
} while c != nullchar_scr
}
#endif

View File

@ -34,7 +34,7 @@ nav:
- List of keywords: lang/keywords.md
- Library reference:
- stdlib module: stdlib/stdlib.md
- string module: stdlib/string.md
- string and scrstring modules: stdlib/string.md
- stdio module: stdlib/stdio.md
- keyboard, err, random: stdlib/other.md
- joy, mouse, x_coord: stdlib/input.md