forked from jpedrosa/sua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexastyle.swift
234 lines (216 loc) · 6.87 KB
/
hexastyle.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
public struct RGBAColor {
public var r: UInt8
public var g: UInt8
public var b: UInt8
public var a: UInt8?
}
public struct Hexastyle {
public var style = 0
public var color: RGBAColor?
public var backgroundColor: RGBAColor?
public static let BOLD = 2
public static let UNDERLINE = 4
public static let ITALIC = 8
public static let STRIKEOUT = 16
func checkStyle(bit: Int) -> Bool {
return (style & bit) > 0
}
public var isBold: Bool { return checkStyle(bit: Hexastyle.BOLD) }
public var isUnderline: Bool { return checkStyle(bit: Hexastyle.UNDERLINE) }
public var isItalic: Bool { return checkStyle(bit: Hexastyle.ITALIC) }
public var isStrikeOut: Bool { return checkStyle(bit: Hexastyle.STRIKEOUT) }
public static func matchHexa(c: UInt8) -> Bool {
return (c >= 97 && c <= 102) || (c >= 65 && c <= 70) ||
(c >= 48 && c <= 57)
}
// The sequence has to end in either = or , (comma).
public static func parseHexaSequence(bytes: [UInt8], startIndex: Int,
maxBytes: Int) throws -> (RGBAColor?, Int) {
var i = startIndex
let hti = HexaUtils.hexaToInt
let c1 = bytes[i]
if matchHexa(c: c1) && i + 3 < maxBytes {
let c2 = bytes[i + 1]
let c3 = bytes[i + 2]
if matchHexa(c: c2) && matchHexa(c: c3) {
i += 3
let c4 = bytes[i]
if c4 == 44 || c4 == 61 { // , =
return (RGBAColor(r: hti(c1: c1, c2: c1)!,
g: hti(c1: c2, c2: c2)!,
b: hti(c1: c3, c2: c3)!,
a: nil), i)
} else if matchHexa(c: c4) {
i += 1
if i < maxBytes {
let c5 = bytes[i]
if c5 == 44 || c5 == 61 { // , =
return (RGBAColor(r: hti(c1: c1, c2: c1)!,
g: hti(c1: c2, c2: c2)!,
b: hti(c1: c3, c2: c3)!,
a: hti(c1: c4, c2: c4)), i)
} else if matchHexa(c: c5) && i + 2 < maxBytes {
i += 1
let c6 = bytes[i]
if matchHexa(c: c6) {
i += 1
let c7 = bytes[i]
if c7 == 44 || c7 == 61 { // , =
return (RGBAColor(r: hti(c1: c1, c2: c2)!,
g: hti(c1: c3, c2: c4)!,
b: hti(c1: c5, c2: c6)!,
a: nil), i)
} else if matchHexa(c: c7) && i + 2 < maxBytes {
i += 1
let c8 = bytes[i]
if matchHexa(c: c8) {
i += 1
let c9 = bytes[i]
if c9 == 44 || c9 == 61 { // , =
return (RGBAColor(r: hti(c1: c1, c2: c2)!,
g: hti(c1: c3, c2: c4)!,
b: hti(c1: c5, c2: c6)!,
a: hti(c1: c7, c2: c8)), i)
}
}
}
}
}
}
}
}
}
return (nil, i)
}
// Starts from %^b#=. It starts past the percent character.
public static func parseHexastyle(bytes: [UInt8], startIndex: Int,
maxBytes: Int) throws -> (Hexastyle?, Int) {
var i = startIndex
var style = 0
func parseColor() throws -> Hexastyle? {
let (maybeColor, advi) = try parseHexaSequence(bytes: bytes,
startIndex: i, maxBytes: maxBytes)
i = advi
if let color = maybeColor {
let c = bytes[i]
if c == 44 { // ,
i += 1
if i < maxBytes {
let (maybeBgColor, advi) = try parseHexaSequence(bytes: bytes,
startIndex: i, maxBytes: maxBytes)
i = advi
if let bgColor = maybeBgColor {
if bytes[i] == 61 { // =
return Hexastyle(style: style, color: color,
backgroundColor: bgColor)
} else { // ,
// Ignore.
}
} else {
i -= 1 // Go back once, because the main while loop already
// increments it.
}
}
} else if c == 61 { // =
return Hexastyle(style: style, color: color, backgroundColor: nil)
} else {
throw HexastyleError.Unreachable
}
}
return nil
}
if bytes[i] == 35 { // #
i += 1
if bytes[i] == 61 { // =
return (Hexastyle(style:0, color: nil, backgroundColor: nil), i)
} else {
return (try parseColor(), i)
}
} else {
STYLE: repeat {
switch bytes[i] {
case 98: // b
if style & Hexastyle.BOLD > 0 {
break STYLE
}
style |= Hexastyle.BOLD
case 117: // u
if style & Hexastyle.UNDERLINE > 0 {
break STYLE
}
style |= Hexastyle.UNDERLINE
case 115: // s
if style & Hexastyle.STRIKEOUT > 0 {
break STYLE
}
style |= Hexastyle.STRIKEOUT
case 105: // i
if style & Hexastyle.ITALIC > 0 {
break STYLE
}
style |= Hexastyle.ITALIC
case 35: // #
i += 1
if i < maxBytes {
if bytes[i] == 61 { // =
return (Hexastyle(style: style, color: nil,
backgroundColor: nil), i)
} else {
return (try parseColor(), i)
}
}
break STYLE
default: break STYLE
}
i += 1
} while i < maxBytes
}
return (nil, i)
}
public static func parseText(string: String) throws
-> [(String, Hexastyle?)] {
let a = string.bytes
return try parseBytes(bytes: a, startIndex: 0, maxBytes: a.count)
}
public static func parseBytes(bytes: [UInt8], startIndex: Int,
maxBytes: Int) throws -> [(String, Hexastyle?)] {
var i = startIndex
var tokenIndex = i
let len = maxBytes
var list = [(String, Hexastyle?)]()
let vlen = len - 2
var hexac: Hexastyle?
func collect(ei: Int) throws {
if let z = String.fromCharCodes(charCodes: bytes, start: tokenIndex,
end: ei) {
list.append((z, hexac))
} else {
throw HexastyleError.Unicode
}
}
while i < vlen {
if bytes[i] == 37 { // %
let (hc, advi) = try parseHexastyle(bytes: bytes, startIndex: i + 1,
maxBytes: len)
if let _ = hc {
if i - 1 >= tokenIndex {
try collect(ei: i - 1)
}
tokenIndex = advi + 1
}
hexac = hc
i = advi
}
i += 1
}
if tokenIndex < len {
try collect(ei: len)
}
return list
}
}
public enum HexastyleError: ErrorProtocol {
case Parse
case Unicode
case Unreachable
}