blob: e691d21781247b8681be5e0858b9e8b34e8130fd (
plain)
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
|
#ifndef HTML_TOKEN_HPP
#define HTML_TOKEN_HPP
#include <string>
#include "code_point.h"
enum HtmlTokenType {
HtmlTokenType_None = 0,
HtmlTokenType_StartTag,
HtmlTokenType_EndTag,
HtmlTokenType_Attribute,
HtmlTokenType_EOF,
HtmlTokenType_Character,
HtmlTokenType_Length
};
struct HtmlToken {
HtmlTokenType type;
// TODO: @Performance
char character_token;
std::wstring tag_name;
std::wstring code_entity;
void append_to_tag_name(code_point_t c) {
tag_name += c;
}
void append_to_code_entity(code_point_t c) {
code_entity += c;
}
/// Transforms the code_entity into a usable value.
/// Note that we are assuming that the code_entity is
/// valid at this point in time.
code_point_t code_entity_to_value(bool is_hex) {
code_point_t value = 0x0000;
if (is_hex) {
int multiplier = 1;
for (size_t i = code_entity.size() - 1; i > 0; i--) {
auto c = code_entity[i];
if (c >= CodePoints::LOWERCASE_A) { // [a, z]
c = 10 + c - CodePoints::LOWERCASE_A;
}
else if (c >= CodePoints::UPPERCASE_A) { // [A, Z]
c = 10 + c - CodePoints::UPPERCASE_A;
}
else { // [0, 9]
c = c - CodePoints::DIGIT_ZERO; // Now it is between 0 and 9
}
// Now we have c in decimal, let's convert it to the final value.
c = c * multiplier;
value += c;
multiplier *= 16;
}
}
else {
int multiplier = 1;
for (int i = code_entity.size() - 1; i >= 0; i--) {
auto c = code_entity[i];
c = c - CodePoints::DIGIT_ZERO; // Now it is between 0 and 9
value += c * multiplier;
multiplier *= 10;
}
}
return value;
}
void print();
void reset();
};
#endif
|