taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

Data+fromUInt8Array.swift (1014B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-24 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import Foundation
      9 
     10 extension Data {
     11     init(fromUInt8Array inValues: [UInt8]) {
     12         let values = inValues
     13         self.init(bytes: values, count: values.count)
     14     }
     15 
     16     struct HexEncodingOptions: OptionSet {
     17         let rawValue: Int
     18         static let upperCase = HexEncodingOptions(rawValue: 1 << 0)
     19     }
     20 
     21     func hexEncodedString(options: HexEncodingOptions = []) -> String {
     22         let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
     23         return self.map { String(format: format, $0) }.joined()
     24     }
     25 
     26     mutating func append(data: Data, offset: Int, size: Int) {
     27         let start = data.startIndex + offset
     28         let end = start + size
     29         self.append(data[start..<end])
     30     }
     31 
     32     init(fromData data: Data, offset: Int, size: Int) {
     33         let start = data.startIndex + offset
     34         let end = start + size
     35         self.init(data[start..<end])
     36     }
     37 }