Skip to content

Commit 4c84edb

Browse files
committed
[JSON] Small changes
* typo * Remove unnecessary withExtendedLifetime() * remove JSONObject.subscript(_:) as it's not O(1) * Use 'LosslessStringConvertible'
1 parent c0fc9b5 commit 4c84edb

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ private struct JSONMap {
106106
let data: [Int]
107107

108108
/// Top-level value.
109-
func withValue<T>(_ fn: (JSONMapValue) throws -> T) rethrows -> T {
110-
try withExtendedLifetime(data) {
111-
try data.withUnsafeBufferPointer { buf in
112-
try fn(JSONMapValue(data: buf.baseAddress!))
113-
}
109+
func withValue<T>(_ body: (JSONMapValue) throws -> T) rethrows -> T {
110+
try data.withUnsafeBufferPointer { buf in
111+
try body(JSONMapValue(data: buf.baseAddress!))
114112
}
115113
}
116114
}
@@ -188,7 +186,7 @@ private struct JSONScanner {
188186
}
189187

190188
@inline(__always)
191-
mutating func skipWhilespace() {
189+
mutating func skipWhitespace() {
192190
while hasData {
193191
switch ptr.pointee {
194192
case UInt8(ascii: " "), UInt8(ascii: "\t"), UInt8(ascii: "\n"), UInt8(ascii: "\r"):
@@ -291,15 +289,15 @@ private struct JSONScanner {
291289

292290
mutating func scanObject() throws {
293291
let handle = map.startCollection(.object)
294-
skipWhilespace()
292+
skipWhitespace()
295293
if !advance(if: "}") {
296294
while hasData {
297295
try scanString(start: ptr)
298-
skipWhilespace()
296+
skipWhitespace()
299297
try expect(":")
300298
try scanValue()
301299
if advance(if: ",") {
302-
skipWhilespace()
300+
skipWhitespace()
303301
continue
304302
}
305303
break
@@ -311,12 +309,12 @@ private struct JSONScanner {
311309

312310
mutating func scanArray() throws {
313311
let handle = map.startCollection(.array)
314-
skipWhilespace()
312+
skipWhitespace()
315313
if !advance(if: "]") {
316314
while hasData {
317315
try scanValue()
318316
if advance(if: ",") {
319-
skipWhilespace()
317+
skipWhitespace()
320318
continue
321319
}
322320
break
@@ -327,7 +325,7 @@ private struct JSONScanner {
327325
}
328326

329327
mutating func scanValue() throws {
330-
skipWhilespace()
328+
skipWhitespace()
331329
let start = ptr
332330
switch try advance() {
333331
case UInt8(ascii: "n"):
@@ -347,7 +345,7 @@ private struct JSONScanner {
347345
case let chr:
348346
throw JSONError.unexpectedCharacter(chr, context: "value start")
349347
}
350-
skipWhilespace()
348+
skipWhitespace()
351349
}
352350

353351
static func scan(buffer: UnsafeBufferPointer<UInt8>) throws -> JSONMap {
@@ -689,11 +687,6 @@ extension JSONMapValue {
689687
func contains(key: String) -> Bool {
690688
return find(key) != nil
691689
}
692-
693-
@inline(__always)
694-
subscript(_ key: String) -> JSONMapValue? {
695-
return find(key)
696-
}
697690
}
698691

699692
@inline(__always)
@@ -924,7 +917,7 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
924917

925918
@inline(__always)
926919
func _getOrThrow(forKey key: Key) throws -> JSONMapValue {
927-
if let value = mapping[key.stringValue] {
920+
if let value = mapping.find(key.stringValue) {
928921
return value
929922
}
930923
throw DecodingError.keyNotFound(

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONEncoding.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ private class JSONReference {
7878
.init(backing: .string(str))
7979
}
8080
@inline(__always)
81-
static func number(_ integer: some BinaryInteger & CustomStringConvertible) -> JSONReference {
82-
.init(backing: .number(integer.description))
81+
static func number(_ integer: some BinaryInteger & LosslessStringConvertible) -> JSONReference {
82+
.init(backing: .number(String(integer)))
8383
}
8484
@inline(__always)
85-
static func number(_ floating: some BinaryFloatingPoint & CustomStringConvertible) -> JSONReference {
86-
.init(backing: .number(floating.description))
85+
static func number(_ floating: some BinaryFloatingPoint & LosslessStringConvertible) -> JSONReference {
86+
.init(backing: .number(String(floating)))
8787
}
8888
}
8989

@@ -246,10 +246,10 @@ extension JSONEncoding {
246246
func _encode(_ value: Bool) -> JSONReference {
247247
value ? .trueKeyword : .falseKeyword
248248
}
249-
func _encode(_ value: some BinaryFloatingPoint & CustomStringConvertible) -> JSONReference {
249+
func _encode(_ value: some BinaryFloatingPoint & LosslessStringConvertible) -> JSONReference {
250250
.number(value)
251251
}
252-
func _encode(_ value: some BinaryInteger & CustomStringConvertible) -> JSONReference {
252+
func _encode(_ value: some BinaryInteger & LosslessStringConvertible) -> JSONReference {
253253
.number(value)
254254
}
255255
func _encode(_ value: String) -> JSONReference {

0 commit comments

Comments
 (0)