-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTLL_Lib.htll
More file actions
775 lines (677 loc) · 16.7 KB
/
HTLL_Lib.htll
File metadata and controls
775 lines (677 loc) · 16.7 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
func StrLen(arr string) {
int the_size := 0
string.size ; place size in rax
the_size := rax
return the_size
}
; Chr(num) – returns an array with the character for ASCII code num
func arr Chr(num) {
arr result
result.add num
return result
}
func arr StringTrimRight(arr string, n) {
Loop, n {
string.pop
}
return string
}
; StringTrimLeft(string, n) – returns a new array with n bytes removed from the start
func arr StringTrimLeft(arr string, n) {
arr result
int rax_temp := 0
Loop, string.size {
if (A_Index >= n) {
string.index A_Index
rax_temp := rax
result.add rax_temp
}
}
return result
}
; countChars(s, c) – returns the number of times byte c appears in array s
func countChars(arr s, c) {
int count := 0
int rax_temp := 0
count := 0
s.size
rax_temp := rax
Loop, rax_temp {
s.index A_Index
rax_temp := rax
if (rax_temp = c) {
count++
}
}
return count
}
; SubStr(s, start, len) – returns a substring starting at start (1-based) for len characters
func arr SubStr(arr s, start, len) {
arr result
int rax_temp := 0
s.size
rax_temp := rax
int end
end := rax_temp
if (len > 0) {
end := start + len - 1
}
Loop, s.size {
if (A_Index >= start) {
if (A_Index <= end) {
s.index A_Index
rax_temp := rax
result.add rax_temp
}
}
}
return result
}
; Trim(s) – returns a new array with leading/trailing whitespace removed
func arr Trim(arr s) {
int what_size := 0
arr result
s.size
what_size := rax
int i := 0
i := 0
int j := 0
j := 0
j := what_size - 1
int isSpace := 0
int char := 0
; find first non-whitespace
Loop, what_size {
s.index i
char := rax
isSpace := 0
if (char = 32) {
isSpace := 1
}
if (char = 9) {
isSpace := 1
}
if (char = 10) {
isSpace := 1
}
if (char = 13) {
isSpace := 1
}
if (char = 11) {
isSpace := 1
}
if (char = 12) {
isSpace := 1
}
if (isSpace = 0) {
break
}
i++
}
; find last non-whitespace
Loop, what_size {
s.index j
char := rax
isSpace := 0
if (char = 32) {
isSpace := 1
}
if (char = 9) {
isSpace := 1
}
if (char = 10) {
isSpace := 1
}
if (char = 13) {
isSpace := 1
}
if (char = 11) {
isSpace := 1
}
if (char = 12) {
isSpace := 1
}
if (isSpace = 0) {
break
}
j--
}
; copy range i to j into result
Loop, what_size {
if (A_Index >= i) {
if (A_Index <= j) {
s.index A_Index
char := rax
result.add char
}
}
}
return result
}
; InStr(haystack, needle, startPos) – returns 1‑based position or 0
func InStr(arr haystack, arr needle, startPos) {
arr h_copy
h_copy.copy haystack
arr n_copy
n_copy.copy needle
int hlen := 0
int nlen := 0
int i := 0
int j := 0
int match_it := 0
int hc := 0
int nc := 0
int end_limit := 0
int idx := 0
int loop_cnt := 0
int found_pos := 0
h_copy.size
hlen := rax
n_copy.size
nlen := rax
; Explicitly initialize found_pos to hlen (Not Found)
found_pos := hlen
if (nlen = 0) {
return 0
}
if (nlen > hlen) {
return hlen
}
i := startPos
end_limit := hlen - nlen
if (i > end_limit) {
return hlen
}
loop_cnt := end_limit - i
loop_cnt += 1
Loop, loop_cnt {
match_it := 1
j := 0
Loop, nlen {
idx := i + j
h_copy.index idx
hc := rax
n_copy.index j
nc := rax
if (hc != nc) {
match_it := 0
break
}
j++
}
if (match_it = 1) {
found_pos := i
break
}
i++
}
return found_pos
}
func arr StrLower(arr s) {
int len := 0
int c := 0
arr result
s.size
len := rax
Loop, len {
s.index A_Index
c := rax
if (c >= 65) {
if (c <= 90) {
c += 32
}
}
result.add c
}
return result
}
; INT_To_STR(num) – converts an integer to an array of ASCII characters
func arr INT_To_STR(num) {
arr result
arr temp_buffer
int remainder_val := 0
int current_digit := 0
int actual_str_len := 0
int temp_size := 0
; Handle 0 edge case immediately
if (num = 0) {
result.add 48
return result
}
remainder_val := num
; Extract digits in reverse order (e.g., 123 -> '3', '2', '1')
togo str_extraction_loop
current_digit := remainder_val
current_digit %= 10
current_digit += 48
temp_buffer.add current_digit
remainder_val //= 10
if (remainder_val > 0) {
goto str_extraction_loop
}
; Get the size of our reversed buffer
temp_buffer.size
actual_str_len := rax
; Loop backwards to put them in the right order
Loop, actual_str_len {
temp_size := actual_str_len
temp_size--
temp_size -= A_Index
temp_buffer.index temp_size
result.add rax
}
return result
}
; StrReplace(haystack, needle, replacement) – Replaces all occurrences of needle with replacement
func arr StrReplace(arr haystack, arr needle, arr replacement) {
arr h_local
h_local.copy haystack
arr n_local
n_local.copy needle
arr r_local
r_local.copy replacement
arr result
arr t_arr
int hl := 0
int nl_val := 0
int sPos := 0
int p := 0
int sub_l := 0
h_local.size
hl := rax
n_local.size
nl_val := rax
; Body Initializations
sPos := 0
result.clear
if (nl_val = 0) {
result.copy h_local
return result
}
togo replace_loop
InStr(h_local, n_local, sPos)
p := rax
if (p = hl) {
goto end_replace_loop
}
sub_l := p - sPos
if (sub_l > 0) {
t_arr := SubStr(h_local, sPos, sub_l)
Loop, t_arr.size {
t_arr.index A_Index
result.add rax
}
}
Loop, r_local.size {
r_local.index A_Index
result.add rax
}
sPos := p + nl_val
if (sPos < hl) {
goto replace_loop
}
togo end_replace_loop
if (sPos < hl) {
sub_l := hl - sPos
t_arr := SubStr(h_local, sPos, sub_l)
Loop, t_arr.size {
t_arr.index A_Index
result.add rax
}
}
return result
}
; StrSplit(inputStr, delimiter, num) – Returns the Nth token (1-based)
func arr StrSplit(arr inputStr, arr delimiter, num) {
arr i_local
i_local.copy inputStr
arr d_local
d_local.copy delimiter
arr res
int h_len := 0
int d_len := 0
int s_pos := 0
int e_pos := 0
int cur_count := 0
int sub_len := 0
i_local.size
h_len := rax
d_local.size
d_len := rax
; BODY INITIALIZATIONS (Crucial for repeated calls!)
cur_count := 0
s_pos := 0
res.clear
if (d_len = 0) {
return res
}
togo split_loop
cur_count += 1
InStr(i_local, d_local, s_pos)
e_pos := rax
if (cur_count = num) {
if (e_pos = h_len) {
res := SubStr(i_local, s_pos, 0)
}
if (e_pos != h_len) {
sub_len := e_pos - s_pos
res := SubStr(i_local, s_pos, sub_len)
}
goto end_split_loop
}
if (e_pos = h_len) {
goto end_split_loop
}
s_pos := e_pos + d_len
if (s_pos < h_len) {
goto split_loop
}
togo end_split_loop
return res
}
func arr LoopParseFunc(arr var, arr delim1, arr delim2, num) {
arr v_local
v_local.copy var
arr d1_local
d1_local.copy delim1
arr d2_local
d2_local.copy delim2
arr res
int v_len := 0
int d1_len := 0
int d2_len := 0
int i := 0
int j := 0
int char_val := 0
int is_delim := 0
int t_start := 0
int t_count := 0
int in_t := 0
int s_len := 0
int stop_searching := 0
v_local.size
v_len := rax
d1_local.size
d1_len := rax
d2_local.size
d2_len := rax
; BODY INITIALIZATIONS
t_count := 0
in_t := 0
stop_searching := 0
res.clear
; Case 1: No delimiters provided (Return Nth character)
if (d1_len = 0) {
if (d2_len = 0) {
if (num > 0) {
if (num <= v_len) {
j := num - 1
v_local.index j
res.add rax
}
}
stop_searching := 1
}
}
; Case 2: Normal Multi-Delimiter Split
if (stop_searching = 0) {
i := 0
Loop, v_len {
v_local.index i
char_val := rax
is_delim := 0
; Check Delimiter 1
if (d1_len > 0) {
j := 0
Loop, d1_len {
d1_local.index j
if (rax = char_val) {
is_delim := 1
}
j += 1
}
}
; Check Delimiter 2
if (is_delim = 0) {
if (d2_len > 0) {
j := 0
Loop, d2_len {
d2_local.index j
if (rax = char_val) {
is_delim := 1
}
j += 1
}
}
}
if (is_delim = 0) {
if (in_t = 0) {
in_t := 1
t_count += 1
t_start := i
}
}
if (is_delim = 1) {
if (in_t = 1) {
in_t := 0
if (t_count = num) {
s_len := i - t_start
res := SubStr(v_local, t_start, s_len)
stop_searching := 1
break ; Safe break (pops r12/r13)
}
}
}
i += 1
}
; Final check for the last token if it hasn't been found yet
if (stop_searching = 0) {
if (in_t = 1) {
if (t_count = num) {
res := SubStr(v_local, t_start, 0)
}
}
}
}
return res ; THE ONLY RETURN IN THE FUNCTION
}
func RegExMatch(arr haystack, arr needle, startPos) {
arr h_loc
h_loc.copy haystack
arr n_loc
n_loc.copy needle
int h_len := 0
int n_len := 0
int cur_i := 0
int hi := 0
int ni := 0
int match_ok := 0
int c_h := 0
int c_n := 0
int is_w := 0
int pr_w := 0
int tmp_val := 0
int res_pos := 0
int is_esc := 0
h_loc.size
h_len := rax
n_loc.size
n_len := rax
; Correct Not Found value
res_pos := h_len
if (n_len = 0) {
return 0
}
cur_i := startPos
togo match_outer
if (cur_i >= h_len) {
goto match_outer_end
}
hi := cur_i
ni := 0
match_ok := 1
togo match_inner
if (ni >= n_len) {
goto match_inner_end
}
n_loc.index ni
c_n := rax
is_esc := 0
if (c_n = 92) { ; '\'
ni += 1
n_loc.index ni
c_n := rax
is_esc := 1
if (c_n = 100) { ; '\d'
if (hi >= h_len) { match_ok := 0 }
if (hi < h_len) {
h_loc.index hi
c_h := rax
if (c_h < 48) { match_ok := 0 }
if (c_h > 57) { match_ok := 0 }
hi += 1
}
}
if (c_n = 98) { ; '\b'
is_w := 0
if (hi < h_len) {
h_loc.index hi
tmp_val := rax
if (tmp_val > 47) { if (tmp_val < 58) { is_w := 1 } }
if (tmp_val > 64) { if (tmp_val < 91) { is_w := 1 } }
if (tmp_val > 96) { if (tmp_val < 123) { is_w := 1 } }
}
pr_w := 0
if (hi > 0) {
tmp_val := hi - 1
h_loc.index tmp_val
tmp_val := rax
if (tmp_val > 47) { if (tmp_val < 58) { pr_w := 1 } }
if (tmp_val > 64) { if (tmp_val < 91) { pr_w := 1 } }
if (tmp_val > 96) { if (tmp_val < 123) { pr_w := 1 } }
}
if (is_w = pr_w) { match_ok := 0 }
}
}
; THE FIX: Only run literal match if we didn't just process an escape!
if (is_esc = 0) {
if (hi >= h_len) { match_ok := 0 }
if (hi < h_len) {
h_loc.index hi
c_h := rax
if (c_h != c_n) { match_ok := 0 }
hi += 1
}
}
if (match_ok = 0) {
goto match_inner_end
}
ni += 1
goto match_inner
togo match_inner_end
if (match_ok = 1) {
res_pos := cur_i
goto match_outer_end
}
cur_i += 1
goto match_outer
togo match_outer_end
return res_pos
}
func arr RegExReplace(arr input, arr pattern, arr replacement) {
arr i_loc
i_loc.copy input
arr p_loc
p_loc.copy pattern
arr r_loc
r_loc.copy replacement
arr out_arr
arr sub_chunk
int tot_len := 0
int pat_len := 0
int cur_p := 0
int m_pos := 0
int chunk_l := 0
int rax_val := 0
int r_len := 0
int sc_len := 0
int m_len := 0
int ni := 0
int c_n := 0
int is_esc := 0
i_loc.size
tot_len := rax
p_loc.size
pat_len := rax
r_loc.size
r_len := rax
cur_p := 0
out_arr.clear
m_len := 0
ni := 0
togo calc_mlen
if (ni >= pat_len) { goto calc_mlen_end }
p_loc.index ni
c_n := rax
is_esc := 0
if (c_n = 92) {
ni += 1
p_loc.index ni
c_n := rax
is_esc := 1
if (c_n = 100) { m_len += 1 }
if (c_n = 98) { m_len += 0 }
}
if (is_esc = 0) { m_len += 1 }
ni += 1
goto calc_mlen
togo calc_mlen_end
if (m_len = 0) { m_len := 1 }
togo repl_engine
RegExMatch(i_loc, p_loc, cur_p)
m_pos := rax
if (m_pos = tot_len) { goto repl_engine_end }
chunk_l := m_pos - cur_p
if (chunk_l > 0) {
sub_chunk := SubStr(i_loc, cur_p, chunk_l)
sub_chunk.size
sc_len := rax
ni := 0
togo copy_before
if (ni >= sc_len) { goto copy_before_end }
sub_chunk.index ni
rax_val := rax
out_arr.add rax_val
ni += 1
goto copy_before
togo copy_before_end
}
ni := 0
togo copy_repl
if (ni >= r_len) { goto copy_repl_end }
r_loc.index ni
rax_val := rax
out_arr.add rax_val
ni += 1
goto copy_repl
togo copy_repl_end
cur_p := m_pos + m_len
if (cur_p < tot_len) { goto repl_engine }
togo repl_engine_end
if (cur_p < tot_len) {
chunk_l := tot_len - cur_p
sub_chunk := SubStr(i_loc, cur_p, chunk_l)
sub_chunk.size
sc_len := rax
ni := 0
togo copy_after
if (ni >= sc_len) { goto copy_after_end }
sub_chunk.index ni
rax_val := rax
out_arr.add rax_val
ni += 1
goto copy_after
togo copy_after_end
}
return out_arr
}