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
@@ -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> {
@@ -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)
}