Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
Accept Data, remove text property
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloZhu committed Oct 21, 2020
1 parent 93785bd commit 3534afe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
7 changes: 0 additions & 7 deletions Sources/QR8bitByte.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ struct QR8bitByte {
let mode: QRMode = .bitByte8
let parsedData: Data

init(_ data: String, encoding: String.Encoding = .utf8) throws {
guard let parsed = data.data(using: encoding) else {
throw QRCodeError.text(data, incompatibleWithEncoding: encoding)
}
self.parsedData = parsed
}

var count: Int {
return parsedData.count
}
Expand Down
34 changes: 26 additions & 8 deletions Sources/QRCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
SOFTWARE.
*/

import Foundation

/// QRCode abstraction and generator.
open class QRCode {
/// Content.
public let text: String
/// Error correct level.
public let correctLevel: QRErrorCorrectLevel
/// If the image codes has a border around its content.
Expand All @@ -39,13 +38,32 @@ open class QRCode {
/// - hasBorder: if the image codes has a border around, defaults and suggests to be true.
/// - Throws: see `QRCodeError`
/// - Warning: Is computationally intensive.
public init(_ text: String,
encoding: String.Encoding = .utf8,
errorCorrectLevel: QRErrorCorrectLevel = .H,
withBorder hasBorder: Bool = true) throws {
self.model = try QRCodeModel(text: text, encoding: encoding,
public convenience init(
_ text: String,
encoding: String.Encoding = .utf8,
errorCorrectLevel: QRErrorCorrectLevel = .H,
withBorder hasBorder: Bool = true
) throws {
guard let data = text.data(using: encoding) else {
throw QRCodeError.text(text, incompatibleWithEncoding: encoding)
}
try self.init(data, errorCorrectLevel: errorCorrectLevel,
withBorder: hasBorder)
}

/// Construct a QRCode instance.
/// - Parameters:
/// - data: raw content of the QRCode.
/// - errorCorrectLevel: error correct level, defaults to high.
/// - hasBorder: if the image codes has a border around, defaults and suggests to be true.
/// - Throws: see `QRCodeError`
public init(
_ data: Data,
errorCorrectLevel: QRErrorCorrectLevel = .H,
withBorder hasBorder: Bool = true
) throws {
self.model = try QRCodeModel(data: data,
errorCorrectLevel: errorCorrectLevel)
self.text = text
self.correctLevel = errorCorrectLevel
self.hasBorder = hasBorder
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/QRCodeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
SOFTWARE.
*/

import Foundation

struct QRCodeModel {
let typeNumber: Int
let errorCorrectLevel: QRErrorCorrectLevel
Expand All @@ -30,12 +32,12 @@ struct QRCodeModel {
private let encodedText: QR8bitByte
private var dataCache: [Int]

init(text: String, encoding: String.Encoding = .utf8,
init(data: Data,
errorCorrectLevel: QRErrorCorrectLevel) throws {
self.encodedText = try QR8bitByte(text, encoding: encoding)
self.encodedText = QR8bitByte(parsedData: data)

self.typeNumber = try QRCodeType
.typeNumber(of: text, encoding: encoding,
.typeNumber(forLength: encodedText.count,
errorCorrectLevel: errorCorrectLevel)
self.errorCorrectLevel = errorCorrectLevel
self.dataCache = try QRCodeModel.createData(
Expand Down
6 changes: 2 additions & 4 deletions Sources/QRCodeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ struct QRCodeType {

extension QRCodeType {
/// Get the type by string length
static func typeNumber(of text: String, encoding: String.Encoding = .utf8,
static func typeNumber(forLength length: Int,
errorCorrectLevel: QRErrorCorrectLevel
) throws -> Int {
let textLength = text.lengthOfBytes(using: encoding)

for i in QRCodeLimitLength.indices {
if textLength <= QRCodeLimitLength[i][errorCorrectLevel.offset] {
if length <= QRCodeLimitLength[i][errorCorrectLevel.offset] {
return i + 1
}
}
Expand Down

0 comments on commit 3534afe

Please sign in to comment.