Skip to content

Commit c986761

Browse files
committed
[JSON] Misc improvements
1 parent 9246250 commit c986761

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T
5555
map: [
5656
0: <OM>, -- object marker
5757
1: 15, | `- number of *map* elements in this collection
58-
2: <SS>, | --- key 1 'array'
58+
2: <SS>, | --- key 1: 'array'
5959
3: <int_ptr>, | | |- pointer in the payload
6060
4: 5, | | `- length
61-
5: <AM>, | --- value1 array
61+
5: <AM>, | --- value 1: array
6262
6: 4, | | `- number of *map* elements in the array
63-
7: <NM>, | | -- arr elm 1 '-1.3'
63+
7: <NM>, | | -- arr elm 1: '-1.3'
6464
8: <int_ptr>, | | |
6565
9: 4, | | |
66-
10: <TL>, | | -- arr elm 2 'true'
67-
11: <SS>, | --- key 2 'number'
66+
10: <TL>, | | -- arr elm 2: 'true'
67+
11: <SS>, | --- key 2: 'number'
6868
12: <int_ptr>, | |
6969
13: 6, | |
70-
14: <NM> | --- value1: '42'
70+
14: <NM> | --- value 2: '42'
7171
15: <int_ptr>, | |
7272
16: 2, | |
7373
]

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONEncoding.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,15 @@ private struct JSONWriter {
167167
case 0x0D:
168168
flush()
169169
write(string: "\\r")
170-
case 0x0...0xF:
170+
case 0x00...0x1F:
171171
let c = cursor.pointee
172172
flush()
173-
write(string: "\\u000")
174-
if (c < 10) {
175-
write(ascii: UInt8(ascii: "0") &+ c)
176-
} else {
177-
write(ascii: UInt8(ascii: "A") &+ (c &- 10))
178-
}
179-
case 0x10...0x1F:
180-
let c = cursor.pointee & 0xF
181-
flush()
182-
write(string: "\\u001")
183-
if (c < 10) {
184-
write(ascii: UInt8(ascii: "0") &+ c)
185-
} else {
186-
write(ascii: UInt8(ascii: "A") &+ (c &- 10))
173+
write(string: "\\u00")
174+
let _0 = UInt8(ascii: "0")
175+
let _A = UInt8(ascii: "A")
176+
for shift in stride(from: 4, through: 0, by: -4) {
177+
let d = (c >> shift) & 0xF
178+
write(ascii: d < 10 ? (_0 + d) : (_A + d - 10))
187179
}
188180
default:
189181
// Accumulate this byte.

Tests/SwiftCompilerPluginTest/JSONTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ final class JSONTests: XCTestCase {
6161
}
6262

6363
func testComplexStruct() {
64-
let empty = ComplexStruct(result: nil, diagnostics: [])
65-
_testRoundTrip(of: empty, expectedJSON: #"{"diagnostics":[]}"#)
64+
let empty = ComplexStruct(result: nil, diagnostics: [], elapsed: 0.0)
65+
_testRoundTrip(of: empty, expectedJSON: #"{"diagnostics":[],"elapsed":0.0}"#)
6666

6767
let value = ComplexStruct(
6868
result: "\tresult\nfoo",
@@ -72,12 +72,13 @@ final class JSONTests: XCTestCase {
7272
animal: .cat,
7373
data: [nil, 42]
7474
)
75-
]
75+
],
76+
elapsed: 42.3
7677
)
7778
_testRoundTrip(
7879
of: value,
7980
expectedJSON: #"""
80-
{"diagnostics":[{"animal":"cat","data":[null,42],"message":"error 🛑"}],"result":"\tresult\nfoo"}
81+
{"diagnostics":[{"animal":"cat","data":[null,42],"message":"error 🛑"}],"elapsed":42.3,"result":"\tresult\nfoo"}
8182
"""#
8283
)
8384
}
@@ -214,4 +215,5 @@ fileprivate struct ComplexStruct: Codable, Equatable {
214215

215216
var result: String?
216217
var diagnostics: [Diagnostic]
218+
var elapsed: Double
217219
}

0 commit comments

Comments
 (0)