Skip to content

Commit

Permalink
Major 7 simple value decoding is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Hassan Shahbazi authored and Hassan Shahbazi committed May 15, 2018
1 parent c8eef6a commit d302eda
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
<Test
Identifier = "CBORSwiftTests/test_2_generalDecode()">
</Test>
<Test
Identifier = "CBORSwiftTests/test_9_FinalComprehensiveTest_Decode()">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
Expand Down
9 changes: 8 additions & 1 deletion CBORSwift/Classes/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Decoder: NSObject {
if type == .major5 {
decoded = DecodeMap(header: header)
}
if type == .major7 {
decoded = DecodeSimpleValue(header: header) ?? NSObject()
}
}
return decoded
}
Expand Down Expand Up @@ -106,7 +109,6 @@ extension Decoder {
var offset = 0
get(len: &len, offset: &offset, header: header)


var dict = Dictionary<NSObject, NSObject>()
for _ in 0..<len {
let key = decode()
Expand All @@ -117,6 +119,11 @@ extension Decoder {
return dict as NSDictionary
}

private func DecodeSimpleValue(header: UInt8) -> NSNumber? {
let header = Int(header) % 244
return NSSimpleValue.decode(header: header)
}

private func get(len: inout Int, offset: inout Int, header: Int) {
if header < 24 {
len = header
Expand Down
21 changes: 16 additions & 5 deletions CBORSwift/Classes/MyNSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class NSByteString: NSObject {
}

public class NSSimpleValue: NSObject {
private let FALSECode: UInt8 = 0x14
private let TRUECode: UInt8 = 0x15
private let NILCode: UInt8 = 0x16
private static let FALSECode: UInt8 = 0x14
private static let TRUECode: UInt8 = 0x15
private static let NILCode: UInt8 = 0x16
private var value: Bool?

public init(_ value: NSNumber?) {
Expand All @@ -42,9 +42,9 @@ public class NSSimpleValue: NSObject {
}

@objc override func encode() -> String {
var byte = NILCode
var byte = NSSimpleValue.NILCode
if value != nil {
byte = (value!) ? TRUECode : FALSECode
byte = (value!) ? NSSimpleValue.TRUECode : NSSimpleValue.FALSECode
}
var encodedArray = Encoder.prepareByteArray(major: .major7, measure: 0)
encodedArray = [UInt8](encodedArray[0..<3])
Expand All @@ -56,4 +56,15 @@ public class NSSimpleValue: NSObject {
return Data(bytes: encodedArray).binary_decimal.hex
}

public class func decode(header: Int) -> NSNumber? {
let header = header + Int(0x14)

if header == FALSECode {
return NSNumber(value: false)
}
if header == TRUECode {
return NSNumber(value: true)
}
return nil
}
}
11 changes: 11 additions & 0 deletions CBORSwiftTests/CBORDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,15 @@ class CBORDecoderTests: XCTestCase {
XCTAssertNotNil(decoded)
XCTAssertEqual(["item0": 127, "item1": "string", 5: "number", 18: 98], decoded as? NSDictionary)
}

//MARK: Simple Value encoding
func test_2_decodeSimpleBool() {
var decoded = CBOR.decode([0xF4])
XCTAssertNotNil(decoded)
XCTAssertEqual(false, decoded as? NSNumber)

decoded = CBOR.decode([0xF5])
XCTAssertNotNil(decoded)
XCTAssertEqual(true, decoded as? NSNumber)
}
}

0 comments on commit d302eda

Please sign in to comment.