Skip to content

Commit

Permalink
Major 7 simple value encoding 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 3c9d17f commit c8eef6a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions CBORSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
179837BF209A22310048DCD2 /* CBOR.swift in Sources */ = {isa = PBXBuildFile; fileRef = 179837BE209A22310048DCD2 /* CBOR.swift */; };
179837C1209A2A490048DCD2 /* Encoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 179837C0209A2A490048DCD2 /* Encoder.swift */; };
179837C3209A502F0048DCD2 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 179837C2209A502F0048DCD2 /* Extensions.swift */; };
5CC7970520AB114A005F45C0 /* MyNSObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CC7970420AB114A005F45C0 /* MyNSObject.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -43,6 +44,7 @@
179837BE209A22310048DCD2 /* CBOR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CBOR.swift; sourceTree = "<group>"; };
179837C0209A2A490048DCD2 /* Encoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Encoder.swift; sourceTree = "<group>"; };
179837C2209A502F0048DCD2 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
5CC7970420AB114A005F45C0 /* MyNSObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyNSObject.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -111,6 +113,7 @@
1747B8BF209C5BA9000663CA /* Decoder.swift */,
179837BB209A1CAD0048DCD2 /* MajorTypes.swift */,
179837C2209A502F0048DCD2 /* Extensions.swift */,
5CC7970420AB114A005F45C0 /* MyNSObject.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -225,6 +228,7 @@
buildActionMask = 2147483647;
files = (
179837BC209A1CAD0048DCD2 /* MajorTypes.swift in Sources */,
5CC7970520AB114A005F45C0 /* MyNSObject.swift in Sources */,
179837BF209A22310048DCD2 /* CBOR.swift in Sources */,
179837C1209A2A490048DCD2 /* Encoder.swift in Sources */,
1747B8C0209C5BA9000663CA /* Decoder.swift in Sources */,
Expand Down
Binary file not shown.
22 changes: 0 additions & 22 deletions CBORSwift/Classes/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,6 @@ extension NSNumber {
}
}

public class NSByteString: NSObject {
private var value: String = ""

public init(_ value: String) {
super.init()
self.value = value
}

@objc override func encode() -> String {
var byteArray = [UInt8]()
for offset in stride(from: 0, to: self.value.count, by: 2) {
let byte = value[offset..<offset+2].hex_decimal
byteArray.append(UInt8(byte))
}
let encodedArray = Encoder.prepareByteArray(major: .major2, measure: byteArray.count)
let headerData = Data(bytes: encodedArray).binary_decimal.hex
let byteData = Data(bytes: byteArray).hex

return headerData.appending(byteData)
}
}

extension NSString {
@objc override func encode() -> String {
let encodedArray = Encoder.prepareByteArray(major: .major3, measure: self.length)
Expand Down
59 changes: 59 additions & 0 deletions CBORSwift/Classes/MyNSObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// MyNSObject.swift
// CBORSwift
//
// Created by Hassan Shahbazi on 2018-05-15.
// Copyright © 2018 Hassan Shahbazi. All rights reserved.
//

import UIKit

public class NSByteString: NSObject {
private var value: String = ""

public init(_ value: String) {
super.init()
self.value = value
}

@objc override func encode() -> String {
var byteArray = [UInt8]()
for offset in stride(from: 0, to: self.value.count, by: 2) {
let byte = value[offset..<offset+2].hex_decimal
byteArray.append(UInt8(byte))
}
let encodedArray = Encoder.prepareByteArray(major: .major2, measure: byteArray.count)
let headerData = Data(bytes: encodedArray).binary_decimal.hex
let byteData = Data(bytes: byteArray).hex

return headerData.appending(byteData)
}
}

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

public init(_ value: NSNumber?) {
super.init()
self.value = value?.boolValue
}

@objc override func encode() -> String {
var byte = NILCode
if value != nil {
byte = (value!) ? TRUECode : FALSECode
}
var encodedArray = Encoder.prepareByteArray(major: .major7, measure: 0)
encodedArray = [UInt8](encodedArray[0..<3])

var byteArray = Data(bytes: [byte]).hex.hex_binary
byteArray = [UInt8](byteArray[3..<byteArray.count])

encodedArray.append(contentsOf: byteArray)
return Data(bytes: encodedArray).binary_decimal.hex
}

}
15 changes: 15 additions & 0 deletions CBORSwiftTests/CBOREncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,19 @@ class CBOREncoderTests: XCTestCase {
XCTAssertEqual(sorted[1].1, "776f726c64")
}

//MARK: Simple Value encoding
func test_2_encodeSimpleBool() {
var encoded = CBOR.encode(NSSimpleValue(false))
XCTAssertNotNil(encoded)
XCTAssertEqual([0xF4], encoded)

encoded = CBOR.encode(NSSimpleValue(true))
XCTAssertNotNil(encoded)
XCTAssertEqual([0xF5], encoded)

encoded = CBOR.encode(NSSimpleValue(nil))
XCTAssertNotNil(encoded)
XCTAssertEqual([0xF6], encoded)
}

}

0 comments on commit c8eef6a

Please sign in to comment.