mirror of
https://github.com/KarolS/millfork.git
synced 2024-09-28 18:55:09 +00:00
Add pstring module
This commit is contained in:
parent
4485c9479e
commit
320b84edb9
@ -16,6 +16,8 @@
|
||||
|
||||
* Added experimental `signed16` and `unsigned16` types. Added `putsigned16` function.
|
||||
|
||||
* Added length-prefixed strings (Pascal strings), `pstring` module and `putpstr` function.
|
||||
|
||||
* Fixed `scrstr2word` in some rare encoding combinations.
|
||||
|
||||
* Added warnings for calling from one segment to another overlapping one.
|
||||
|
@ -44,7 +44,7 @@
|
||||
|
||||
* [`stdlib` module](stdlib/stdlib.md)
|
||||
|
||||
* [`string` and `scrstring` modules](stdlib/string.md)
|
||||
* [`string`, `scrstring` and `pstring` modules](stdlib/string.md)
|
||||
|
||||
* [`stdio` module](stdlib/stdio.md)
|
||||
|
||||
|
@ -44,7 +44,7 @@
|
||||
|
||||
* [`stdlib` module](stdlib/stdlib.md)
|
||||
|
||||
* [`string` and `scrstring` modules](stdlib/string.md)
|
||||
* [`string`, `scrstring` and `pstring` modules](stdlib/string.md)
|
||||
|
||||
* [`stdio` module](stdlib/stdio.md)
|
||||
|
||||
|
@ -51,3 +51,17 @@ It contains functions for handling strings in the screen encoding with the same
|
||||
#### `word scrstrz2word(pointer str)`
|
||||
#### `void scrstrzappend(pointer buffer, pointer str)`
|
||||
#### `void scrstrzappendchar(pointer buffer, byte char)`
|
||||
|
||||
## pstring
|
||||
|
||||
The `scrstring` module automatically imports the [`err` module](./other.md).
|
||||
|
||||
It contains functions for handling length-prefixed strings in any 8-bit encoding.
|
||||
|
||||
#### `byte pstrlen(pointer str)`
|
||||
#### `sbyte pstrcmp(pointer str1, pointer str2)`
|
||||
#### `void pstrcopy(pointer dest, pointer src)`
|
||||
#### `void pstrpaste(pointer dest, pointer src)`
|
||||
#### `word pstr2word(pointer str)`
|
||||
#### `void pstrappend(pointer buffer, pointer str)`
|
||||
#### `void pstrappendchar(pointer buffer, byte char)`
|
||||
|
@ -23,17 +23,26 @@ void print_failure() {
|
||||
}
|
||||
|
||||
void assert_equal(word expected, word actual) {
|
||||
current_test_number += 1
|
||||
if actual != expected {
|
||||
print_failure()
|
||||
putstrz("Expected:"z)
|
||||
putstrz("Expected: "z)
|
||||
putword(expected)
|
||||
putstrz("Actual:"z)
|
||||
putstrz(" Actual: "z)
|
||||
putword(actual)
|
||||
new_line()
|
||||
readkey()
|
||||
}
|
||||
}
|
||||
|
||||
void assert_true(bool result) {
|
||||
current_test_number += 1
|
||||
if not(result) {
|
||||
print_failure()
|
||||
readkey()
|
||||
}
|
||||
}
|
||||
|
||||
void begin_test() {
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,15 @@
|
||||
import test_fibonacci
|
||||
import test_pstring
|
||||
import test_string
|
||||
|
||||
void main() {
|
||||
ensure_mixedcase()
|
||||
|
||||
test_fibonacci()
|
||||
test_string()
|
||||
test_pstring()
|
||||
|
||||
new_line()
|
||||
putstrz("Total failures: "z)
|
||||
putword(failure_count)
|
||||
}
|
23
examples/tests/test_pstring.mfk
Normal file
23
examples/tests/test_pstring.mfk
Normal file
@ -0,0 +1,23 @@
|
||||
import pstring
|
||||
import framework
|
||||
|
||||
void test_pstring() {
|
||||
array buffer[256]
|
||||
|
||||
start_suite("pstring"z)
|
||||
assert_equal(0, pstrcmp("a"p, "a"p))
|
||||
assert_equal(-1, pstrcmp("a"p, "b"p))
|
||||
assert_equal(-1, pstrcmp("a"p, "ab"p))
|
||||
assert_equal(1, pstrcmp("b"p, "a"p))
|
||||
assert_equal(1, pstrcmp("ab"p, "a"p))
|
||||
|
||||
pstrcopy(buffer, "test"p)
|
||||
assert_equal(4, pstrlen(buffer))
|
||||
assert_equal(0, pstrcmp("test"p, buffer))
|
||||
pstrappend(buffer, "hello"p)
|
||||
assert_equal(9, pstrlen(buffer))
|
||||
assert_equal(0, pstrcmp("testhello"p, buffer))
|
||||
assert_equal(1234, pstr2word("1234"p))
|
||||
|
||||
|
||||
}
|
23
examples/tests/test_string.mfk
Normal file
23
examples/tests/test_string.mfk
Normal file
@ -0,0 +1,23 @@
|
||||
import string
|
||||
import framework
|
||||
|
||||
void test_string() {
|
||||
array buffer[256]
|
||||
|
||||
start_suite("string"z)
|
||||
assert_equal(0, strzcmp("a"z, "a"z))
|
||||
assert_equal(-1, strzcmp("a"z, "b"z))
|
||||
assert_equal(-1, strzcmp("a"z, "ab"z))
|
||||
assert_equal(1, strzcmp("b"z, "a"z))
|
||||
assert_equal(1, strzcmp("ab"z, "a"z))
|
||||
|
||||
strzcopy(buffer, "test"z)
|
||||
assert_equal(4, strzlen(buffer))
|
||||
assert_equal(0, strzcmp("test"z, buffer))
|
||||
strzappend(buffer, "hello"z)
|
||||
assert_equal(9, strzlen(buffer))
|
||||
assert_equal(0, strzcmp("testhello"z, buffer))
|
||||
assert_equal(1234, strz2word("1234"z))
|
||||
|
||||
|
||||
}
|
74
include/pstring.mfk
Normal file
74
include/pstring.mfk
Normal file
@ -0,0 +1,74 @@
|
||||
import err
|
||||
|
||||
byte pstrlen(pointer str) {
|
||||
return str[0]
|
||||
}
|
||||
|
||||
sbyte pstrcmp(pointer str1, pointer str2) {
|
||||
byte size
|
||||
byte i
|
||||
size = str1[0]
|
||||
if str2[0] < size {
|
||||
size = str2[0]
|
||||
}
|
||||
for i,1,to,size{
|
||||
if str1[i] != str2[i] {
|
||||
if str1[i] < str2[i] { return -1 }
|
||||
return 1
|
||||
}
|
||||
}
|
||||
if str1[0] == size {
|
||||
if str2[0] == size { return 0 }
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
void pstrcopy(pointer dest, pointer src) {
|
||||
byte i
|
||||
byte size
|
||||
size = src[0]
|
||||
for i,0,parallelto,size{
|
||||
dest[i] = src[i]
|
||||
}
|
||||
}
|
||||
|
||||
void pstrappend(pointer buffer, pointer str) {
|
||||
byte i
|
||||
byte size
|
||||
pointer dest
|
||||
dest = buffer + buffer[0] + 1
|
||||
size = str[0]
|
||||
for i,0,paralleluntil,size{
|
||||
dest[i] = str[i + 1]
|
||||
}
|
||||
buffer[0] += size
|
||||
}
|
||||
|
||||
void pstrappendchar(pointer buffer, byte char) {
|
||||
buffer[0] += 1
|
||||
buffer[buffer[0]] = char
|
||||
}
|
||||
|
||||
word pstr2word(pointer str) {
|
||||
byte size, i, char
|
||||
word next, result
|
||||
result = 0
|
||||
size = str[0]
|
||||
errno = err_ok
|
||||
for i,1,to,size {
|
||||
char = str[i]
|
||||
if '0' <= char <= '0' + 9 {
|
||||
next = result * 10
|
||||
next += char - '0'
|
||||
if next < result {
|
||||
errno = err_range
|
||||
}
|
||||
result = next
|
||||
} else {
|
||||
errno = err_numberformat
|
||||
return result
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
@ -82,3 +82,7 @@ inline void ensure_mixedcase() {
|
||||
inline void ensure_mixedcase() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void putpstr(pointer pstr) {
|
||||
putstr(pstr + 1, pstr[0])
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ nav:
|
||||
- List of keywords: lang/keywords.md
|
||||
- Library reference:
|
||||
- stdlib module: stdlib/stdlib.md
|
||||
- string and scrstring modules: stdlib/string.md
|
||||
- string, scrstring and pstring modules: stdlib/string.md
|
||||
- stdio module: stdlib/stdio.md
|
||||
- err and random modules: stdlib/other.md
|
||||
- keyboard module: stdlib/keyboard.md
|
||||
|
Loading…
Reference in New Issue
Block a user