Skip to content

Commit 5a77a1a

Browse files
authored
Merge pull request #663 from tbkka/swift-4-truncating-init
Change `extendingOrTruncating` to `truncatingIfNeeded`
2 parents 5620b39 + afcf6ad commit 5a77a1a

File tree

10 files changed

+75
-76
lines changed

10 files changed

+75
-76
lines changed

Sources/SwiftProtobuf/BinaryDecoder.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ internal struct BinaryDecoder: Decoder {
238238
if bodyBytes % itemSize != 0 || itemCount > UInt64(Int.max) {
239239
throw BinaryDecodingError.truncated
240240
}
241-
value.reserveCapacity(value.count + Int(extendingOrTruncating: itemCount))
241+
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
242242
for _ in 1...itemCount {
243243
value.append(try decodeFloat())
244244
}
@@ -279,7 +279,7 @@ internal struct BinaryDecoder: Decoder {
279279
if bodyBytes % itemSize != 0 || itemCount > UInt64(Int.max) {
280280
throw BinaryDecodingError.truncated
281281
}
282-
value.reserveCapacity(value.count + Int(extendingOrTruncating: itemCount))
282+
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
283283
for _ in 1...itemCount {
284284
let i = try decodeDouble()
285285
value.append(i)
@@ -296,7 +296,7 @@ internal struct BinaryDecoder: Decoder {
296296
return
297297
}
298298
let varint = try decodeVarint()
299-
value = Int32(extendingOrTruncating: varint)
299+
value = Int32(truncatingIfNeeded: varint)
300300
consumed = true
301301
}
302302

@@ -305,15 +305,15 @@ internal struct BinaryDecoder: Decoder {
305305
return
306306
}
307307
let varint = try decodeVarint()
308-
value = Int32(extendingOrTruncating: varint)
308+
value = Int32(truncatingIfNeeded: varint)
309309
consumed = true
310310
}
311311

312312
internal mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
313313
switch fieldWireFormat {
314314
case WireFormat.varint:
315315
let varint = try decodeVarint()
316-
value.append(Int32(extendingOrTruncating: varint))
316+
value.append(Int32(truncatingIfNeeded: varint))
317317
consumed = true
318318
case WireFormat.lengthDelimited:
319319
var n: Int = 0
@@ -323,7 +323,7 @@ internal struct BinaryDecoder: Decoder {
323323
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
324324
while !decoder.complete {
325325
let varint = try decoder.decodeVarint()
326-
value.append(Int32(extendingOrTruncating: varint))
326+
value.append(Int32(truncatingIfNeeded: varint))
327327
}
328328
consumed = true
329329
default:
@@ -376,7 +376,7 @@ internal struct BinaryDecoder: Decoder {
376376
return
377377
}
378378
let varint = try decodeVarint()
379-
value = UInt32(extendingOrTruncating: varint)
379+
value = UInt32(truncatingIfNeeded: varint)
380380
consumed = true
381381
}
382382

@@ -385,15 +385,15 @@ internal struct BinaryDecoder: Decoder {
385385
return
386386
}
387387
let varint = try decodeVarint()
388-
value = UInt32(extendingOrTruncating: varint)
388+
value = UInt32(truncatingIfNeeded: varint)
389389
consumed = true
390390
}
391391

392392
internal mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
393393
switch fieldWireFormat {
394394
case WireFormat.varint:
395395
let varint = try decodeVarint()
396-
value.append(UInt32(extendingOrTruncating: varint))
396+
value.append(UInt32(truncatingIfNeeded: varint))
397397
consumed = true
398398
case WireFormat.lengthDelimited:
399399
var n: Int = 0
@@ -403,7 +403,7 @@ internal struct BinaryDecoder: Decoder {
403403
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
404404
while !decoder.complete {
405405
let t = try decoder.decodeVarint()
406-
value.append(UInt32(extendingOrTruncating: t))
406+
value.append(UInt32(truncatingIfNeeded: t))
407407
}
408408
consumed = true
409409
default:
@@ -454,7 +454,7 @@ internal struct BinaryDecoder: Decoder {
454454
return
455455
}
456456
let varint = try decodeVarint()
457-
let t = UInt32(extendingOrTruncating: varint)
457+
let t = UInt32(truncatingIfNeeded: varint)
458458
value = ZigZag.decoded(t)
459459
consumed = true
460460
}
@@ -464,7 +464,7 @@ internal struct BinaryDecoder: Decoder {
464464
return
465465
}
466466
let varint = try decodeVarint()
467-
let t = UInt32(extendingOrTruncating: varint)
467+
let t = UInt32(truncatingIfNeeded: varint)
468468
value = ZigZag.decoded(t)
469469
consumed = true
470470
}
@@ -473,7 +473,7 @@ internal struct BinaryDecoder: Decoder {
473473
switch fieldWireFormat {
474474
case WireFormat.varint:
475475
let varint = try decodeVarint()
476-
let t = UInt32(extendingOrTruncating: varint)
476+
let t = UInt32(truncatingIfNeeded: varint)
477477
value.append(ZigZag.decoded(t))
478478
consumed = true
479479
case WireFormat.lengthDelimited:
@@ -484,7 +484,7 @@ internal struct BinaryDecoder: Decoder {
484484
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
485485
while !decoder.complete {
486486
let varint = try decoder.decodeVarint()
487-
let t = UInt32(extendingOrTruncating: varint)
487+
let t = UInt32(truncatingIfNeeded: varint)
488488
value.append(ZigZag.decoded(t))
489489
}
490490
consumed = true
@@ -824,7 +824,7 @@ internal struct BinaryDecoder: Decoder {
824824
return
825825
}
826826
let varint = try decodeVarint()
827-
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
827+
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
828828
value = v
829829
consumed = true
830830
}
@@ -835,7 +835,7 @@ internal struct BinaryDecoder: Decoder {
835835
return
836836
}
837837
let varint = try decodeVarint()
838-
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
838+
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
839839
value = v
840840
consumed = true
841841
}
@@ -845,7 +845,7 @@ internal struct BinaryDecoder: Decoder {
845845
switch fieldWireFormat {
846846
case WireFormat.varint:
847847
let varint = try decodeVarint()
848-
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
848+
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
849849
value.append(v)
850850
consumed = true
851851
}
@@ -858,7 +858,7 @@ internal struct BinaryDecoder: Decoder {
858858
var subdecoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
859859
while !subdecoder.complete {
860860
let u64 = try subdecoder.decodeVarint()
861-
let i32 = Int32(extendingOrTruncating: u64)
861+
let i32 = Int32(truncatingIfNeeded: u64)
862862
if let v = E(rawValue: Int(i32)) {
863863
value.append(v)
864864
} else if !options.discardUnknownFields {
@@ -1417,7 +1417,7 @@ internal struct BinaryDecoder: Decoder {
14171417
}
14181418
let t = try decodeVarint()
14191419
if t < UInt64(UInt32.max) {
1420-
guard let tag = FieldTag(rawValue: UInt32(extendingOrTruncating: t)) else {
1420+
guard let tag = FieldTag(rawValue: UInt32(truncatingIfNeeded: t)) else {
14211421
throw BinaryDecodingError.malformedProtobuf
14221422
}
14231423
fieldWireFormat = tag.wireFormat

Sources/SwiftProtobuf/BinaryEncodingSizeVisitor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
211211
let tagSize = FieldTag(fieldNumber: fieldNumber,
212212
wireFormat: .varint).encodedSize
213213
serializedSize += tagSize
214-
let dataSize = Varint.encodedSize(of: Int32(extendingOrTruncating: value.rawValue))
214+
let dataSize = Varint.encodedSize(of: Int32(truncatingIfNeeded: value.rawValue))
215215
serializedSize += dataSize
216216
}
217217

@@ -221,7 +221,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
221221
wireFormat: .varint).encodedSize
222222
serializedSize += value.count * tagSize
223223
for v in value {
224-
let dataSize = Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
224+
let dataSize = Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
225225
serializedSize += dataSize
226226
}
227227
}
@@ -237,7 +237,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
237237
serializedSize += tagSize
238238
var dataSize = 0
239239
for v in value {
240-
dataSize += Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
240+
dataSize += Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
241241
}
242242
serializedSize += Varint.encodedSize(of: Int64(dataSize)) + dataSize
243243
}

Sources/SwiftProtobuf/BinaryEncodingVisitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ internal struct BinaryEncodingVisitor: Visitor {
254254
encoder.startField(fieldNumber: fieldNumber, wireFormat: .lengthDelimited)
255255
var packedSize = 0
256256
for v in value {
257-
packedSize += Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
257+
packedSize += Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
258258
}
259259
encoder.putVarInt(value: packedSize)
260260
for v in value {

Sources/SwiftProtobuf/FieldTag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal struct FieldTag: RawRepresentable {
6363

6464
/// Creates a new tag by composing the given field number and wire format.
6565
init(fieldNumber: Int, wireFormat: WireFormat) {
66-
self.rawValue = UInt32(extendingOrTruncating: fieldNumber) << 3 |
66+
self.rawValue = UInt32(truncatingIfNeeded: fieldNumber) << 3 |
6767
UInt32(wireFormat.rawValue)
6868
}
6969
}

Sources/SwiftProtobuf/JSONDecoder.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal struct JSONDecoder: Decoder {
127127
if n > Int64(Int32.max) || n < Int64(Int32.min) {
128128
throw JSONDecodingError.numberRange
129129
}
130-
value = Int32(extendingOrTruncating: n)
130+
value = Int32(truncatingIfNeeded: n)
131131
}
132132

133133
mutating func decodeSingularInt32Field(value: inout Int32?) throws {
@@ -139,7 +139,7 @@ internal struct JSONDecoder: Decoder {
139139
if n > Int64(Int32.max) || n < Int64(Int32.min) {
140140
throw JSONDecodingError.numberRange
141141
}
142-
value = Int32(extendingOrTruncating: n)
142+
value = Int32(truncatingIfNeeded: n)
143143
}
144144

145145
mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
@@ -155,7 +155,7 @@ internal struct JSONDecoder: Decoder {
155155
if n > Int64(Int32.max) || n < Int64(Int32.min) {
156156
throw JSONDecodingError.numberRange
157157
}
158-
value.append(Int32(extendingOrTruncating: n))
158+
value.append(Int32(truncatingIfNeeded: n))
159159
if scanner.skipOptionalArrayEnd() {
160160
return
161161
}
@@ -206,7 +206,7 @@ internal struct JSONDecoder: Decoder {
206206
if n > UInt64(UInt32.max) {
207207
throw JSONDecodingError.numberRange
208208
}
209-
value = UInt32(extendingOrTruncating: n)
209+
value = UInt32(truncatingIfNeeded: n)
210210
}
211211

212212
mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
@@ -218,7 +218,7 @@ internal struct JSONDecoder: Decoder {
218218
if n > UInt64(UInt32.max) {
219219
throw JSONDecodingError.numberRange
220220
}
221-
value = UInt32(extendingOrTruncating: n)
221+
value = UInt32(truncatingIfNeeded: n)
222222
}
223223

224224
mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
@@ -234,7 +234,7 @@ internal struct JSONDecoder: Decoder {
234234
if n > UInt64(UInt32.max) {
235235
throw JSONDecodingError.numberRange
236236
}
237-
value.append(UInt32(extendingOrTruncating: n))
237+
value.append(UInt32(truncatingIfNeeded: n))
238238
if scanner.skipOptionalArrayEnd() {
239239
return
240240
}

Sources/SwiftProtobuf/JSONEncoder.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,19 @@ internal struct JSONEncoder {
311311
data.append(hexDigits[Int(c.value / 16)])
312312
data.append(hexDigits[Int(c.value & 15)])
313313
case 23...126:
314-
data.append(UInt8(extendingOrTruncating: c.value))
314+
data.append(UInt8(truncatingIfNeeded: c.value))
315315
case 0x80...0x7ff:
316-
data.append(0xc0 + UInt8(extendingOrTruncating: c.value >> 6))
317-
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
316+
data.append(0xc0 + UInt8(truncatingIfNeeded: c.value >> 6))
317+
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
318318
case 0x800...0xffff:
319-
data.append(0xe0 + UInt8(extendingOrTruncating: c.value >> 12))
320-
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 6) & 0x3f))
321-
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
319+
data.append(0xe0 + UInt8(truncatingIfNeeded: c.value >> 12))
320+
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 6) & 0x3f))
321+
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
322322
default:
323-
data.append(0xf0 + UInt8(extendingOrTruncating: c.value >> 18))
324-
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 12) & 0x3f))
325-
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 6) & 0x3f))
326-
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
323+
data.append(0xf0 + UInt8(truncatingIfNeeded: c.value >> 18))
324+
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 12) & 0x3f))
325+
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 6) & 0x3f))
326+
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
327327
}
328328
}
329329
data.append(asciiDoubleQuote)

Sources/SwiftProtobuf/JSONScanner.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ private func parseBytes(
178178
n |= k
179179
chars += 1
180180
if chars == 4 {
181-
p[0] = UInt8(extendingOrTruncating: n >> 16)
182-
p[1] = UInt8(extendingOrTruncating: n >> 8)
183-
p[2] = UInt8(extendingOrTruncating: n)
181+
p[0] = UInt8(truncatingIfNeeded: n >> 16)
182+
p[1] = UInt8(truncatingIfNeeded: n >> 8)
183+
p[2] = UInt8(truncatingIfNeeded: n)
184184
p += 3
185185
chars = 0
186186
n = 0
@@ -215,13 +215,13 @@ private func parseBytes(
215215
}
216216
switch chars {
217217
case 3:
218-
p[0] = UInt8(extendingOrTruncating: n >> 10)
219-
p[1] = UInt8(extendingOrTruncating: n >> 2)
218+
p[0] = UInt8(truncatingIfNeeded: n >> 10)
219+
p[1] = UInt8(truncatingIfNeeded: n >> 2)
220220
if padding == 1 || padding == 0 {
221221
return
222222
}
223223
case 2:
224-
p[0] = UInt8(extendingOrTruncating: n >> 4)
224+
p[0] = UInt8(truncatingIfNeeded: n >> 4)
225225
if padding == 2 || padding == 0 {
226226
return
227227
}

Sources/SwiftProtobuf/MathUtils.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,46 @@ internal func div<T : SignedInteger>(_ a: T, _ b: T) -> T {
4242
#if !swift(>=4.0)
4343
//
4444
// Swift 3 called this initializer "truncatingBitPattern";
45-
// Swift 4 (prerelease) changed this initializer to
46-
// "extendingOrTruncating".
45+
// Swift 4 changed this initializer to "truncatingIfNeeded".
4746
//
4847
extension UInt8 {
49-
internal init(extendingOrTruncating value: UInt32) {
48+
internal init(truncatingIfNeeded value: UInt32) {
5049
self.init(truncatingBitPattern: value)
5150
}
52-
internal init(extendingOrTruncating value: Int) {
51+
internal init(truncatingIfNeeded value: Int) {
5352
self.init(truncatingBitPattern: value)
5453
}
55-
internal init(extendingOrTruncating value: UInt64) {
54+
internal init(truncatingIfNeeded value: UInt64) {
5655
self.init(truncatingBitPattern: value)
5756
}
5857
}
5958

6059
extension UInt32 {
61-
internal init(extendingOrTruncating value: UInt64) {
60+
internal init(truncatingIfNeeded value: UInt64) {
6261
self.init(truncatingBitPattern: value)
6362
}
64-
internal init(extendingOrTruncating value: Int) {
63+
internal init(truncatingIfNeeded value: Int) {
6564
self.init(truncatingBitPattern: value)
6665
}
6766
}
6867

6968
extension Int32 {
70-
internal init(extendingOrTruncating value: UInt64) {
69+
internal init(truncatingIfNeeded value: UInt64) {
7170
self.init(truncatingBitPattern: value)
7271
}
73-
internal init(extendingOrTruncating value: Int64) {
72+
internal init(truncatingIfNeeded value: Int64) {
7473
self.init(truncatingBitPattern: value)
7574
}
76-
internal init(extendingOrTruncating value: Int) {
75+
internal init(truncatingIfNeeded value: Int) {
7776
self.init(truncatingBitPattern: value)
7877
}
7978
}
8079

8180
extension Int {
82-
internal init(extendingOrTruncating value: Int64) {
81+
internal init(truncatingIfNeeded value: Int64) {
8382
self.init(truncatingBitPattern: value)
8483
}
85-
internal init(extendingOrTruncating value: UInt64) {
84+
internal init(truncatingIfNeeded value: UInt64) {
8685
self.init(truncatingBitPattern: value)
8786
}
8887
}

0 commit comments

Comments
 (0)