Skip to content

Commit 7196507

Browse files
Merge pull request #18 from SomeRandomiOSDev/SlicedData
Updated CBORParser to use relative data indices
2 parents 20a8632 + b12f91e commit 7196507

File tree

5 files changed

+1055
-898
lines changed

5 files changed

+1055
-898
lines changed

CBORCoding.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "CBORCoding"
4-
s.version = "1.3.0"
4+
s.version = "1.3.1"
55
s.summary = "A CBOR Encoder and Decoder"
66
s.description = <<-DESC
77
A lightweight framework containing a coder pair for encoding and decoding `Codable` conforming types to and from CBOR document format for iOS, macOS, tvOS, and watchOS.

Sources/CBORCoding/CBOR.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ public struct CBOR {
139139
chunks.reserveCapacity(numberOfChunks)
140140

141141
for i in 0 ..< numberOfChunks {
142-
let range = ((i * chunkSize) ..< ((i + 1) * chunkSize)).clamped(to: 0 ..< numberOfBytes)
142+
let lowerBound = data.index(data.startIndex, offsetBy: i * chunkSize)
143+
let upperBound = data.index(data.startIndex, offsetBy: (i + 1) * chunkSize, limitedBy: data.endIndex) ?? data.endIndex
143144

144-
chunks.append(Data(data[range]))
145+
chunks.append(Data(data[lowerBound ..< upperBound]))
145146
}
146147

147148
self.chunks = chunks
@@ -184,9 +185,10 @@ public struct CBOR {
184185
chunks.reserveCapacity(numberOfChunks)
185186

186187
for i in 0 ..< numberOfChunks {
187-
let range = ((i * chunkSize) ..< ((i + 1) * chunkSize)).clamped(to: 0 ..< numberOfBytes)
188+
let lowerBound = data.index(data.startIndex, offsetBy: i * chunkSize)
189+
let upperBound = data.index(data.startIndex, offsetBy: (i + 1) * chunkSize, limitedBy: data.endIndex) ?? data.endIndex
188190

189-
chunks.append(Data(data[range]))
191+
chunks.append(Data(data[lowerBound ..< upperBound]))
190192
}
191193

192194
self.chunks = chunks
@@ -291,9 +293,9 @@ extension CBOR {
291293

292294
init?(bits: Data) {
293295
guard !bits.isEmpty else { return nil }
294-
guard CBOR.majorType(for: bits[0]) == .tag else { return nil }
296+
guard CBOR.majorType(for: bits[bits.startIndex]) == .tag else { return nil }
295297

296-
let additonalInfo = CBOR.additionalInfo(for: bits[0])
298+
let additonalInfo = CBOR.additionalInfo(for: bits[bits.startIndex])
297299
switch additonalInfo {
298300
case 0: self = .standardDateTime
299301
case 1: self = .epochDateTime
@@ -307,7 +309,7 @@ extension CBOR {
307309
case 24:
308310
guard bits.count >= 2 else { return nil }
309311

310-
switch bits[1] {
312+
switch bits[bits.index(after: bits.startIndex)] {
311313
case 24: self = .encodedCBORData
312314
case 32: self = .uri
313315
case 33: self = .base64URL
@@ -320,7 +322,7 @@ extension CBOR {
320322
case 25:
321323
guard bits.count >= 3 else { return nil }
322324

323-
switch (bits[1], bits[2]) {
325+
switch (bits[bits.index(bits.startIndex, offsetBy: 1)], bits[bits.index(bits.startIndex, offsetBy: 2)]) {
324326
case (0xD9, 0xF7): self = .selfDescribedCBOR
325327
default: return nil
326328
}
@@ -387,12 +389,12 @@ extension CBOR {
387389
internal var stringValue: String
388390
internal var intValue: Int?
389391

390-
internal init?(stringValue: String) {
392+
internal init(stringValue: String) {
391393
self.stringValue = stringValue
392394
self.intValue = nil
393395
}
394396

395-
internal init?(intValue: Int) {
397+
internal init(intValue: Int) {
396398
self.stringValue = "\(intValue)"
397399
self.intValue = intValue
398400
}
@@ -406,9 +408,7 @@ extension CBOR {
406408

407409
// MARK: Constants
408410

409-
// swiftlint:disable force_unwrapping
410-
internal static let `super` = CodingKey(stringValue: "super")!
411-
// swiftlint:enable force_unwrapping
411+
internal static let `super` = CodingKey(stringValue: "super")
412412
}
413413

414414
// MARK: Internal Methods
@@ -459,16 +459,16 @@ extension DecodingError {
459459
let expectedTypes: String
460460
if expected.count > 2 {
461461
var types = expected.map { "\($0)" }
462-
types[types.endIndex - 1] = "or " + types[types.endIndex - 1]
462+
types[types.index(before: types.endIndex)] = "or " + types[types.index(before: types.endIndex)]
463463

464464
expectedTypes = types.joined(separator: ", ")
465465
} else if expected.count > 1 {
466466
expectedTypes = expected.map({ "\($0)" }).joined(separator: " or ")
467467
} else {
468-
expectedTypes = "\(expected[0])"
468+
expectedTypes = "\(expected[expected.startIndex])"
469469
}
470470

471-
self = .typeMismatch(expected[0], Context(codingPath: path, debugDescription: "Expected to decode \(expectedTypes) but found \(actual) instead."))
471+
self = .typeMismatch(expected[expected.startIndex], Context(codingPath: path, debugDescription: "Expected to decode \(expectedTypes) but found \(actual) instead."))
472472

473473
case let .dataCorrupted(description):
474474
self = .dataCorrupted(Context(codingPath: path, debugDescription: description))

Sources/CBORCoding/CBOREncoder.swift

+3-9
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,7 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer
589589

590590
do {
591591
for (key, value) in dictionary {
592-
// swiftlint:disable force_unwrapping
593-
let codingKey = CBOR.CodingKey(stringValue: key)!
594-
// swiftlint:enable force_unwrapping
592+
let codingKey = CBOR.CodingKey(stringValue: key)
595593

596594
codingPath.append(codingKey)
597595
defer { codingPath.removeLast() }
@@ -616,9 +614,7 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer
616614

617615
do {
618616
for (key, value) in dictionary {
619-
// swiftlint:disable force_unwrapping
620-
let codingKey = CBOR.CodingKey(intValue: key)!
621-
// swiftlint:enable force_unwrapping
617+
let codingKey = CBOR.CodingKey(intValue: key)
622618

623619
codingPath.append(codingKey)
624620
defer { codingPath.removeLast() }
@@ -1120,9 +1116,7 @@ private class __CBORReferencingEncoder: __CBOREncoder {
11201116
array.insert(value, at: index)
11211117

11221118
case let .dictionary(dictionary, key):
1123-
// swiftlint:disable force_unwrapping
1124-
dictionary[CBOR.CodingKey(stringValue: key)!] = value
1125-
// swiftlint:enable force_unwrapping
1119+
dictionary[CBOR.CodingKey(stringValue: key)] = value
11261120
}
11271121
}
11281122

0 commit comments

Comments
 (0)