Skip to content

Commit cf4f3e4

Browse files
committed
[JSON] JSONWriter.serialize(string:) improvement
Use 'for' look instead of 'while cursor != end { cursor += 1 }'
1 parent 17a0c6b commit cf4f3e4

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONEncoding.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,15 @@ private struct JSONWriter {
141141
var string = string
142142
write(ascii: "\"")
143143
string.withUTF8 { utf8 in
144-
var cursor = utf8.baseAddress!
145-
let end = utf8.baseAddress! + utf8.count
146-
var mark = cursor
147-
func flush() {
148-
if cursor > mark {
144+
let start = utf8.baseAddress!
145+
let end = start + utf8.count
146+
var mark = start
147+
148+
for cursor in start..<end {
149+
@inline(__always) func flush() {
149150
write(utf8: UnsafeBufferPointer(start: mark, count: cursor - mark))
151+
mark = cursor + 1
150152
}
151-
cursor += 1
152-
mark = cursor
153-
}
154-
while cursor != end {
155153
switch cursor.pointee {
156154
case UInt8(ascii: "\""):
157155
flush()
@@ -186,13 +184,13 @@ private struct JSONWriter {
186184
}
187185
default:
188186
// Accumulate this byte.
189-
cursor += 1
187+
break
190188
}
191189
}
192190

193191
// Append accumulated bytes.
194-
if cursor > mark {
195-
write(utf8: UnsafeBufferPointer(start: mark, count: cursor - mark))
192+
if end > mark {
193+
write(utf8: UnsafeBufferPointer(start: mark, count: end - mark))
196194
}
197195
}
198196
write(ascii: "\"")

0 commit comments

Comments
 (0)