prog8/codeGenTargets/src/prog8/codegen/target/cbm/IsoEncoding.kt

28 lines
813 B
Kotlin
Raw Normal View History

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
object IsoEncoding {
2022-01-19 19:45:24 +00:00
val charset: Charset = Charset.forName("ISO-8859-15")
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() })
} catch (ce: CharConversionException) {
Err(ce)
}
}
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))
} catch (ce: CharConversionException) {
Err(ce)
}
}
}