Skip to content

Commit eb129e8

Browse files
committed
Handle illegal escape sequences with �
1 parent f4bf68b commit eb129e8

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

fluent.syntax/src/main/kotlin/org/projectfluent/syntax/processor/Processor.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.projectfluent.syntax.processor
22

33
import org.projectfluent.syntax.ast.* // ktlint-disable no-wildcard-imports
4-
import java.lang.Exception
54
import java.lang.StringBuilder
65

76
/**
@@ -161,21 +160,27 @@ class Processor {
161160
if (uni4 != "") {
162161
val codepoint = uni4.substring(1).toInt(16)
163162
if (Character.isBmpCodePoint(codepoint)) {
164-
return codepoint.toChar().toString()
163+
val char = codepoint.toChar()
164+
if (!Character.isSurrogate(char)) {
165+
return char.toString()
166+
}
165167
}
166168
}
167169

168170
val uni6 = matches.next()
169171
if (uni6 != "") {
170172
val codepoint = uni6.substring(1).toInt(16)
171173
if (Character.isValidCodePoint(codepoint)) {
172-
val builder = StringBuilder()
173-
builder.append(Character.highSurrogate(codepoint))
174-
builder.append(Character.lowSurrogate(codepoint))
175-
return builder
174+
val char = codepoint.toChar()
175+
if (!Character.isSurrogate(char)) {
176+
val builder = StringBuilder()
177+
builder.append(Character.highSurrogate(codepoint))
178+
builder.append(Character.lowSurrogate(codepoint))
179+
return builder
180+
}
176181
}
177182
}
178183

179-
throw Exception("Unexpected")
184+
return ""
180185
}
181186
}

fluent.syntax/src/test/kotlin/org/projectfluent/syntax/processor/ProcessorTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ internal class ProcessorTest {
8181
processor.unescapeLiteralsToText(pattern)
8282
)
8383

84+
pattern.elements.clear()
85+
pattern.elements.addAll(
86+
arrayOf(
87+
TextElement("Illegal escape sequence: "),
88+
Placeable(expression = StringLiteral("""\ud800"""))
89+
)
90+
)
91+
assertEquals(
92+
Pattern(TextElement("Illegal escape sequence: �")),
93+
processor.unescapeLiteralsToText(pattern)
94+
)
95+
96+
pattern.elements.clear()
97+
pattern.elements.addAll(
98+
arrayOf(
99+
TextElement("Illegal escape sequence: "),
100+
Placeable(expression = StringLiteral("""\U00d800"""))
101+
)
102+
)
103+
assertEquals(
104+
Pattern(TextElement("Illegal escape sequence: �")),
105+
processor.unescapeLiteralsToText(pattern)
106+
)
107+
84108
pattern.elements.clear()
85109
pattern.elements.addAll(
86110
arrayOf(

0 commit comments

Comments
 (0)