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
|
2022-01-19 19:45:24 +00:00
|
|
|
import java.nio.charset.Charset
|
2022-01-19 00:08:24 +00:00
|
|
|
|
|
|
|
object IsoEncoding {
|
2022-01-19 19:45:24 +00:00
|
|
|
val charset: Charset = Charset.forName("ISO-8859-15")
|
|
|
|
|
2022-01-19 00:08:24 +00:00
|
|
|
fun encode(str: String): Result<List<UByte>, CharConversionException> {
|
|
|
|
return try {
|
2022-01-19 19:45:24 +00:00
|
|
|
Ok(str.toByteArray(charset).map { it.toUByte() })
|
2022-01-19 00:08:24 +00:00
|
|
|
} catch (ce: CharConversionException) {
|
|
|
|
Err(ce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:27:28 +00:00
|
|
|
fun decode(bytes: List<UByte>): Result<String, CharConversionException> {
|
|
|
|
return try {
|
2022-01-19 19:45:24 +00:00
|
|
|
Ok(String(bytes.map { it.toByte() }.toByteArray(), charset))
|
2022-01-19 00:27:28 +00:00
|
|
|
} catch (ce: CharConversionException) {
|
|
|
|
Err(ce)
|
|
|
|
}
|
2022-01-19 00:08:24 +00:00
|
|
|
}
|
|
|
|
}
|