Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sjsonnet/src/sjsonnet/Format.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ object Format {
case 'f' | 'F' => formatFloat(formatted, s)
case 'g' => formatGeneric(formatted, s).toLowerCase
case 'G' => formatGeneric(formatted, s)
case 'c' => widenRaw(formatted, s.toChar.toString)
case 'c' => widenRaw(formatted, Character.toString(s.toInt))
case 's' =>
if (s.toLong == s) widenRaw(formatted, s.toLong.toString)
else widenRaw(formatted, s.toString)
Expand Down
16 changes: 12 additions & 4 deletions sjsonnet/src/sjsonnet/stdlib/ArrayModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,16 @@ object ArrayModule extends AbstractFunctionModule {

case s: Val.Str =>
var current = init.force
for (char <- s.value) {
val str = s.value
var i = 0
while (i < str.length) {
val c = current
current = func.apply2(c, Val.Str(pos, new String(Array(char))), pos.noOffset)(
val codePoint = str.codePointAt(i)
current = func.apply2(c, Val.Str(pos, Character.toString(codePoint)), pos.noOffset)(
ev,
TailstrictModeDisabled
)
i += Character.charCount(codePoint)
}
current

Expand All @@ -324,9 +328,13 @@ object ArrayModule extends AbstractFunctionModule {
current
case s: Val.Str =>
var current = init.force
for (char <- s.value.reverse) {
val str = s.value
var i = str.length
while (i > 0) {
val codePoint = str.codePointBefore(i)
i -= Character.charCount(codePoint)
val c = current
current = func.apply2(Val.Str(pos, new String(Array(char))), c, pos.noOffset)(
current = func.apply2(Val.Str(pos, Character.toString(codePoint)), c, pos.noOffset)(
ev,
TailstrictModeDisabled
)
Expand Down
29 changes: 29 additions & 0 deletions sjsonnet/test/src/sjsonnet/UnicodeHandlingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,34 @@ object UnicodeHandlingTests extends TestSuite {
eval("""std.trim("🌍 ")""") ==> ujson.Str("🌍")
eval("""std.trim(" 🌍 ")""") ==> ujson.Str("🌍")
}

test("foldl") {
// foldl must iterate by codepoint, not UTF-16 code unit
eval("""std.foldl(function(acc, c) acc + [c], "a😀b", [])""") ==>
ujson.Arr("a", "😀", "b")
eval("""std.foldl(function(acc, c) acc + 1, "a😀b", 0)""") ==> ujson.Num(3)
eval("""std.foldl(function(acc, c) acc + [c], "🎉🔥", [])""") ==>
ujson.Arr("🎉", "🔥")
// Round-trip concatenation
eval("""std.foldl(function(acc, c) acc + c, "a😀b", "")""") ==> ujson.Str("a😀b")
}

test("foldr") {
// foldr must iterate by codepoint, not UTF-16 code unit
eval("""std.foldr(function(c, acc) acc + [c], "a😀b", [])""") ==>
ujson.Arr("b", "😀", "a")
eval("""std.foldr(function(c, acc) acc + [c], "🎉🔥", [])""") ==>
ujson.Arr("🔥", "🎉")
// Round-trip concatenation (right-to-left: 'b' then '😀' then 'a')
eval("""std.foldr(function(c, acc) acc + c, "a😀b", "")""") ==> ujson.Str("b😀a")
}

test("formatPercentC") {
// %c must handle non-BMP codepoints
eval("""std.format("%c", [128512])""") ==> ujson.Str("😀") // U+1F600
eval("""std.format("%c", [128293])""") ==> ujson.Str("🔥") // U+1F525
eval("""std.format("%c", [127757])""") ==> ujson.Str("🌍") // U+1F30D
eval("""std.format("%c", [65])""") ==> ujson.Str("A") // BMP char
}
}
}
Loading