2022-01-19 00:08:24 +00:00
|
|
|
package prog8.codegen.target.cbm
|
|
|
|
|
|
|
|
import com.github.michaelbull.result.Result
|
|
|
|
import com.github.michaelbull.result.Ok
|
|
|
|
import com.github.michaelbull.result.Err
|
|
|
|
import java.io.CharConversionException
|
|
|
|
|
|
|
|
object IsoEncoding {
|
|
|
|
fun encode(str: String): Result<List<UByte>, CharConversionException> {
|
|
|
|
return try {
|
|
|
|
Ok(str.toByteArray(Charsets.ISO_8859_1).map { it.toUByte() })
|
|
|
|
} catch (ce: CharConversionException) {
|
|
|
|
Err(ce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:27:28 +00:00
|
|
|
fun decode(bytes: List<UByte>): Result<String, CharConversionException> {
|
|
|
|
return try {
|
|
|
|
Ok(String(bytes.map { it.toByte() }.toByteArray(), Charsets.ISO_8859_1))
|
|
|
|
} catch (ce: CharConversionException) {
|
|
|
|
Err(ce)
|
|
|
|
}
|
2022-01-19 00:08:24 +00:00
|
|
|
}
|
|
|
|
}
|