hassan-shahbazi / CBORSwift

Swift implementation for CBOR
http://cbor.io/impls.html#swift
MIT License
10 stars 1 forks source link

Supporting Unicode languages #8

Open JafarH96 opened 2 years ago

JafarH96 commented 2 years ago

CBOR Swift does not support Unicode languages and to solve this problem you should implement as follows:

In "Extensions" :

public var ascii_bytes: [UInt8] {
        // To supporting unicode languages it should return with .utf8 encoding
        return self.data(using: .ascii)?.bytes ?? self.data(using: .utf8)!.bytes
    }

In "Encoder":

@objc override func encode() -> String {

        // To support Unicode languages, the string encoding must be specified before giving the string length
        let asciiBytes = self.ascii_bytes
        let encodedArray = Encoder.prepareByteArray(major: .major3, measure: asciiBytes.count)
        let headerData  = Data(bytes: encodedArray).binary_decimal.hex
        let strData     = Data(bytes: asciiBytes).hexString

        return headerData.appending(strData)
    }
hassan-shahbazi commented 2 years ago

Thanks for the report. Can you apply changes and make a PR for it?