Skip to content

Commit 81e5803

Browse files
committed
fix: better names for magic hex
1 parent 0a33462 commit 81e5803

File tree

2 files changed

+92
-57
lines changed

2 files changed

+92
-57
lines changed

ios/inputParser/EnrichedHTMLParser.mm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ - (HTMLNode *)getInlineStyleNodes:(NSAttributedString *)text
184184
textNode.range = range;
185185
HTMLNode *currentNode = textNode;
186186

187-
for (NSNumber *sty in _inlineOrder) {
187+
for (NSNumber *inlineStyleType in _inlineOrder) {
188188

189-
id<BaseStyleProtocol> obj = _styles[sty];
190-
Class styleClass = obj.class;
189+
id<BaseStyleProtocol> styleObject = _styles[inlineStyleType];
190+
Class styleClass = styleObject.class;
191191

192192
NSString *key = [styleClass attributeKey];
193-
id v = attrs[key];
193+
id value = attrs[key];
194194

195-
if (!v || ![obj styleCondition:v range:range])
195+
if (!value || ![styleObject styleCondition:value range:range])
196196
continue;
197197

198198
HTMLElement *wrap = [HTMLElement new];
@@ -201,7 +201,7 @@ - (HTMLNode *)getInlineStyleNodes:(NSAttributedString *)text
201201
wrap.tag = tag;
202202
wrap.attributes =
203203
[styleClass respondsToSelector:@selector(getParametersFromValue:)]
204-
? [styleClass getParametersFromValue:v]
204+
? [styleClass getParametersFromValue:value]
205205
: nullptr;
206206
wrap.selfClosing = [styleClass isSelfClosing];
207207
[wrap.children addObject:currentNode];
@@ -212,11 +212,11 @@ - (HTMLNode *)getInlineStyleNodes:(NSAttributedString *)text
212212
}
213213

214214
- (void)createHtmlFromNode:(HTMLNode *)node
215-
into:(NSMutableData *)buf
215+
into:(NSMutableData *)buffer
216216
pretify:(BOOL)pretify {
217217
if ([node isKindOfClass:[HTMLTextNode class]]) {
218218
HTMLTextNode *t = (HTMLTextNode *)node;
219-
appendEscapedRange(buf, t.source, t.range);
219+
appendEscapedRange(buffer, t.source, t.range);
220220
return;
221221
}
222222

@@ -229,21 +229,21 @@ - (void)createHtmlFromNode:(HTMLNode *)node
229229
BOOL addNewLineAfter = pretify && needsNewLineAfter(element.tag);
230230

231231
if (element.selfClosing) {
232-
appendSelfClosingTag(buf, element.tag, element.attributes,
232+
appendSelfClosingTag(buffer, element.tag, element.attributes,
233233
addNewLineBefore);
234234
return;
235235
}
236236

237-
appendOpenTag(buf, element.tag, element.attributes ?: nullptr,
237+
appendOpenTag(buffer, element.tag, element.attributes ?: nullptr,
238238
addNewLineBefore);
239239

240240
for (HTMLNode *child in element.children)
241-
[self createHtmlFromNode:child into:buf pretify:pretify];
241+
[self createHtmlFromNode:child into:buffer pretify:pretify];
242242

243243
if (addNewLineAfter)
244-
appendC(buf, "\n");
244+
appendC(buffer, "\n");
245245

246-
appendCloseTag(buf, element.tag);
246+
appendCloseTag(buffer, element.tag);
247247
}
248248

249249
@end
Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
#pragma once
2-
32
#import <Foundation/Foundation.h>
3+
static const unichar ZeroWidthSpace = 0x200B;
4+
5+
static const int UTF8_1ByteLimit = 0x80;
6+
static const int UTF8_2ByteLimit = 0x800;
7+
8+
static const char UTF8_2ByteLeadMask = 0xC0;
9+
static const char UTF8_3ByteLeadMask = 0xE0;
10+
static const char UTF8_ContinuationMask = 0x80;
11+
static const char UTF8_ContinuationPayloadMask = 0x3F;
12+
13+
static const unichar HtmlLessThanChar = '<';
14+
static const unichar HtmlGreaterThanChar = '>';
15+
static const unichar HtmlAmpersandChar = '&';
16+
17+
static const char *NewlineOpenTag = "\n<";
18+
19+
static const char *OpenTagStart = "<";
20+
static const char *CloseTagStart = "</";
21+
static const char *SelfCloseTagSuffix = "/>";
22+
static const char *TagEnd = ">";
23+
static const char *Space = " ";
24+
static const char *EqualsSign = "=";
25+
static const char *Quote = "\"";
26+
27+
static const char *EscapeLT = "&lt;";
28+
static const char *EscapeGT = "&gt;";
29+
static const char *EscapeAmp = "&amp;";
30+
31+
static const char *HtmlTagUL = "ul";
32+
static const char *HtmlTagOL = "ol";
33+
static const char *HtmlTagLI = "li";
34+
static const char *HtmlTagBR = "br";
35+
static const char *HtmlTagHTML = "html";
36+
static const char *HtmlTagBlockquote = "blockquote";
37+
static const char *HtmlTagCodeblock = "codeblock";
438

539
static inline void appendC(NSMutableData *buf, const char *c) {
640
if (!c)
@@ -16,36 +50,41 @@ static inline void appendEscapedRange(NSMutableData *buf, NSString *src,
1650

1751
for (NSUInteger i = 0; i < len; i++) {
1852
unichar c = tmp[i];
19-
if (c == 0x200B)
53+
if (c == ZeroWidthSpace)
2054
continue;
2155

2256
switch (c) {
23-
case '<':
24-
appendC(buf, "&lt;");
57+
case HtmlLessThanChar:
58+
appendC(buf, EscapeLT);
2559
break;
26-
case '>':
27-
appendC(buf, "&gt;");
60+
case HtmlGreaterThanChar:
61+
appendC(buf, EscapeGT);
2862
break;
29-
case '&':
30-
appendC(buf, "&amp;");
63+
case HtmlAmpersandChar:
64+
appendC(buf, EscapeAmp);
3165
break;
3266

3367
default: {
3468
char out[4];
3569
int n = 0;
36-
if (c < 0x80) {
70+
71+
if (c < UTF8_1ByteLimit) {
3772
out[0] = (char)c;
3873
n = 1;
39-
} else if (c < 0x800) {
40-
out[0] = 0xC0 | (c >> 6);
41-
out[1] = 0x80 | (c & 0x3F);
74+
75+
} else if (c < UTF8_2ByteLimit) {
76+
out[0] = UTF8_2ByteLeadMask | (c >> 6);
77+
out[1] = UTF8_ContinuationMask | (c & UTF8_ContinuationPayloadMask);
4278
n = 2;
79+
4380
} else {
44-
out[0] = 0xE0 | (c >> 12);
45-
out[1] = 0x80 | ((c >> 6) & 0x3F);
46-
out[2] = 0x80 | (c & 0x3F);
81+
out[0] = UTF8_3ByteLeadMask | (c >> 12);
82+
out[1] =
83+
UTF8_ContinuationMask | ((c >> 6) & UTF8_ContinuationPayloadMask);
84+
out[2] = UTF8_ContinuationMask | (c & UTF8_ContinuationPayloadMask);
4785
n = 3;
4886
}
87+
4988
[buf appendBytes:out length:n];
5089
}
5190
}
@@ -54,32 +93,33 @@ static inline void appendEscapedRange(NSMutableData *buf, NSString *src,
5493

5594
static inline void appendKeyVal(NSMutableData *buf, NSString *key,
5695
NSString *val) {
57-
appendC(buf, " ");
58-
const char *k = key.UTF8String;
59-
appendC(buf, k);
60-
appendC(buf, "=\"");
96+
appendC(buf, Space);
97+
appendC(buf, key.UTF8String);
98+
appendC(buf, EqualsSign);
99+
appendC(buf, Quote);
61100
appendEscapedRange(buf, val, NSMakeRange(0, val.length));
62-
appendC(buf, "\"");
101+
appendC(buf, Quote);
63102
}
64103

65104
static inline BOOL isBlockTag(const char *t) {
66105
if (!t)
67106
return NO;
107+
68108
switch (t[0]) {
69109
case 'p':
70-
return t[1] == 0;
110+
return t[1] == '\0';
71111
case 'h':
72-
return (t[2] == 0 && (t[1] == '1' || t[1] == '2' || t[1] == '3'));
112+
return t[2] == '\0' && (t[1] == '1' || t[1] == '2' || t[1] == '3');
73113
case 'u':
74-
return strcmp(t, "ul") == 0;
114+
return strcmp(t, HtmlTagUL) == 0;
75115
case 'o':
76-
return strcmp(t, "ol") == 0;
116+
return strcmp(t, HtmlTagOL) == 0;
77117
case 'l':
78-
return strcmp(t, "li") == 0;
118+
return strcmp(t, HtmlTagLI) == 0;
79119
case 'b':
80-
return strcmp(t, "br") == 0 || strcmp(t, "blockquote") == 0;
120+
return strcmp(t, HtmlTagBR) == 0 || strcmp(t, HtmlTagBlockquote) == 0;
81121
case 'c':
82-
return strcmp(t, "codeblock") == 0;
122+
return strcmp(t, HtmlTagCodeblock) == 0;
83123
default:
84124
return NO;
85125
}
@@ -88,41 +128,36 @@ static inline BOOL isBlockTag(const char *t) {
88128
static inline BOOL needsNewLineAfter(const char *t) {
89129
if (!t)
90130
return NO;
91-
return (strcmp(t, "ul") == 0 || strcmp(t, "ol") == 0 ||
92-
strcmp(t, "blockquote") == 0 || strcmp(t, "codeblock") == 0 ||
93-
strcmp(t, "html") == 0);
131+
132+
return (strcmp(t, HtmlTagUL) == 0 || strcmp(t, HtmlTagOL) == 0 ||
133+
strcmp(t, HtmlTagBlockquote) == 0 ||
134+
strcmp(t, HtmlTagCodeblock) == 0 || strcmp(t, HtmlTagHTML) == 0);
94135
}
95136

96137
static inline void appendOpenTag(NSMutableData *buf, const char *t,
97138
NSDictionary *attrs, BOOL block) {
98-
if (block)
99-
appendC(buf, "\n<");
100-
else
101-
appendC(buf, "<");
102-
139+
appendC(buf, block ? NewlineOpenTag : OpenTagStart);
103140
appendC(buf, t);
141+
104142
for (NSString *key in attrs)
105143
appendKeyVal(buf, key, attrs[key]);
106144

107-
appendC(buf, ">");
145+
appendC(buf, TagEnd);
108146
}
109147

110148
static inline void appendSelfClosingTag(NSMutableData *buf, const char *t,
111149
NSDictionary *attrs, BOOL block) {
112-
if (block)
113-
appendC(buf, "\n<");
114-
else
115-
appendC(buf, "<");
116-
150+
appendC(buf, block ? NewlineOpenTag : OpenTagStart);
117151
appendC(buf, t);
152+
118153
for (NSString *key in attrs)
119154
appendKeyVal(buf, key, attrs[key]);
120155

121-
appendC(buf, "/>");
156+
appendC(buf, SelfCloseTagSuffix);
122157
}
123158

124159
static inline void appendCloseTag(NSMutableData *buf, const char *t) {
125-
appendC(buf, "</");
160+
appendC(buf, CloseTagStart);
126161
appendC(buf, t);
127-
appendC(buf, ">");
162+
appendC(buf, TagEnd);
128163
}

0 commit comments

Comments
 (0)