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 = " <" ;
28+ static const char *EscapeGT = " >" ;
29+ static const char *EscapeAmp = " &" ;
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
539static 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, " < " );
57+ case HtmlLessThanChar :
58+ appendC (buf, EscapeLT );
2559 break ;
26- case ' > ' :
27- appendC (buf, " > " );
60+ case HtmlGreaterThanChar :
61+ appendC (buf, EscapeGT );
2862 break ;
29- case ' & ' :
30- appendC (buf, " & " );
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
5594static 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
65104static 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) {
88128static 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
96137static 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
110148static 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
124159static 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