Skip to content

Commit c366b48

Browse files
authored
Merge pull request rarestype#122 from rarestype/optimize-string-rendering
[patch]: optimize string descriptions
2 parents 5c8b87b + 930d7be commit c366b48

6 files changed

Lines changed: 16 additions & 12 deletions

File tree

Sources/JSONAST/JSON.Array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension JSON.Array: CustomStringConvertible {
1616
/// Reparsing and reserializing this string is guaranteed to return the
1717
/// same string.
1818
public var description: String {
19-
"[\(self.elements.map(\.description).joined(separator: ","))]"
19+
"[\(self.elements.lazy.map(\.description).joined(separator: ","))]"
2020
}
2121
}
2222
extension JSON.Array: ExpressibleByArrayLiteral {

Sources/JSONAST/JSON.Key.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ extension JSON.Key {
1515
self.init(rawValue: codingKey.stringValue)
1616
}
1717
}
18+
extension JSON.Key {
19+
var quoted: JSON.Literal<String> { .init(self.rawValue) }
20+
}
1821
extension JSON.Key: CustomStringConvertible {
1922
@inlinable public var description: String {
2023
self.rawValue

Sources/JSONAST/JSON.Number.Base10.Inverse.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ extension JSON.Number.Base10 {
66
/// - x: A positive exponent. If `x` is `2`, this subscript
77
/// will return `1e-2`.
88
/// - _: A ``BinaryFloatingPoint`` type.
9-
static subscript<T>(x: Int, as _: T.Type) -> T
10-
where T: BinaryFloatingPoint {
9+
static subscript<T>(x: Int, as _: T.Type) -> T where T: BinaryFloatingPoint {
1110
let inverses: [T] = [
1211
1,
1312
1e-1,

Sources/JSONAST/JSON.Number.Inline.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ extension JSON.Number {
3131
/// - units: The magnitude, in units of `places`.
3232
/// - places: The number of decimal places.
3333
@inlinable public init(sign: FloatingPointSign, units: UInt64, places: UInt32 = 0) {
34-
self.sign = sign
35-
self.units = units
36-
self._places = places
34+
self.sign = sign
35+
self.units = units
36+
self._places = places
3737
}
3838
}
3939
}
@@ -138,9 +138,9 @@ extension JSON.Number.Inline: CustomStringConvertible {
138138
case .minus: return "-\(self.units)"
139139
}
140140
}
141-
let places: Int = .init(self.places)
142141
let unpadded: String = .init(self.units)
143-
let string: String = """
142+
let places: Int = .init(self.places)
143+
let string: String = """
144144
\(String.init(repeating: "0", count: Swift.max(0, 1 + places - unpadded.count)))\
145145
\(unpadded)
146146
"""

Sources/JSONAST/JSON.Object.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ extension JSON.Object: CustomStringConvertible {
3939
/// same string.
4040
public var description: String {
4141
"""
42-
{\(self.fields.map {
43-
"\(String.init(JSON.Literal<String>.init($0.key.rawValue))):\($0.value)"
44-
}.joined(separator: ","))}
42+
{\(
43+
self.fields.lazy.map {
44+
"\(String.init($0.key.quoted)):\($0.value)"
45+
}.joined(separator: ",")
46+
)}
4547
"""
4648
}
4749
}

Sources/JSONAST/JSON.TypecastError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension JSON {
1010
}
1111
}
1212
extension JSON.TypecastError {
13-
@inlinable public init(invalid json: __shared JSON.Node) {
13+
@inlinable public init(invalid json: borrowing JSON.Node) {
1414
switch json {
1515
case .null: self = .null
1616
case .bool: self = .bool

0 commit comments

Comments
 (0)