From 742fc50ccc1feed562b35256d9111236e2937752 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Tue, 31 Mar 2020 17:58:46 +0200 Subject: [PATCH] Don't used `str2word` as `scrstr2word` when the string terminator matches, but digits don't --- include/scrstring.mfk | 9 ++++++++- src/main/scala/millfork/Platform.scala | 3 +++ src/main/scala/millfork/parser/TextCodec.scala | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/scrstring.mfk b/include/scrstring.mfk index e99b88b0..4eee62f2 100644 --- a/include/scrstring.mfk +++ b/include/scrstring.mfk @@ -6,7 +6,6 @@ alias scrstrzcmp = strzcmp alias scrstrzcopy = strzcopy alias scrstrzpaste = strzpaste alias scrstrzappendchar = strzappendchar -alias scrstrz2word = strz2word alias scrstrzappend = strzappend @@ -27,6 +26,14 @@ void scrstrzappendchar(pointer buffer, byte char) { buffer[1] = nullchar_scr } +#endif + +#if DECIMALS_SAME + +alias scrstrz2word = strz2word + +#else + word scrstrz2word(pointer str) { byte i byte char diff --git a/src/main/scala/millfork/Platform.scala b/src/main/scala/millfork/Platform.scala index 29d0d093..d176c315 100644 --- a/src/main/scala/millfork/Platform.scala +++ b/src/main/scala/millfork/Platform.scala @@ -295,6 +295,9 @@ object Platform { val builtInFeatures = builtInCpuFeatures(cpu) ++ Map( "ENCODING_SAME" -> toLong(codec.name == srcCodec.name), + "DECIMALS_SAME" -> toLong(codec.stringTerminator == srcCodec.stringTerminator && ('0' to '9').forall{c => + codec.encodeOneChar(c) == srcCodec.encodeOneChar(c) + }), "ENCCONV_SUPPORTED" -> toLong((codec.name, srcCodec.name) match { case (TextCodec.Petscii.name, TextCodec.CbmScreencodes.name) | (TextCodec.PetsciiJp.name, TextCodec.CbmScreencodesJp.name) | diff --git a/src/main/scala/millfork/parser/TextCodec.scala b/src/main/scala/millfork/parser/TextCodec.scala index e53739c0..eed934b1 100644 --- a/src/main/scala/millfork/parser/TextCodec.scala +++ b/src/main/scala/millfork/parser/TextCodec.scala @@ -21,6 +21,10 @@ sealed trait TextCodec { def decode(by: Int): Char + // encodes one char from BMP, without any lookup tables, conversions, and stuff + // useful only for things like ASCII digits + def encodeOneChar(c: Char): List[Int] + def dump(): Unit = { (0 until 256).map(decode).zipWithIndex.grouped(32).map(row => row.head._2.toHexString + "\t" + row.map(_._1).mkString("")).foreach(println(_)) } @@ -105,6 +109,10 @@ class UnicodeTextCodec(override val name: String, val charset: Charset, override } } + def encodeOneChar(c: Char): List[Int] = { + c.toString.getBytes(charset).map(_.&(0xff)).toList + } + override def decode(by: Int): Char = { if (by >= 0x20 && by <= 0x7E) by.toChar else if (by == 0) '.' @@ -246,6 +254,8 @@ class TableTextCodec(override val name: String, val index = by & 0xff if (index < map.length) map(index) else TextCodec.NotAChar } + + override def encodeOneChar(c: Char): List[Int] = List(map.indexOf(c.toInt)) } object TextCodec {