SwiftLint
This commit is contained in:
parent
1f6b55014a
commit
2b94443b56
9 changed files with 320 additions and 94 deletions
|
@ -1,23 +1,27 @@
|
|||
struct Color: CustomStringConvertible, Equatable {
|
||||
let r, g, b, a: UInt8
|
||||
let red: UInt8
|
||||
let green: UInt8
|
||||
let blue: UInt8
|
||||
let alpha: UInt8
|
||||
|
||||
init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 0xFF) {
|
||||
self.r = r
|
||||
self.g = g
|
||||
self.b = b
|
||||
self.a = a
|
||||
init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8 = 0xFF) {
|
||||
self.red = red
|
||||
self.green = green
|
||||
self.blue = blue
|
||||
self.alpha = alpha
|
||||
}
|
||||
|
||||
init?(_ array: [UInt8]) {
|
||||
guard array.count >= 3 else { return nil }
|
||||
r = array[0]
|
||||
g = array[1]
|
||||
b = array[2]
|
||||
a = array.count >= 4 ? array[3] : 0xFF
|
||||
init(_ array: [UInt8]) {
|
||||
precondition(array.count == 3 || array.count == 4)
|
||||
red = array[0]
|
||||
green = array[1]
|
||||
blue = array[2]
|
||||
alpha = array.count == 4 ? array[3] : 0xFF
|
||||
}
|
||||
|
||||
var description: String {
|
||||
a != 0xFF ? String(format: "#%02X%02X%02X%02X", r, g, b, a) : String(format: "#%02X%02X%02X", r, g, b)
|
||||
let alphaSuffix = alpha != 0xFF ? String(format: "%02X", alpha) : ""
|
||||
return String(format: "#%02X%02X%02X%@", red, green, blue, alphaSuffix)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,11 +53,16 @@ extension Dictionary where Key == String, Value == ColorDef {
|
|||
sorted(by: Self.compare)
|
||||
}
|
||||
|
||||
static func compare(_ a: (String, ColorDef), _ b: (String, ColorDef)) -> Bool {
|
||||
switch (a, b) {
|
||||
case ((_, .color), (_, .reference)): return true
|
||||
case ((_, .reference), (_, .color)): return false
|
||||
case let ((left, _), (right, _)): return left.localizedStandardCompare(right) == .orderedAscending
|
||||
static func compare(_ lhs: (String, ColorDef), _ rhs: (String, ColorDef)) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case ((_, .color), (_, .reference)):
|
||||
return true
|
||||
|
||||
case ((_, .reference), (_, .color)):
|
||||
return false
|
||||
|
||||
case let ((left, _), (right, _)):
|
||||
return left.localizedStandardCompare(right) == .orderedAscending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,6 @@
|
|||
import Foundation
|
||||
|
||||
private extension CharacterSet {
|
||||
static let hex = CharacterSet(charactersIn: "0123456789abcdef")
|
||||
static let name = alphanumerics.union(CharacterSet(charactersIn: "_/"))
|
||||
}
|
||||
|
||||
private extension Collection {
|
||||
func chunks(size: Int) -> UnfoldSequence<Self.SubSequence, Self.Index> {
|
||||
sequence(state: startIndex) { state -> SubSequence? in
|
||||
guard state != endIndex else { return nil }
|
||||
let next = index(state, offsetBy: size, limitedBy: endIndex) ?? endIndex
|
||||
defer { state = next }
|
||||
return self[state..<next]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Scanner {
|
||||
func string(_ s: String) -> Bool {
|
||||
scanString(s) != nil
|
||||
}
|
||||
|
||||
func color() -> Color? {
|
||||
if string("#"), let digits = scanCharacters(from: .hex) {
|
||||
switch digits.count {
|
||||
|
@ -34,7 +14,8 @@ extension Scanner {
|
|||
let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) }
|
||||
return Color(digits)
|
||||
|
||||
default: return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,6 +91,7 @@ extension Scanner {
|
|||
return result
|
||||
}
|
||||
|
||||
// swiftlint:disable:next discouraged_optional_collection
|
||||
func commaSeparated() -> [UInt8]? {
|
||||
var result: [UInt8] = []
|
||||
repeat {
|
||||
|
@ -121,3 +103,25 @@ extension Scanner {
|
|||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private extension Scanner {
|
||||
func string(_ string: String) -> Bool {
|
||||
scanString(string) != nil
|
||||
}
|
||||
}
|
||||
|
||||
private extension CharacterSet {
|
||||
static let hex = CharacterSet(charactersIn: "0123456789abcdef")
|
||||
static let name = alphanumerics.union(CharacterSet(charactersIn: "_/"))
|
||||
}
|
||||
|
||||
private extension Collection {
|
||||
func chunks(size: Int) -> UnfoldSequence<Self.SubSequence, Self.Index> {
|
||||
sequence(state: startIndex) { state -> SubSequence? in
|
||||
guard state != endIndex else { return nil }
|
||||
let next = index(state, offsetBy: size, limitedBy: endIndex) ?? endIndex
|
||||
defer { state = next }
|
||||
return self[state..<next]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue