@@ -31,12 +31,7 @@ internal struct BinaryReverseEncoder {
31
31
}
32
32
33
33
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) }
40
35
}
41
36
42
37
internal var used : Int {
@@ -45,21 +40,19 @@ internal struct BinaryReverseEncoder {
45
40
46
41
internal var remainder : UnsafeMutableRawBufferPointer {
47
42
return UnsafeMutableRawBufferPointer ( start: buffer. baseAddress!,
48
- count: buffer. count - used)
43
+ count: buffer. count - used)
49
44
}
50
45
51
46
internal mutating func consume( _ bytes: Int ) {
52
47
pointer = pointer. advanced ( by: - bytes)
53
48
}
54
49
55
- @discardableResult
56
- private mutating func prepend( contentsOf bufferPointer: UnsafeRawBufferPointer ) -> Int {
50
+ private mutating func prepend( contentsOf bufferPointer: UnsafeRawBufferPointer ) {
57
51
let count = bufferPointer. count
58
- consume ( count)
52
+ consume ( count)
59
53
if let baseAddress = bufferPointer. baseAddress, count > 0 {
60
54
pointer. copyMemory ( from: baseAddress, byteCount: count)
61
55
}
62
- return count
63
56
}
64
57
65
58
mutating func appendUnknown( data: Data ) {
@@ -76,11 +69,11 @@ internal struct BinaryReverseEncoder {
76
69
77
70
mutating func putVarInt( value: UInt64 ) {
78
71
if value > 127 {
79
- putVarInt ( value: value >> 7 )
72
+ putVarInt ( value: value >> 7 )
80
73
prepend ( UInt8 ( value & 0x7f | 0x80 ) )
81
74
} else {
82
75
prepend ( UInt8 ( value) )
83
- }
76
+ }
84
77
}
85
78
86
79
mutating func putVarInt( value: Int64 ) {
@@ -103,28 +96,28 @@ internal struct BinaryReverseEncoder {
103
96
mutating func putFixedUInt64( value: UInt64 ) {
104
97
var v = value. littleEndian
105
98
let n = MemoryLayout< UInt64> . size
106
- consume ( n)
99
+ consume ( n)
107
100
pointer. copyMemory ( from: & v, byteCount: n)
108
101
}
109
102
110
103
mutating func putFixedUInt32( value: UInt32 ) {
111
104
var v = value. littleEndian
112
105
let n = MemoryLayout< UInt32> . size
113
- consume ( n)
106
+ consume ( n)
114
107
pointer. copyMemory ( from: & v, byteCount: n)
115
108
}
116
109
117
110
mutating func putFloatValue( value: Float ) {
118
111
let n = MemoryLayout< Float> . size
119
112
var v = value. bitPattern. littleEndian
120
- consume ( n)
113
+ consume ( n)
121
114
pointer. copyMemory ( from: & v, byteCount: n)
122
115
}
123
116
124
117
mutating func putDoubleValue( value: Double ) {
125
118
let n = MemoryLayout< Double> . size
126
119
var v = value. bitPattern. littleEndian
127
- consume ( n)
120
+ consume ( n)
128
121
pointer. copyMemory ( from: & v, byteCount: n)
129
122
}
130
123
@@ -133,19 +126,20 @@ internal struct BinaryReverseEncoder {
133
126
let utf8 = value. utf8
134
127
// If the String does not support an internal representation in a form
135
128
// 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) )
138
131
putVarInt ( value: body. count)
139
- return r
132
+ return true
140
133
}
141
- if isAvailable == nil {
142
- precondition ( false )
134
+ if usedContiguousCopy == nil {
143
135
let count = utf8. count
144
- putVarInt ( value: count)
136
+ consume ( count)
137
+ var i = 0
145
138
for b in utf8 {
146
- pointer. storeBytes ( of : b , as : UInt8 . self )
147
- consume ( 1 )
139
+ pointer [ i ] = b
140
+ i += 1
148
141
}
142
+ putVarInt ( value: count)
149
143
}
150
144
}
151
145
0 commit comments