Skip to content

Commit e1e1c97

Browse files
committed
Clean up error messages for reading char values
1 parent 3e417a9 commit e1e1c97

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,16 +3543,16 @@ final class JsonReader private[jsoniter_scala](
35433543
case 'f' => '\f'
35443544
case '\\' => '\\'
35453545
case '/' => '/'
3546-
case _ => illegalEscapeSequenceError(pos + 1)
3546+
case _ => escapeSequenceError(pos + 1)
35473547
}
35483548
parseEncodedString(i + 1, lim, charBuf, pos + 2)
35493549
} else if (remaining > 5) {
35503550
val ch1 = readEscapedUnicode(pos + 2, buf)
35513551
charBuf(i) = ch1
35523552
if (ch1 < 0xD800 || ch1 > 0xDFFF) parseEncodedString(i + 1, lim, charBuf, pos + 6)
35533553
else if (remaining > 11) {
3554-
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
3555-
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
3554+
if (buf(pos + 6) != '\\') escapeSequenceError(pos + 6)
3555+
if (buf(pos + 7) != 'u') escapeSequenceError(pos + 7)
35563556
val ch2 = readEscapedUnicode(pos + 8, buf)
35573557
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
35583558
charBuf(i + 1) = ch2
@@ -3600,7 +3600,7 @@ final class JsonReader private[jsoniter_scala](
36003600
if (remaining > 0) {
36013601
val b1 = buf(pos)
36023602
if (b1 >= 0) {
3603-
if (b1 == '"') decodeError("illegal value for char", pos)
3603+
if (b1 == '"') decodeError("illegal character", pos)
36043604
else if (b1 != '\\') { // 0aaaaaaa (UTF-8 byte) -> 000000000aaaaaaa (UTF-16 char)
36053605
if (b1 < ' ') unescapedControlCharacterError(pos)
36063606
head = pos + 1
@@ -3618,11 +3618,11 @@ final class JsonReader private[jsoniter_scala](
36183618
case '"' => '"'
36193619
case '/' => '/'
36203620
case '\\' => '\\'
3621-
case _ => illegalEscapeSequenceError(pos + 1)
3621+
case _ => escapeSequenceError(pos + 1)
36223622
}
36233623
} else if (remaining > 5) {
36243624
val ch = readEscapedUnicode(pos + 2, buf)
3625-
if (ch >= 0xD800 && ch <= 0xDFFF) decodeError("illegal surrogate character", pos + 5)
3625+
if (ch >= 0xD800 && ch <= 0xDFFF) surrogateCharacterError(pos + 5)
36263626
head = pos + 6
36273627
ch
36283628
} else parseChar(loadMoreOrError(pos))
@@ -3644,7 +3644,7 @@ final class JsonReader private[jsoniter_scala](
36443644
head = pos + 3
36453645
ch
36463646
} else parseChar(loadMoreOrError(pos))
3647-
} else if ((b1 >> 3) == -2) decodeError("illegal surrogate character", pos + 3)
3647+
} else if ((b1 >> 3) == -2) surrogateCharacterError(pos + 3)
36483648
else malformedBytesError(b1, pos)
36493649
} else parseChar(loadMoreOrError(pos))
36503650
}
@@ -3798,7 +3798,11 @@ final class JsonReader private[jsoniter_scala](
37983798
hexDigitError(pos + 1)
37993799
}
38003800

3801-
private[this] def illegalEscapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
3801+
private[this] def characterError(pos: Int): Nothing = decodeError("illegal character", pos)
3802+
3803+
private[this] def escapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
3804+
3805+
private[this] def surrogateCharacterError(pos: Int): Nothing = decodeError("illegal surrogate character", pos)
38023806

38033807
private[this] def unescapedControlCharacterError(pos: Int): Nothing = decodeError("unescaped control character", pos)
38043808

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,16 +4019,16 @@ final class JsonReader private[jsoniter_scala](
40194019
case 'f' => '\f'
40204020
case '\\' => '\\'
40214021
case '/' => '/'
4022-
case _ => illegalEscapeSequenceError(pos + 1)
4022+
case _ => escapeSequenceError(pos + 1)
40234023
}
40244024
parseEncodedString(i + 1, lim, charBuf, pos + 2)
40254025
} else if (remaining > 5) {
40264026
val ch1 = readEscapedUnicode(pos + 2, buf)
40274027
charBuf(i) = ch1
40284028
if (ch1 < 0xD800 || ch1 > 0xDFFF) parseEncodedString(i + 1, lim, charBuf, pos + 6)
40294029
else if (remaining > 11) {
4030-
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
4031-
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
4030+
if (buf(pos + 6) != '\\') escapeSequenceError(pos + 6)
4031+
if (buf(pos + 7) != 'u') escapeSequenceError(pos + 7)
40324032
val ch2 = readEscapedUnicode(pos + 8, buf)
40334033
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
40344034
charBuf(i + 1) = ch2
@@ -4076,7 +4076,7 @@ final class JsonReader private[jsoniter_scala](
40764076
if (remaining > 0) {
40774077
val b1 = buf(pos)
40784078
if (b1 >= 0) {
4079-
if (b1 == '"') decodeError("illegal value for char", pos)
4079+
if (b1 == '"') characterError(pos)
40804080
else if (b1 != '\\') { // 0aaaaaaa (UTF-8 byte) -> 000000000aaaaaaa (UTF-16 char)
40814081
if (b1 < ' ') unescapedControlCharacterError(pos)
40824082
head = pos + 1
@@ -4094,11 +4094,11 @@ final class JsonReader private[jsoniter_scala](
40944094
case '"' => '"'
40954095
case '/' => '/'
40964096
case '\\' => '\\'
4097-
case _ => illegalEscapeSequenceError(pos + 1)
4097+
case _ => escapeSequenceError(pos + 1)
40984098
}
40994099
} else if (remaining > 5) {
41004100
val ch = readEscapedUnicode(pos + 2, buf)
4101-
if (ch >= 0xD800 && ch <= 0xDFFF) decodeError("illegal surrogate character", pos + 5)
4101+
if (ch >= 0xD800 && ch <= 0xDFFF) surrogateCharacterError(pos + 5)
41024102
head = pos + 6
41034103
ch
41044104
} else parseChar(loadMoreOrError(pos))
@@ -4120,7 +4120,7 @@ final class JsonReader private[jsoniter_scala](
41204120
head = pos + 3
41214121
ch
41224122
} else parseChar(loadMoreOrError(pos))
4123-
} else if ((b1 >> 3) == -2) decodeError("illegal surrogate character", pos + 3)
4123+
} else if ((b1 >> 3) == -2) surrogateCharacterError(pos + 3)
41244124
else malformedBytesError(b1, pos)
41254125
} else parseChar(loadMoreOrError(pos))
41264126
}
@@ -4274,7 +4274,11 @@ final class JsonReader private[jsoniter_scala](
42744274
hexDigitError(pos + 1)
42754275
}
42764276

4277-
private[this] def illegalEscapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
4277+
private[this] def characterError(pos: Int): Nothing = decodeError("illegal character", pos)
4278+
4279+
private[this] def escapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
4280+
4281+
private[this] def surrogateCharacterError(pos: Int): Nothing = decodeError("illegal surrogate character", pos)
42784282

42794283
private[this] def unescapedControlCharacterError(pos: Int): Nothing = decodeError("unescaped control character", pos)
42804284

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4015,16 +4015,16 @@ final class JsonReader private[jsoniter_scala](
40154015
case 'f' => '\f'
40164016
case '\\' => '\\'
40174017
case '/' => '/'
4018-
case _ => illegalEscapeSequenceError(pos + 1)
4018+
case _ => escapeSequenceError(pos + 1)
40194019
}
40204020
parseEncodedString(i + 1, lim, charBuf, pos + 2)
40214021
} else if (remaining > 5) {
40224022
val ch1 = readEscapedUnicode(pos + 2, buf)
40234023
charBuf(i) = ch1
40244024
if (ch1 < 0xD800 || ch1 > 0xDFFF) parseEncodedString(i + 1, lim, charBuf, pos + 6)
40254025
else if (remaining > 11) {
4026-
if (buf(pos + 6) != '\\') illegalEscapeSequenceError(pos + 6)
4027-
if (buf(pos + 7) != 'u') illegalEscapeSequenceError(pos + 7)
4026+
if (buf(pos + 6) != '\\') escapeSequenceError(pos + 6)
4027+
if (buf(pos + 7) != 'u') escapeSequenceError(pos + 7)
40284028
val ch2 = readEscapedUnicode(pos + 8, buf)
40294029
if (ch1 >= 0xDC00 || (ch2 & 0xFC00) != 0xDC00) decodeError("illegal surrogate character pair", pos + 11)
40304030
charBuf(i + 1) = ch2
@@ -4072,7 +4072,7 @@ final class JsonReader private[jsoniter_scala](
40724072
if (remaining > 0) {
40734073
val b1 = buf(pos)
40744074
if (b1 >= 0) {
4075-
if (b1 == '"') decodeError("illegal value for char", pos)
4075+
if (b1 == '"') characterError(pos)
40764076
else if (b1 != '\\') { // 0aaaaaaa (UTF-8 byte) -> 000000000aaaaaaa (UTF-16 char)
40774077
if (b1 < ' ') unescapedControlCharacterError(pos)
40784078
head = pos + 1
@@ -4090,11 +4090,11 @@ final class JsonReader private[jsoniter_scala](
40904090
case '"' => '"'
40914091
case '/' => '/'
40924092
case '\\' => '\\'
4093-
case _ => illegalEscapeSequenceError(pos + 1)
4093+
case _ => escapeSequenceError(pos + 1)
40944094
}
40954095
} else if (remaining > 5) {
40964096
val ch = readEscapedUnicode(pos + 2, buf)
4097-
if (ch >= 0xD800 && ch <= 0xDFFF) decodeError("illegal surrogate character", pos + 5)
4097+
if (ch >= 0xD800 && ch <= 0xDFFF) surrogateCharacterError(pos + 5)
40984098
head = pos + 6
40994099
ch
41004100
} else parseChar(loadMoreOrError(pos))
@@ -4116,7 +4116,7 @@ final class JsonReader private[jsoniter_scala](
41164116
head = pos + 3
41174117
ch
41184118
} else parseChar(loadMoreOrError(pos))
4119-
} else if ((b1 >> 3) == -2) decodeError("illegal surrogate character", pos + 3)
4119+
} else if ((b1 >> 3) == -2) surrogateCharacterError(pos + 3)
41204120
else malformedBytesError(b1, pos)
41214121
} else parseChar(loadMoreOrError(pos))
41224122
}
@@ -4270,7 +4270,11 @@ final class JsonReader private[jsoniter_scala](
42704270
hexDigitError(pos + 1)
42714271
}
42724272

4273-
private[this] def illegalEscapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
4273+
private[this] def characterError(pos: Int): Nothing = decodeError("illegal character", pos)
4274+
4275+
private[this] def escapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
4276+
4277+
private[this] def surrogateCharacterError(pos: Int): Nothing = decodeError("illegal surrogate character", pos)
42744278

42754279
private[this] def unescapedControlCharacterError(pos: Int): Nothing = decodeError("unescaped control character", pos)
42764280

jsoniter-scala-core/shared/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReaderSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,7 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
23292329
checkError("", "unexpected end of input, offset: 0x00000000")
23302330
checkError(""""""", "unexpected end of input, offset: 0x00000001")
23312331
checkError(""""\""", "unexpected end of input, offset: 0x00000002")
2332-
checkError("""""""", "illegal value for char, offset: 0x00000001")
2332+
checkError("""""""", "illegal character, offset: 0x00000001")
23332333
checkError2(Array[Byte](0x22.toByte, 0xC0.toByte), "unexpected end of input, offset: 0x00000002")
23342334
checkError2(Array[Byte](0x22.toByte, 0xE0.toByte, 0x80.toByte), "unexpected end of input, offset: 0x00000003")
23352335
}

0 commit comments

Comments
 (0)