fix string encoding for escaped characters

This commit is contained in:
Irmen de Jong 2022-03-25 00:17:41 +01:00
parent 27f6d47efa
commit b5331d821c
6 changed files with 29 additions and 5 deletions

View File

@ -195,7 +195,17 @@ object AtasciiEncoding {
fun encode(str: String): Result<List<UByte>, CharConversionException> {
return Ok(str.map { encodeTable.getValue(it).toUByte() })
val mapped = str.map { chr ->
when (chr) {
'\u0000' -> 0u
in '\u8000'..'\u80ff' -> {
// special case: take the lower 8 bit hex value directly
(chr.code - 0x8000).toUByte()
}
else -> encodeTable.getValue(chr).toUByte()
}
}
return Ok(mapped)
}
fun decode(bytes: List<UByte>): Result<String, CharConversionException> {

View File

@ -11,7 +11,17 @@ object IsoEncoding {
fun encode(str: String): Result<List<UByte>, CharConversionException> {
return try {
Ok(str.toByteArray(charset).map { it.toUByte() })
val mapped = str.map { chr ->
when (chr) {
'\u0000' -> 0u
in '\u8000'..'\u80ff' -> {
// special case: take the lower 8 bit hex value directly
(chr.code - 0x8000).toUByte()
}
else -> charset.encode(chr.toString())[0].toUByte()
}
}
Ok(mapped)
} catch (ce: CharConversionException) {
Err(ce)
}

View File

@ -8,7 +8,7 @@
txt {
sub clear_screen() {
txt.chrout(125)
syscall1(3, "\x1b[2J\x1B[H")
}
sub nl() {

View File

@ -5,6 +5,7 @@ For next release
^^^^^^^^^^^^^^^^
- simplify cx16.joystick_get2() once this cx16 rom issue is resolved: https://github.com/commanderx16/x16-rom/issues/203
Can now be resolved because the fix got merged https://github.com/commanderx16/x16-rom/pull/204
- add unit test for the string encoders special handling of 0x0000 and 0x8000-0x80ff
...

View File

@ -1,7 +1,11 @@
%import textio
; NOTE: meant to test to virtual machine output target (use -target vitual)
main {
sub start() {
txt.clear_screen()
txt.print("Welcome to a prog8 pixel shader :-)\n")
syscall1(8, 0) ; enable lo res creen
ubyte shifter

View File

@ -193,10 +193,9 @@ class Assembler {
}
}
'x' -> {
// special hack 0x8000..0x80ff will be outputted verbatim without encoding
try {
val hex = ("" + iter.nextChar() + iter.nextChar()).toInt(16)
(0x8000 + hex).toChar()
hex.toChar()
} catch (sb: StringIndexOutOfBoundsException) {
throw IllegalArgumentException("invalid \\x escape sequence")
} catch (nf: NumberFormatException) {