Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Sources/SwiftProtobuf/BinaryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ internal struct BinaryDecoder: Decoder {
if bodyBytes % itemSize != 0 || itemCount > UInt64(Int.max) {
throw BinaryDecodingError.truncated
}
value.reserveCapacity(value.count + Int(extendingOrTruncating: itemCount))
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
for _ in 1...itemCount {
value.append(try decodeFloat())
}
Expand Down Expand Up @@ -279,7 +279,7 @@ internal struct BinaryDecoder: Decoder {
if bodyBytes % itemSize != 0 || itemCount > UInt64(Int.max) {
throw BinaryDecodingError.truncated
}
value.reserveCapacity(value.count + Int(extendingOrTruncating: itemCount))
value.reserveCapacity(value.count + Int(truncatingIfNeeded: itemCount))
for _ in 1...itemCount {
let i = try decodeDouble()
value.append(i)
Expand All @@ -296,7 +296,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
value = Int32(extendingOrTruncating: varint)
value = Int32(truncatingIfNeeded: varint)
consumed = true
}

Expand All @@ -305,15 +305,15 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
value = Int32(extendingOrTruncating: varint)
value = Int32(truncatingIfNeeded: varint)
consumed = true
}

internal mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
switch fieldWireFormat {
case WireFormat.varint:
let varint = try decodeVarint()
value.append(Int32(extendingOrTruncating: varint))
value.append(Int32(truncatingIfNeeded: varint))
consumed = true
case WireFormat.lengthDelimited:
var n: Int = 0
Expand All @@ -323,7 +323,7 @@ internal struct BinaryDecoder: Decoder {
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
while !decoder.complete {
let varint = try decoder.decodeVarint()
value.append(Int32(extendingOrTruncating: varint))
value.append(Int32(truncatingIfNeeded: varint))
}
consumed = true
default:
Expand Down Expand Up @@ -376,7 +376,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
value = UInt32(extendingOrTruncating: varint)
value = UInt32(truncatingIfNeeded: varint)
consumed = true
}

Expand All @@ -385,15 +385,15 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
value = UInt32(extendingOrTruncating: varint)
value = UInt32(truncatingIfNeeded: varint)
consumed = true
}

internal mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
switch fieldWireFormat {
case WireFormat.varint:
let varint = try decodeVarint()
value.append(UInt32(extendingOrTruncating: varint))
value.append(UInt32(truncatingIfNeeded: varint))
consumed = true
case WireFormat.lengthDelimited:
var n: Int = 0
Expand All @@ -403,7 +403,7 @@ internal struct BinaryDecoder: Decoder {
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
while !decoder.complete {
let t = try decoder.decodeVarint()
value.append(UInt32(extendingOrTruncating: t))
value.append(UInt32(truncatingIfNeeded: t))
}
consumed = true
default:
Expand Down Expand Up @@ -454,7 +454,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
let t = UInt32(extendingOrTruncating: varint)
let t = UInt32(truncatingIfNeeded: varint)
value = ZigZag.decoded(t)
consumed = true
}
Expand All @@ -464,7 +464,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
let t = UInt32(extendingOrTruncating: varint)
let t = UInt32(truncatingIfNeeded: varint)
value = ZigZag.decoded(t)
consumed = true
}
Expand All @@ -473,7 +473,7 @@ internal struct BinaryDecoder: Decoder {
switch fieldWireFormat {
case WireFormat.varint:
let varint = try decodeVarint()
let t = UInt32(extendingOrTruncating: varint)
let t = UInt32(truncatingIfNeeded: varint)
value.append(ZigZag.decoded(t))
consumed = true
case WireFormat.lengthDelimited:
Expand All @@ -484,7 +484,7 @@ internal struct BinaryDecoder: Decoder {
var decoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
while !decoder.complete {
let varint = try decoder.decodeVarint()
let t = UInt32(extendingOrTruncating: varint)
let t = UInt32(truncatingIfNeeded: varint)
value.append(ZigZag.decoded(t))
}
consumed = true
Expand Down Expand Up @@ -824,7 +824,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
value = v
consumed = true
}
Expand All @@ -835,7 +835,7 @@ internal struct BinaryDecoder: Decoder {
return
}
let varint = try decodeVarint()
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
value = v
consumed = true
}
Expand All @@ -845,7 +845,7 @@ internal struct BinaryDecoder: Decoder {
switch fieldWireFormat {
case WireFormat.varint:
let varint = try decodeVarint()
if let v = E(rawValue: Int(Int32(extendingOrTruncating: varint))) {
if let v = E(rawValue: Int(Int32(truncatingIfNeeded: varint))) {
value.append(v)
consumed = true
}
Expand All @@ -858,7 +858,7 @@ internal struct BinaryDecoder: Decoder {
var subdecoder = BinaryDecoder(forReadingFrom: p, count: n, parent: self)
while !subdecoder.complete {
let u64 = try subdecoder.decodeVarint()
let i32 = Int32(extendingOrTruncating: u64)
let i32 = Int32(truncatingIfNeeded: u64)
if let v = E(rawValue: Int(i32)) {
value.append(v)
} else if !options.discardUnknownFields {
Expand Down Expand Up @@ -1417,7 +1417,7 @@ internal struct BinaryDecoder: Decoder {
}
let t = try decodeVarint()
if t < UInt64(UInt32.max) {
guard let tag = FieldTag(rawValue: UInt32(extendingOrTruncating: t)) else {
guard let tag = FieldTag(rawValue: UInt32(truncatingIfNeeded: t)) else {
throw BinaryDecodingError.malformedProtobuf
}
fieldWireFormat = tag.wireFormat
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/BinaryEncodingSizeVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
let tagSize = FieldTag(fieldNumber: fieldNumber,
wireFormat: .varint).encodedSize
serializedSize += tagSize
let dataSize = Varint.encodedSize(of: Int32(extendingOrTruncating: value.rawValue))
let dataSize = Varint.encodedSize(of: Int32(truncatingIfNeeded: value.rawValue))
serializedSize += dataSize
}

Expand All @@ -221,7 +221,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
wireFormat: .varint).encodedSize
serializedSize += value.count * tagSize
for v in value {
let dataSize = Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
let dataSize = Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
serializedSize += dataSize
}
}
Expand All @@ -237,7 +237,7 @@ internal struct BinaryEncodingSizeVisitor: Visitor {
serializedSize += tagSize
var dataSize = 0
for v in value {
dataSize += Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
dataSize += Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
}
serializedSize += Varint.encodedSize(of: Int64(dataSize)) + dataSize
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/BinaryEncodingVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ internal struct BinaryEncodingVisitor: Visitor {
encoder.startField(fieldNumber: fieldNumber, wireFormat: .lengthDelimited)
var packedSize = 0
for v in value {
packedSize += Varint.encodedSize(of: Int32(extendingOrTruncating: v.rawValue))
packedSize += Varint.encodedSize(of: Int32(truncatingIfNeeded: v.rawValue))
}
encoder.putVarInt(value: packedSize)
for v in value {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/FieldTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal struct FieldTag: RawRepresentable {

/// Creates a new tag by composing the given field number and wire format.
init(fieldNumber: Int, wireFormat: WireFormat) {
self.rawValue = UInt32(extendingOrTruncating: fieldNumber) << 3 |
self.rawValue = UInt32(truncatingIfNeeded: fieldNumber) << 3 |
UInt32(wireFormat.rawValue)
}
}
12 changes: 6 additions & 6 deletions Sources/SwiftProtobuf/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal struct JSONDecoder: Decoder {
if n > Int64(Int32.max) || n < Int64(Int32.min) {
throw JSONDecodingError.numberRange
}
value = Int32(extendingOrTruncating: n)
value = Int32(truncatingIfNeeded: n)
}

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

mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
Expand All @@ -155,7 +155,7 @@ internal struct JSONDecoder: Decoder {
if n > Int64(Int32.max) || n < Int64(Int32.min) {
throw JSONDecodingError.numberRange
}
value.append(Int32(extendingOrTruncating: n))
value.append(Int32(truncatingIfNeeded: n))
if scanner.skipOptionalArrayEnd() {
return
}
Expand Down Expand Up @@ -206,7 +206,7 @@ internal struct JSONDecoder: Decoder {
if n > UInt64(UInt32.max) {
throw JSONDecodingError.numberRange
}
value = UInt32(extendingOrTruncating: n)
value = UInt32(truncatingIfNeeded: n)
}

mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
Expand All @@ -218,7 +218,7 @@ internal struct JSONDecoder: Decoder {
if n > UInt64(UInt32.max) {
throw JSONDecodingError.numberRange
}
value = UInt32(extendingOrTruncating: n)
value = UInt32(truncatingIfNeeded: n)
}

mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
Expand All @@ -234,7 +234,7 @@ internal struct JSONDecoder: Decoder {
if n > UInt64(UInt32.max) {
throw JSONDecodingError.numberRange
}
value.append(UInt32(extendingOrTruncating: n))
value.append(UInt32(truncatingIfNeeded: n))
if scanner.skipOptionalArrayEnd() {
return
}
Expand Down
20 changes: 10 additions & 10 deletions Sources/SwiftProtobuf/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,19 @@ internal struct JSONEncoder {
data.append(hexDigits[Int(c.value / 16)])
data.append(hexDigits[Int(c.value & 15)])
case 23...126:
data.append(UInt8(extendingOrTruncating: c.value))
data.append(UInt8(truncatingIfNeeded: c.value))
case 0x80...0x7ff:
data.append(0xc0 + UInt8(extendingOrTruncating: c.value >> 6))
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
data.append(0xc0 + UInt8(truncatingIfNeeded: c.value >> 6))
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
case 0x800...0xffff:
data.append(0xe0 + UInt8(extendingOrTruncating: c.value >> 12))
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 6) & 0x3f))
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
data.append(0xe0 + UInt8(truncatingIfNeeded: c.value >> 12))
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 6) & 0x3f))
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
default:
data.append(0xf0 + UInt8(extendingOrTruncating: c.value >> 18))
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 12) & 0x3f))
data.append(0x80 + UInt8(extendingOrTruncating: (c.value >> 6) & 0x3f))
data.append(0x80 + UInt8(extendingOrTruncating: c.value & 0x3f))
data.append(0xf0 + UInt8(truncatingIfNeeded: c.value >> 18))
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 12) & 0x3f))
data.append(0x80 + UInt8(truncatingIfNeeded: (c.value >> 6) & 0x3f))
data.append(0x80 + UInt8(truncatingIfNeeded: c.value & 0x3f))
}
}
data.append(asciiDoubleQuote)
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftProtobuf/JSONScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ private func parseBytes(
n |= k
chars += 1
if chars == 4 {
p[0] = UInt8(extendingOrTruncating: n >> 16)
p[1] = UInt8(extendingOrTruncating: n >> 8)
p[2] = UInt8(extendingOrTruncating: n)
p[0] = UInt8(truncatingIfNeeded: n >> 16)
p[1] = UInt8(truncatingIfNeeded: n >> 8)
p[2] = UInt8(truncatingIfNeeded: n)
p += 3
chars = 0
n = 0
Expand Down Expand Up @@ -215,13 +215,13 @@ private func parseBytes(
}
switch chars {
case 3:
p[0] = UInt8(extendingOrTruncating: n >> 10)
p[1] = UInt8(extendingOrTruncating: n >> 2)
p[0] = UInt8(truncatingIfNeeded: n >> 10)
p[1] = UInt8(truncatingIfNeeded: n >> 2)
if padding == 1 || padding == 0 {
return
}
case 2:
p[0] = UInt8(extendingOrTruncating: n >> 4)
p[0] = UInt8(truncatingIfNeeded: n >> 4)
if padding == 2 || padding == 0 {
return
}
Expand Down
23 changes: 11 additions & 12 deletions Sources/SwiftProtobuf/MathUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,46 @@ internal func div<T : SignedInteger>(_ a: T, _ b: T) -> T {
#if !swift(>=4.0)
//
// Swift 3 called this initializer "truncatingBitPattern";
// Swift 4 (prerelease) changed this initializer to
// "extendingOrTruncating".
// Swift 4 changed this initializer to "truncatingIfNeeded".
//
extension UInt8 {
internal init(extendingOrTruncating value: UInt32) {
internal init(truncatingIfNeeded value: UInt32) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: Int) {
internal init(truncatingIfNeeded value: Int) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: UInt64) {
internal init(truncatingIfNeeded value: UInt64) {
self.init(truncatingBitPattern: value)
}
}

extension UInt32 {
internal init(extendingOrTruncating value: UInt64) {
internal init(truncatingIfNeeded value: UInt64) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: Int) {
internal init(truncatingIfNeeded value: Int) {
self.init(truncatingBitPattern: value)
}
}

extension Int32 {
internal init(extendingOrTruncating value: UInt64) {
internal init(truncatingIfNeeded value: UInt64) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: Int64) {
internal init(truncatingIfNeeded value: Int64) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: Int) {
internal init(truncatingIfNeeded value: Int) {
self.init(truncatingBitPattern: value)
}
}

extension Int {
internal init(extendingOrTruncating value: Int64) {
internal init(truncatingIfNeeded value: Int64) {
self.init(truncatingBitPattern: value)
}
internal init(extendingOrTruncating value: UInt64) {
internal init(truncatingIfNeeded value: UInt64) {
self.init(truncatingBitPattern: value)
}
}
Expand Down
Loading