Skip to content

Commit 773afaa

Browse files
committed
Implement the missing non-contiguous String case
While I'm digging around, fix some whitespace errors and simplify a few minor points in the code.
1 parent c427259 commit 773afaa

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

Sources/SwiftProtobuf/BinaryReverseEncoder.swift

+19-25
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ internal struct BinaryReverseEncoder {
3131
}
3232

3333
private mutating func prepend<Bytes: SwiftProtobufContiguousBytes>(contentsOf bytes: Bytes) {
34-
bytes.withUnsafeBytes { dataPointer in
35-
if let baseAddress = dataPointer.baseAddress, dataPointer.count > 0 {
36-
consume(dataPointer.count)
37-
pointer.copyMemory(from: baseAddress, byteCount: dataPointer.count)
38-
}
39-
}
34+
bytes.withUnsafeBytes { prepend(contentsOf: $0) }
4035
}
4136

4237
internal var used: Int {
@@ -45,21 +40,19 @@ internal struct BinaryReverseEncoder {
4540

4641
internal var remainder: UnsafeMutableRawBufferPointer {
4742
return UnsafeMutableRawBufferPointer(start: buffer.baseAddress!,
48-
count: buffer.count - used)
43+
count: buffer.count - used)
4944
}
5045

5146
internal mutating func consume(_ bytes: Int) {
5247
pointer = pointer.advanced(by: -bytes)
5348
}
5449

55-
@discardableResult
56-
private mutating func prepend(contentsOf bufferPointer: UnsafeRawBufferPointer) -> Int {
50+
private mutating func prepend(contentsOf bufferPointer: UnsafeRawBufferPointer) {
5751
let count = bufferPointer.count
58-
consume(count)
52+
consume(count)
5953
if let baseAddress = bufferPointer.baseAddress, count > 0 {
6054
pointer.copyMemory(from: baseAddress, byteCount: count)
6155
}
62-
return count
6356
}
6457

6558
mutating func appendUnknown(data: Data) {
@@ -76,11 +69,11 @@ internal struct BinaryReverseEncoder {
7669

7770
mutating func putVarInt(value: UInt64) {
7871
if value > 127 {
79-
putVarInt(value: value >> 7)
72+
putVarInt(value: value >> 7)
8073
prepend(UInt8(value & 0x7f | 0x80))
8174
} else {
8275
prepend(UInt8(value))
83-
}
76+
}
8477
}
8578

8679
mutating func putVarInt(value: Int64) {
@@ -103,28 +96,28 @@ internal struct BinaryReverseEncoder {
10396
mutating func putFixedUInt64(value: UInt64) {
10497
var v = value.littleEndian
10598
let n = MemoryLayout<UInt64>.size
106-
consume(n)
99+
consume(n)
107100
pointer.copyMemory(from: &v, byteCount: n)
108101
}
109102

110103
mutating func putFixedUInt32(value: UInt32) {
111104
var v = value.littleEndian
112105
let n = MemoryLayout<UInt32>.size
113-
consume(n)
106+
consume(n)
114107
pointer.copyMemory(from: &v, byteCount: n)
115108
}
116109

117110
mutating func putFloatValue(value: Float) {
118111
let n = MemoryLayout<Float>.size
119112
var v = value.bitPattern.littleEndian
120-
consume(n)
113+
consume(n)
121114
pointer.copyMemory(from: &v, byteCount: n)
122115
}
123116

124117
mutating func putDoubleValue(value: Double) {
125118
let n = MemoryLayout<Double>.size
126119
var v = value.bitPattern.littleEndian
127-
consume(n)
120+
consume(n)
128121
pointer.copyMemory(from: &v, byteCount: n)
129122
}
130123

@@ -133,19 +126,20 @@ internal struct BinaryReverseEncoder {
133126
let utf8 = value.utf8
134127
// If the String does not support an internal representation in a form
135128
// of contiguous storage, body is not called and nil is returned.
136-
let isAvailable = utf8.withContiguousStorageIfAvailable { (body: UnsafeBufferPointer<UInt8>) -> Int in
137-
let r = prepend(contentsOf: UnsafeRawBufferPointer(body))
129+
let usedContiguousCopy = utf8.withContiguousStorageIfAvailable { (body: UnsafeBufferPointer<UInt8>) -> Bool in
130+
prepend(contentsOf: UnsafeRawBufferPointer(body))
138131
putVarInt(value: body.count)
139-
return r
132+
return true
140133
}
141-
if isAvailable == nil {
142-
precondition(false)
134+
if usedContiguousCopy == nil {
143135
let count = utf8.count
144-
putVarInt(value: count)
136+
consume(count)
137+
var i = 0
145138
for b in utf8 {
146-
pointer.storeBytes(of: b, as: UInt8.self)
147-
consume(1)
139+
pointer[i] = b
140+
i += 1
148141
}
142+
putVarInt(value: count)
149143
}
150144
}
151145

0 commit comments

Comments
 (0)