-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cpp
More file actions
512 lines (436 loc) · 13 KB
/
Copy pathstrings.cpp
File metadata and controls
512 lines (436 loc) · 13 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "errors.h"
#include "parser.h"
#include "strings.h"
#include "vm.h"
#include <algorithm>
#include <cstring>
uint CString::size() const {
return size_;
}
const char* CString::str() const {
return str_;
}
std::string CString::to_string() const {
return std::string(str_, str_ + size_);
}
uint CString::alloc_size(uint num_chars) {
if (num_chars > MAX_CSTRING_SZ) {
error(Error::ParsedStringOverflow, std::to_string(num_chars));
}
return aligned(1 + num_chars + 1); // count + chars + BL after string
}
void CString::set_cstring(const char* str, uint size) {
if (size > MAX_CSTRING_SZ) {
error(Error::ParsedStringOverflow, std::string(str, str + size));
}
size_ = static_cast<uchar>(size);
memcpy(str_, str, size);
str_[size] = BL; // BL after the string
}
void CString::set_cstring(const std::string& str) {
set_cstring(str.c_str(), static_cast<uint>(str.size()));
}
std::string LongString::to_string() const {
return std::string(str_, str_ + size_);
}
uint LongString::alloc_size(uint num_chars) {
if (num_chars > BUFFER_SZ) {
error(Error::InputBufferOverflow, std::to_string(num_chars));
}
return aligned(sizeof(size_) + num_chars +
1); // count + chars + BL after string
}
void LongString::set_string(const char* str, uint size) {
if (size > BUFFER_SZ) {
error(Error::InputBufferOverflow, std::string(str, str + size));
}
size_ = size;
memcpy(str_, str, size);
str_[size] = BL; // BL after the string
}
void LongString::set_string(const std::string& str) {
set_string(str.c_str(), static_cast<uint>(str.size()));
}
void Wordbuf::init() {
memset(vm.wordbuf_data, BL, WORDBUF_SZ);
vm.wordbuf_ptr = 0;
}
CString* Wordbuf::append_cstring(const std::string& str) {
return append_cstring(str.c_str(), static_cast<uint>(str.size()));
}
CString* Wordbuf::append_cstring(const char* str, uint size) {
if (size > MAX_CSTRING_SZ) {
error(Error::ParsedStringOverflow, std::string(str, str + size));
}
uint alloc_size = CString::alloc_size(size);
if (vm.wordbuf_ptr + alloc_size > WORDBUF_SZ) {
vm.wordbuf_ptr = 0;
}
CString* cstring = reinterpret_cast<CString*>(vm.wordbuf_data + vm.wordbuf_ptr);
vm.wordbuf_ptr += alloc_size;
cstring->set_cstring(str, size);
return cstring;
}
LongString* Wordbuf::append_long_string(const std::string& str) {
return append_long_string(str.c_str(), static_cast<uint>(str.size()));
}
LongString* Wordbuf::append_long_string(const char* str, uint size) {
if (size > BUFFER_SZ) {
error(Error::InputBufferOverflow, std::string(str, str + size));
}
uint alloc_size = LongString::alloc_size(size);
if (vm.wordbuf_ptr + alloc_size > WORDBUF_SZ) {
vm.wordbuf_ptr = 0;
}
LongString* lstring = reinterpret_cast<LongString*>(
vm.wordbuf_data + vm.wordbuf_ptr);
vm.wordbuf_ptr += alloc_size;
lstring->set_string(str, size);
return lstring;
}
static char to_lower(char c) {
return tolower(static_cast<unsigned char>(c));
}
bool case_insensitive_equal(const std::string& a, const std::string& b) {
return case_insensitive_equal(a.c_str(), static_cast<uint>(a.size()),
b.c_str(), static_cast<uint>(b.size()));
}
bool case_insensitive_equal(const char* a_str, uint a_size, const char* b_str,
uint b_size) {
if (a_size != b_size) {
return false;
}
for (uint i = 0; i < a_size; ++i) {
if (to_lower(a_str[i]) != to_lower(b_str[i])) {
return false;
}
}
return true;
}
std::string to_upper(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}
void f_count() {
uint addr = pop();
int len = cfetch(addr++);
push(addr);
push(len);
}
void f_dot_quote() {
uint size;
const char* message = parse_word(size, '"');
if (vm.user->STATE == STATE_COMPILE) {
int str_addr = vm.dict.alloc_string(message, size);
comma(xtXDOT_QUOTE);
comma(str_addr);
}
else {
print_string(message, size);
}
}
void f_xdot_quote() {
int str_addr = fetch(vm.ip);
vm.ip += CELL_SZ;
const LongString* message = reinterpret_cast<const LongString*>(mem_char_ptr(
str_addr));
print_string(message->str(), message->size());
}
void f_s_quote() {
uint size;
const char* message = parse_word(size, '"');
if (vm.user->STATE == STATE_COMPILE) {
int str_addr = vm.dict.alloc_string(message, size);
comma(xtXSLITERAL);
comma(str_addr);
}
else {
LongString* lstring = vm.wordbuf.append_long_string(message, size);
push(mem_addr(lstring->str()));
push(lstring->size());
}
}
void f_s_backslash_quote() {
std::string message = parse_backslash_string();
if (vm.user->STATE == STATE_COMPILE) {
int str_addr = vm.dict.alloc_string(message.c_str(),
static_cast<uint>(message.size()));
comma(xtXSLITERAL);
comma(str_addr);
}
else {
LongString* lstring = vm.wordbuf.append_long_string(message);
push(mem_addr(lstring->str()));
push(lstring->size());
}
}
void f_xsliteral() {
int str_addr = fetch(vm.ip);
vm.ip += CELL_SZ;
const LongString* message = reinterpret_cast<const LongString*>(mem_char_ptr(
str_addr));
push(mem_addr(message->str()));
push(message->size());
}
void f_c_quote() {
const CString* message = parse_cword('"');
if (vm.user->STATE == STATE_COMPILE) {
int str_addr = vm.dict.alloc_cstring(message);
comma(xtXC_QUOTE);
comma(str_addr);
}
else {
push(mem_addr(message));
}
}
void f_xc_quote() {
int str_addr = fetch(vm.ip);
vm.ip += CELL_SZ;
const CString* message = reinterpret_cast<const CString*>(mem_char_ptr(
str_addr));
push(mem_addr(message));
}
void f_dot_paren() {
uint size;
const char* message = parse_word(size, ')');
print_string(message, size);
}
void f_minus_trailing() {
uint size = pop();
uint addr = pop();
const char* str = mem_char_ptr(addr, size);
while (size > 0 && is_space(str[size - 1])) {
size--;
}
addr = mem_addr(str);
push(addr);
push(size);
}
void f_slash_string() {
int n = pop();
uint size = pop();
uint addr = pop();
push(addr + n);
push(size - n);
}
void f_blank() {
int n = pop();
uint addr = pop();
if (n > 0) {
memset(mem_char_ptr(addr, n), BL, n);
}
}
void f_cmove() {
int n = pop();
uint dst_addr = pop();
uint src_addr = pop();
if (n > 0) {
const char* src = mem_char_ptr(src_addr, n);
char* dst = mem_char_ptr(dst_addr, n);
// memcpy on ovelapped regions is undefined behaviour
while (n-- > 0) {
*dst++ = *src++;
}
}
}
void f_cmove_to() {
int n = pop();
uint dst_addr = pop();
uint src_addr = pop();
if (n > 0) {
const char* src = mem_char_ptr(src_addr, n) + n;
char* dst = mem_char_ptr(dst_addr, n) + n;
// memcpy on ovelapped regions is undefined behaviour
while (n-- > 0) {
*--dst = *--src;
}
}
}
void f_compare() {
uint len2 = pop();
uint addr2 = pop();
const char* str2 = mem_char_ptr(addr2, len2);
uint len1 = pop();
uint addr1 = pop();
const char* str1 = mem_char_ptr(addr1, len1);
if (len1 < len2) {
int cmp = memcmp(str1, str2, len1);
if (cmp == 0) {
push(-1);
}
else if (cmp < 0) {
push(-1);
}
else {
push(1);
}
}
else if (len1 > len2) {
int cmp = memcmp(str1, str2, len2);
if (cmp == 0) {
push(1);
}
else if (cmp < 0) {
push(-1);
}
else {
push(1);
}
}
else {
int cmp = memcmp(str1, str2, len1);
if (cmp == 0) {
push(0);
}
else if (cmp < 0) {
push(-1);
}
else {
push(1);
}
}
}
void f_search() {
uint len2 = pop();
uint addr2 = pop();
const char* str2 = mem_char_ptr(addr2, len2);
uint len1 = pop();
uint addr1 = pop();
const char* str1 = mem_char_ptr(addr1, len1);
if (len2 == 0) {
// empty string always matches at start
push(addr1); // address of match
push(len1); // length of match to end
push(F_TRUE);
}
else if (len1 == 0 || len2 > len1) {
// empty string or str2 longer than str1 never matches
push(addr1);
push(len1);
push(F_FALSE);
}
else {
const char* p = std::search(str1, str1 + len1, str2, str2 + len2);
if (p != str1 + len1) {
push(mem_addr(p)); // address of match
push(static_cast<int>(str1 + len1 - p));// length of match to end
push(F_TRUE);
}
else {
push(addr1);
push(len1);
push(F_FALSE);
}
}
}
void f_sliteral() {
uint size = pop();
uint addr = pop();
const char* str = mem_char_ptr(addr, size);
// save copy of the string in names space
uint saved_addr = vm.dict.alloc_string(str, size);
// save execution code
comma(xtXSLITERAL);
comma(saved_addr);
}
void f_replaces() {
uint name_len = pop();
uint name_addr = pop();
const char* name_str = mem_char_ptr(name_addr, name_len);
std::string name = std::string(name_str, name_str + name_len);
uint text_len = pop();
uint text_addr = pop();
const char* text_str = mem_char_ptr(text_addr, text_len);
std::string text = std::string(text_str, text_str + text_len);
vm.substitutions[to_upper(name)] = text;
}
static std::string substitute(const std::string& input,
const std::unordered_map<std::string, std::string>& substitutions,
int& count) {
std::string result;
size_t pos = 0;
count = 0;
while (pos < input.size()) {
size_t start = input.find('%', pos);
if (start == std::string::npos) {
result += input.substr(pos);
break;
}
size_t end = input.find('%', start + 1);
if (end == std::string::npos) {
result += input.substr(pos);
break;
}
result += input.substr(pos, start - pos); // text before %NAME%
std::string key = input.substr(start + 1, end - start - 1);
if (key.empty()) {
result += "%"; // handle %% as literal %
pos = end + 1;
continue;
}
auto it = substitutions.find(to_upper(key));
if (it != substitutions.end()) {
result += it->second; // replace with value
++count;
}
else {
result += "%" + key + "%"; // leave unchanged
}
pos = end + 1;
}
return result;
}
static std::string escape_percents(const std::string& input) {
std::string result;
result.reserve(input.size() *
2); // Reserve enough space to avoid reallocations
for (char ch : input) {
if (ch == '%') {
result += "%%";
}
else {
result += ch;
}
}
return result;
}
void f_substitute() {
uint buffer_len = pop();
uint buffer_addr = pop();
char* buffer_str = mem_char_ptr(buffer_addr, buffer_len);
uint str_len = pop();
uint str_addr = pop();
const char* str_str = mem_char_ptr(str_addr, str_len);
std::string str = std::string(str_str, str_str + str_len);
int count = 0;
std::string result = substitute(str, vm.substitutions, count);
if (static_cast<uint>(result.size()) > buffer_len) {
push(0);
push(0);
push(static_cast<int>(Error::SubstituteException));
}
else {
memcpy(buffer_str, result.c_str(), result.size());
push(buffer_addr);
push(static_cast<int>(result.size()));
push(count);
}
}
void f_unescape() {
uint buffer_addr = pop();
char* buffer_str = mem_char_ptr(buffer_addr);
uint str_len = pop();
uint str_addr = pop();
const char* str_str = mem_char_ptr(str_addr, str_len);
std::string str = std::string(str_str, str_str + str_len);
std::string result = escape_percents(str);
memcpy(buffer_str, result.c_str(), result.size());
push(buffer_addr);
push(static_cast<int>(result.size()));
}