Skip to content

Commit

Permalink
lua 5.1+ compatible encoding of unicode characters as escaped decimal…
Browse files Browse the repository at this point in the history
… bytes
  • Loading branch information
smarek committed Aug 19, 2020
1 parent 74b4229 commit 48c1f05
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
)

override def strLiteralUnicode(code: Char): String =
"\\u{%04x}".format(code.toInt)
decEscapeByteArray(String.valueOf(code).getBytes("UTF-8"))

override def numericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr) = {
override def numericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr): String = {
(detectType(left), detectType(right), op) match {
case (_: IntType, _: IntType, Ast.operator.Div) =>
s"math.floor(${translate(left)} / ${translate(right)})"
Expand Down Expand Up @@ -61,11 +61,11 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
override def doBoolLiteral(n: Boolean): String =
if (n) "true" else "false"
override def doArrayLiteral(t: DataType, value: Seq[Ast.expr]): String =
"{" + value.map((v) => translate(v)).mkString(", ") + "}"
"{" + value.map(v => translate(v)).mkString(", ") + "}"
override def doByteArrayLiteral(arr: Seq[Byte]): String =
"\"" + decEscapeByteArray(arr) + "\""

override def doLocalName(s: String) = s match {
override def doLocalName(s: String): String = s match {
case Identifier.ITERATOR => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
Expand Down Expand Up @@ -123,7 +123,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
s"string.byte(${translate(a)}, 1)"
override def bytesLast(a: Ast.expr): String = {
val table = translate(a)
s"string.byte(${table}, #${table})"
s"string.byte($table, #$table)"
}
override def bytesMin(a: Ast.expr): String = {
importList.add("local utils = require(\"utils\")")
Expand All @@ -146,7 +146,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
s"${translate(a)}[1]"
override def arrayLast(a: Ast.expr): String = {
val table = translate(a)
s"${table}[#${table}]"
s"$table[#$table]"
}
override def arraySize(a: Ast.expr): String =
s"#${translate(a)}"
Expand Down Expand Up @@ -193,5 +193,5 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
* @return array contents decimal-escaped as string
*/
private def decEscapeByteArray(arr: Seq[Byte]): String =
arr.map((x) => "\\%03d".format(x & 0xff)).mkString
arr.map(x => "\\%03d".format(x & 0xff)).mkString
}

0 comments on commit 48c1f05

Please sign in to comment.