From 7be4a2b1163e14441b3d9cd609a5829cd20c5383 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Sun, 3 Mar 2024 23:51:00 +0000 Subject: [PATCH] Simplified intToStr to be always only 10-based: it was never possible to invoke arbitrary-radix conversions anyway, and further formatting number as hex/oct/bin will be done using format specifiers in f-strings (much more flexible and compatible) --- .../struct/translators/BaseTranslator.scala | 2 +- .../struct/translators/CSharpTranslator.scala | 6 ++---- .../struct/translators/CommonMethods.scala | 4 ++-- .../struct/translators/CppTranslator.scala | 14 ++++---------- .../translators/ExpressionValidator.scala | 3 +-- .../struct/translators/GoTranslator.scala | 6 +++--- .../translators/JavaScriptTranslator.scala | 4 ++-- .../struct/translators/JavaTranslator.scala | 4 ++-- .../struct/translators/LuaTranslator.scala | 9 ++------- .../struct/translators/NimTranslator.scala | 2 +- .../struct/translators/PHPTranslator.scala | 12 +++--------- .../struct/translators/PerlTranslator.scala | 18 ++---------------- .../struct/translators/PythonTranslator.scala | 14 ++------------ .../struct/translators/RubyTranslator.scala | 4 ++-- .../struct/translators/RustTranslator.scala | 12 +++--------- 15 files changed, 32 insertions(+), 82 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala index 8b07ee6ea..e59db2048 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala @@ -218,7 +218,7 @@ abstract class BaseTranslator(val provider: TypeProvider) def anyToStr(value: Ast.expr): String = { detectType(value) match { case _: IntType => - intToStr(value, Ast.expr.IntNum(10)) + intToStr(value) case _: StrType => translate(value) case otherType => diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala index b128c617c..4f56a2bb6 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala @@ -100,10 +100,8 @@ class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends B translate(v) override def floatToInt(v: expr): String = s"(long) (${translate(v)})" - override def intToStr(i: expr, base: expr): String = { - importList.add("System") - s"Convert.ToString((long) (${translate(i)}), ${translate(base)})" - } + override def intToStr(i: expr): String = + s"(${translate(i)}).ToString()" override def bytesToStr(bytesExpr: String, encoding: String): String = s"""System.Text.Encoding.GetEncoding("$encoding").GetString($bytesExpr)""" override def strLength(s: expr): String = diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CommonMethods.scala b/shared/src/main/scala/io/kaitai/struct/translators/CommonMethods.scala index 911b35ac3..b7e2e79a3 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CommonMethods.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CommonMethods.scala @@ -46,7 +46,7 @@ abstract trait CommonMethods[T] extends TypeDetector { } case _: IntType => attr.name match { - case "to_s" => intToStr(value, Ast.expr.IntNum(10)) + case "to_s" => intToStr(value) } case _: FloatType => attr.name match { @@ -128,7 +128,7 @@ abstract trait CommonMethods[T] extends TypeDetector { def bytesToStr(value: Ast.expr, encoding: String): T - def intToStr(value: Ast.expr, base: Ast.expr): T + def intToStr(value: Ast.expr): T def floatToInt(value: Ast.expr): T diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala index de059dc49..8218d50d6 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala @@ -190,16 +190,10 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import s"((${translate(v)}) ? 1 : 0)" override def floatToInt(v: expr): String = s"static_cast(${translate(v)})" - override def intToStr(i: expr, base: expr): String = { - val baseStr = translate(base) - baseStr match { - case "10" => - // FIXME: proper way for C++11, but not available in earlier versions - //s"std::to_string(${translate(i)})" - s"${CppCompiler.kstreamName}::to_string(${translate(i)})" - case _ => throw new UnsupportedOperationException(baseStr) - } - } + override def intToStr(i: expr): String = + // FIXME: proper way for C++11, but not available in earlier versions + //s"std::to_string(${translate(i)})" + s"${CppCompiler.kstreamName}::to_string(${translate(i)})" override def bytesToStr(bytesExpr: String, encoding: String): String = s"""${CppCompiler.kstreamName}::bytes_to_str($bytesExpr, "$encoding")""" override def bytesLength(b: Ast.expr): String = diff --git a/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala b/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala index b1dc349cf..bdc7998cd 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/ExpressionValidator.scala @@ -104,9 +104,8 @@ class ExpressionValidator(val provider: TypeProvider) validate(value) } - override def intToStr(value: Ast.expr, num: Ast.expr): Unit = { + override def intToStr(value: Ast.expr): Unit = { validate(value) - validate(num) } override def floatToInt(value: Ast.expr): Unit = validate(value) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala index b51eff6df..7e85ab670 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala @@ -393,9 +393,9 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo ResultString(s"${translate(s)}[${translate(from)}:${translate(to)}]") } - override def intToStr(value: Ast.expr, base: Ast.expr): TranslatorResult = { + override def intToStr(value: Ast.expr): TranslatorResult = { importList.add("strconv") - ResultString(s"strconv.FormatInt(int64(${translate(value)}), ${translate(base)})") + ResultString(s"strconv.Itoa(int64(${translate(value)}))") } override def floatToInt(value: Ast.expr) = @@ -518,7 +518,7 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo def localVarName(n: Int) = s"tmp$n" - def outAddErrCheck() { + def outAddErrCheck(): Unit = { out.puts("if err != nil {") out.inc diff --git a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala index c14d90992..2e8e9b251 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala @@ -104,8 +104,8 @@ class JavaScriptTranslator(provider: TypeProvider) extends BaseTranslator(provid override def floatToInt(v: expr): String = s"Math.trunc(${translate(v)})" - override def intToStr(i: expr, base: expr): String = - s"(${translate(i)}).toString(${translate(base)})" + override def intToStr(i: expr): String = + s"(${translate(i)}).toString()" override def bytesToStr(bytesExpr: String, encoding: String): String = s"""${JavaScriptCompiler.kstreamName}.bytesToStr($bytesExpr, "$encoding")""" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala index 5130a069f..8bcb484db 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/JavaTranslator.scala @@ -112,8 +112,8 @@ class JavaTranslator(provider: TypeProvider, importList: ImportList) extends Bas s"${translate(v)}.id()" override def floatToInt(v: expr): String = s"(int) (${translate(v)} + 0)" - override def intToStr(i: expr, base: expr): String = - s"Long.toString(${translate(i)}, ${translate(base)})" + override def intToStr(i: expr): String = + s"Long.toString(${translate(i)})" override def bytesToStr(bytesExpr: String, encoding: String): String = { // Java has a small number of standard charsets preloaded. Accessing them as constants is more // efficient than looking them up by string in a map, so we utilize this when as possible. diff --git a/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala index b3287d4e9..64bf14f99 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/LuaTranslator.scala @@ -103,13 +103,8 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base s"(${translate(v)} and 1 or 0)" override def floatToInt(v: Ast.expr): String = s"(${translate(v)} > 0) and math.floor(${translate(v)}) or math.ceil(${translate(v)})" - override def intToStr(i: Ast.expr, base: Ast.expr): String = { - val baseStr = translate(base) - baseStr match { - case "10" => s"tostring(${translate(i)})" - case _ => throw new UnsupportedOperationException(baseStr) - } - } + override def intToStr(i: Ast.expr): String = + s"tostring(${translate(i)})" override def bytesToStr(bytesExpr: String, encoding: String): String = { importList.add("local str_decode = require(\"string_decode\")") diff --git a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala index 61e9e506c..4163fe2f7 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala @@ -114,7 +114,7 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base override def arraySize(a: expr): String = s"len(${translate(a)})" override def enumToInt(v: expr, et: EnumType): String = s"ord(${translate(v)})" override def floatToInt(v: expr): String = s"int(${translate(v)})" - override def intToStr(v: expr, base: expr): String = { + override def intToStr(v: expr): String = { importList.add("strutils") s"intToStr(int(${translate(v)}))" } diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala index c1cf4f59b..d449c9ec5 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PHPTranslator.scala @@ -99,15 +99,9 @@ class PHPTranslator(provider: TypeProvider, config: RuntimeConfig) extends BaseT override def floatToInt(v: expr): String = s"intval(${translate(v)})" - override def intToStr(i: expr, base: expr): String = { - val baseStr = translate(base) - baseStr match { - case "10" => - s"strval(${translate(i)})" - case _ => - s"base_convert(strval(${translate(i)}), 10, $baseStr)" - } - } + override def intToStr(i: expr): String = + s"strval(${translate(i)})" + override def bytesToStr(bytesExpr: String, encoding: String): String = s"""${PHPCompiler.kstreamName}::bytesToStr($bytesExpr, "$encoding")""" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala index 7949f2fde..f76893636 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PerlTranslator.scala @@ -134,22 +134,8 @@ class PerlTranslator(provider: TypeProvider, importList: ImportList) extends Bas translate(v) override def floatToInt(v: Ast.expr): String = s"int(${translate(v)})" - override def intToStr(i: Ast.expr, base: Ast.expr): String = { - val baseStr = translate(base) - val format = baseStr match { - case "2" => - s"%b" - case "8" => - s"%o" - case "10" => - s"%d" - case "16" => - s"0x%X" - case _ => throw new UnsupportedOperationException(baseStr) - } - - s"sprintf('$format', ${translate(i)})" - } + override def intToStr(i: Ast.expr): String = + s"sprintf('%d', ${translate(i)})" override def bytesToStr(bytesExpr: String, encoding: String): String = { importList.add("Encode") s"""Encode::decode("$encoding", $bytesExpr)""" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala index 2a4e21f81..57607aa81 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala @@ -90,18 +90,8 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList) extends B s"int(${translate(v)})" override def floatToInt(v: Ast.expr): String = s"int(${translate(v)})" - override def intToStr(i: Ast.expr, base: Ast.expr): String = { - val baseStr = translate(base) - val func = baseStr match { - case "2" => "bin" - case "8" => "oct" - case "10" => "str" - case "16" => "hex" - case _ => throw new UnsupportedOperationException(baseStr) - } - - s"$func(${translate(i)})" - } + override def intToStr(i: Ast.expr): String = + s"str(${translate(i)})" override def bytesToStr(bytesExpr: String, encoding: String): String = s"""($bytesExpr).decode("$encoding")""" diff --git a/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala index 59f716c40..8046c05ee 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/RubyTranslator.scala @@ -84,8 +84,8 @@ class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider) s"${enumInverseMap(et)}[${translate(v)}]" override def floatToInt(v: Ast.expr): String = s"(${translate(v)}).to_i" - override def intToStr(i: Ast.expr, base: Ast.expr): String = - translate(i) + s".to_s(${translate(base)})" + override def intToStr(i: Ast.expr): String = + translate(i) + s".to_s" override def bytesToStr(bytesExpr: String, encoding: String): String = { // We can skip "encode to UTF8" if we're 100% sure that the string we're handling is already diff --git a/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala index 84c931eac..beff3e76e 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/RustTranslator.scala @@ -84,15 +84,9 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig) extends Base override def floatToInt(v: expr): String = s"${translate(v)} as i32" - override def intToStr(i: expr, base: expr): String = { - val baseStr = translate(base) - baseStr match { - case "10" => - s"${translate(i)}.to_string()" - case _ => - s"base_convert(strval(${translate(i)}), 10, $baseStr)" - } - } + override def intToStr(i: expr): String = + s"${translate(i)}.to_string()" + override def bytesToStr(bytesExpr: String, encoding: String): String = encoding match { case "ASCII" =>