Skip to content

Commit

Permalink
Simplified intToStr to be always only 10-based: it was never possible…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
GreyCat committed Mar 3, 2024
1 parent 29bced3 commit 7be4a2b
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(${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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\")")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}))"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" =>
Expand Down

0 comments on commit 7be4a2b

Please sign in to comment.