-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_u32_re2js.html
169 lines (152 loc) · 6.87 KB
/
parse_u32_re2js.html
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
<!DOCTYPE html>
<html>
<head>
<title>parse_u32 Test (Realtime with History)</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>parse_u32 Function Test (Realtime with History)</h1>
<label for="inputString">Enter a string to parse:</label>
<input type="text" id="inputString" name="inputString">
<h2>Parse History:</h2>
<table id="outputTable">
<thead>
<tr>
<th>Input String</th>
<th>Output</th>
<th>Parse Time (ms)</th>
</tr>
</thead>
<tbody>
<!-- Parse results will be appended here -->
</tbody>
</table>
<script>
// Refactored JavaScript - goto statements removed and corrected cursor usage
function parse_u32(yyinput) {
let yycursor = 0
let yycond = 0; // YYC_INIT = 0
let n = 0
const YYCOND_INIT = 0;
const YYCOND_BIN = 1;
const YYCOND_OCT = 2;
const YYCOND_DEC = 3;
const YYCOND_HEX = 4;
loop: while (true) {
let yych;
if (yycursor >= yyinput.length) { yych = 0; } else { yych = yyinput.charCodeAt(yycursor); }
switch (yycond) {
case YYCOND_INIT:
if (yych === 0) {
return null; // yy1: { return null }
} else if (yych >= 49 && yych <= 57) { // '1' - '9'
yycond = YYCOND_DEC; // yy6: { yycond = YYCOND_DEC; continue loop }
continue loop; // yy4: { yycond = YYCOND_DEC; continue loop } (combined with yy6)
} else if (yych === 48) { // '0'
yycursor++; // yy5: { yycursor += (1); ... }
yycond = YYCOND_OCT; // { yycond = YYCOND_OCT; continue loop }
continue loop;
} else if (yych === 98) { // 'b'
if (yyinput.substring(yycursor, yycursor + 2) === '0b') {
yycursor += 2; // yy2: { yycursor += (2); ... }
yycond = YYCOND_BIN; // { yycond = YYCOND_BIN; continue loop }
continue loop;
} else {
return null; // Fallback to default case behavior (yy1) if '0b' prefix not found
}
} else if (yych === 120) { // 'x'
if (yyinput.substring(yycursor, yycursor + 2) === '0x') {
yycursor += 2; // yy3: { yycursor += (2); ... }
yycond = YYCOND_HEX; // { yycond = YYCOND_HEX; continue loop }
continue loop;
} else {
return null; // Fallback to default case behavior (yy1) if '0x' prefix not found
}
} else {
return null; // yy1: { return null } (default case)
}
break; // Added break to prevent fallthrough
case YYCOND_BIN:
if (yych === 0) {
return n; // yy7: { return n }
} else if (yych === 48 || yych === 49) { // '0' or '1'
n = n * 2 + (yyinput.charCodeAt(yycursor) - 48); // yy8: { ... } (modified to use yycursor, not yycursor - 1)
yycursor++;
continue loop;
} else {
return n; // yy7: { return n }
}
break;
case YYCOND_OCT:
if (yych === 0) {
return n; // yy9: { return n }
} else if (yych >= 48 && yych <= 55) { // '0' - '7'
n = n * 8 + (yyinput.charCodeAt(yycursor) - 48); // yy10: { ... } (modified to use yycursor)
yycursor++;
continue loop;
} else {
return n; // yy9: { return n }
}
break;
case YYCOND_DEC:
if (yych === 0) {
return n; // yy11: { return n }
} else if (yych >= 48 && yych <= 57) { // '0' - '9'
n = n * 10 + (yyinput.charCodeAt(yycursor) - 48); // yy12: { ... } (modified to use yycursor)
yycursor++;
continue loop;
} else {
return n; // yy11: { return n }
}
break;
case YYCOND_HEX:
if (yych === 0) {
return n; // yy13: { return n }
} else if (yych >= 48 && yych <= 57) { // '0' - '9'
n = n * 16 + (yyinput.charCodeAt(yycursor) - 48); // yy14: { ... } (modified to use yycursor)
yycursor++;
continue loop;
} else if ((yych >= 65 && yych <= 70) || (yych >= 97 && yych <= 102)) { // 'A'-'F' or 'a'-'f'
n = n * 16 + (yyinput.charCodeAt(yycursor) - (yych >= 97 ? 87 : 55)); // yy15: { ... } (modified to use yycursor)
yycursor++;
continue loop;
} else {
return n; // yy13: { return n }
}
break;
}
}
}
const inputStringElement = document.getElementById('inputString');
const outputTableBody = document.querySelector('#outputTable tbody');
inputStringElement.addEventListener('input', function() {
const inputString = inputStringElement.value;
const startTime = performance.now();
const result = parse_u32(inputString + "\0"); // Add null terminator
const endTime = performance.now();
const parseTime = (endTime - startTime).toFixed(3); // Time in milliseconds, rounded to 3 decimal places
const newRow = document.createElement('tr');
const inputCell = document.createElement('td');
inputCell.textContent = inputString;
newRow.appendChild(inputCell);
const outputCell = document.createElement('td');
outputCell.textContent = (result === null) ? "null" : result;
newRow.appendChild(outputCell);
const timeCell = document.createElement('td');
timeCell.textContent = parseTime;
newRow.appendChild(timeCell);
outputTableBody.appendChild(newRow);
});
</script>
</body>
</html>