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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
|
# Some useful helpers for dealing with text (arrays of characters)
# to-text-line gets called implicitly in various places
# define it to be identical to 'to-text' by default
def to-text-line x:_elem -> y:address:array:character [
local-scope
load-ingredients
y <- to-text x
]
# variant for arrays (since we can't pass them around otherwise)
def array-to-text-line x:address:array:_elem -> y:address:array:character [
local-scope
load-ingredients
y <- to-text *x
]
# to-text on text is just the identity function
def to-text x:address:array:character -> y:address:array:character [
local-scope
load-ingredients
return x
]
def equal a:address:array:character, b:address:array:character -> result:boolean [
local-scope
load-ingredients
a-len:number <- length *a
b-len:number <- length *b
# compare lengths
{
trace 99, [text-equal], [comparing lengths]
length-equal?:boolean <- equal a-len, b-len
break-if length-equal?
return 0
}
# compare each corresponding character
trace 99, [text-equal], [comparing characters]
i:number <- copy 0
{
done?:boolean <- greater-or-equal i, a-len
break-if done?
a2:character <- index *a, i
b2:character <- index *b, i
{
chars-match?:boolean <- equal a2, b2
break-if chars-match?
return 0
}
i <- add i, 1
loop
}
return 1
]
scenario text-equal-reflexive [
run [
default-space:address:array:location <- new location:type, 30
x:address:array:character <- new [abc]
3:boolean/raw <- equal x, x
]
memory-should-contain [
3 <- 1 # x == x for all x
]
]
scenario text-equal-identical [
run [
default-space:address:array:location <- new location:type, 30
x:address:array:character <- new [abc]
y:address:array:character <- new [abc]
3:boolean/raw <- equal x, y
]
memory-should-contain [
3 <- 1 # abc == abc
]
]
scenario text-equal-distinct-lengths [
run [
default-space:address:array:location <- new location:type, 30
x:address:array:character <- new [abc]
y:address:array:character <- new [abcd]
3:boolean/raw <- equal x, y
]
memory-should-contain [
3 <- 0 # abc != abcd
]
trace-should-contain [
text-equal: comparing lengths
]
trace-should-not-contain [
text-equal: comparing characters
]
]
scenario text-equal-with-empty [
run [
default-space:address:array:location <- new location:type, 30
x:address:array:character <- new []
y:address:array:character <- new [abcd]
3:boolean/raw <- equal x, y
]
memory-should-contain [
3 <- 0 # "" != abcd
]
]
scenario text-equal-common-lengths-but-distinct [
run [
default-space:address:array:location <- new location:type, 30
x:address:array:character <- new [abc]
y:address:array:character <- new [abd]
3:boolean/raw <- equal x, y
]
memory-should-contain [
3 <- 0 # abc != abd
]
]
# A new type to help incrementally construct texts.
container buffer [
length:number
data:address:array:character
]
def new-buffer capacity:number -> result:address:buffer [
local-scope
load-ingredients
result <- new buffer:type
*result <- put *result, length:offset, 0
data:address:array:character <- new character:type, capacity
*result <- put *result, data:offset, data
return result
]
def grow-buffer in:address:buffer -> in:address:buffer [
local-scope
load-ingredients
# double buffer size
olddata:address:array:character <- get *in, data:offset
oldlen:number <- length *olddata
newlen:number <- multiply oldlen, 2
newdata:address:array:character <- new character:type, newlen
*in <- put *in, data:offset, newdata
# copy old contents
i:number <- copy 0
{
done?:boolean <- greater-or-equal i, oldlen
break-if done?
src:character <- index *olddata, i
*newdata <- put-index *newdata, i, src
i <- add i, 1
loop
}
]
def buffer-full? in:address:buffer -> result:boolean [
local-scope
load-ingredients
len:number <- get *in, length:offset
s:address:array:character <- get *in, data:offset
capacity:number <- length *s
result <- greater-or-equal len, capacity
]
# most broadly applicable definition of append to a buffer: just call to-text
def append buf:address:buffer, x:_elem -> buf:address:buffer [
local-scope
load-ingredients
text:address:array:character <- to-text x
len:number <- length *text
i:number <- copy 0
{
done?:boolean <- greater-or-equal i, len
break-if done?
c:character <- index *text, i
buf <- append buf, c
i <- add i, 1
loop
}
]
def append in:address:buffer, c:character -> in:address:buffer [
local-scope
load-ingredients
len:number <- get *in, length:offset
{
# backspace? just drop last character if it exists and return
backspace?:boolean <- equal c, 8/backspace
break-unless backspace?
empty?:boolean <- lesser-or-equal len, 0
return-if empty?
len <- subtract len, 1
*in <- put *in, length:offset, len
return
}
{
# grow buffer if necessary
full?:boolean <- buffer-full? in
break-unless full?
in <- grow-buffer in
}
s:address:array:character <- get *in, data:offset
*s <- put-index *s, len, c
len <- add len, 1
*in <- put *in, length:offset, len
]
scenario buffer-append-works [
run [
local-scope
x:address:buffer <- new-buffer 3
s1:address:array:character <- get *x, data:offset
c:character <- copy 97/a
x <- append x, c
c:character <- copy 98/b
x <- append x, c
c:character <- copy 99/c
x <- append x, c
s2:address:array:character <- get *x, data:offset
1:boolean/raw <- equal s1, s2
2:array:character/raw <- copy *s2
+buffer-filled
c:character <- copy 100/d
x <- append x, c
s3:address:array:character <- get *x, data:offset
10:boolean/raw <- equal s1, s3
11:number/raw <- get *x, length:offset
12:array:character/raw <- copy *s3
]
memory-should-contain [
# before +buffer-filled
1 <- 1 # no change in data pointer
2 <- 3 # size of data
3 <- 97 # data
4 <- 98
5 <- 99
# in the end
10 <- 0 # data pointer has grown
11 <- 4 # final length
12 <- 6 # but data's capacity has doubled
13 <- 97 # data
14 <- 98
15 <- 99
16 <- 100
17 <- 0
18 <- 0
]
]
scenario buffer-append-handles-backspace [
run [
local-scope
x:address:buffer <- new-buffer 3
c:character <- copy 97/a
x <- append x, c
c:character <- copy 98/b
x <- append x, c
c:character <- copy 8/backspace
x <- append x, c
s:address:array:character <- buffer-to-array x
1:array:character/raw <- copy *s
]
memory-should-contain [
1 <- 1 # length
2 <- 97 # contents
3 <- 0
]
]
def to-text n:number -> result:address:array:character [
local-scope
load-ingredients
# is n zero?
{
break-if n
result <- new [0]
return
}
# save sign
negate-result:boolean <- copy 0
{
negative?:boolean <- lesser-than n, 0
break-unless negative?
negate-result <- copy 1
n <- multiply n, -1
}
# add digits from right to left into intermediate buffer
tmp:address:buffer <- new-buffer 30
digit-base:number <- copy 48 # '0'
{
done?:boolean <- equal n, 0
break-if done?
n, digit:number <- divide-with-remainder n, 10
c:character <- add digit-base, digit
tmp:address:buffer <- append tmp, c
loop
}
# add sign
{
break-unless negate-result:boolean
minus:character <- copy 45/-
tmp <- append tmp, minus
}
# reverse buffer into text result
len:number <- get *tmp, length:offset
buf:address:array:character <- get *tmp, data:offset
result <- new character:type, len
i:number <- subtract len, 1 # source index, decreasing
j:number <- copy 0 # destination index, increasing
{
# while i >= 0
done?:boolean <- lesser-than i, 0
break-if done?
# result[j] = tmp[i]
src:character <- index *buf, i
*result <- put-index *result, j, src
i <- subtract i, 1
j <- add j, 1
loop
}
]
def to-text x:boolean -> result:address:array:character [
local-scope
load-ingredients
n:number <- copy x:boolean
result <- to-text n
]
def to-text x:address:_elem -> result:address:array:character [
local-scope
load-ingredients
n:number <- copy x
result <- to-text n
]
def buffer-to-array in:address:buffer -> result:address:array:character [
local-scope
load-ingredients
{
# propagate null buffer
break-if in
return 0
}
len:number <- get *in, length:offset
s:address:array:character <- get *in, data:offset
# we can't just return s because it is usually the wrong length
result <- new character:type, len
i:number <- copy 0
{
done?:boolean <- greater-or-equal i, len
break-if done?
src:character <- index *s, i
*result <- put-index *result, i, src
i <- add i, 1
loop
}
]
scenario integer-to-decimal-digit-zero [
run [
1:address:array:character/raw <- to-text 0
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [0]
]
]
scenario integer-to-decimal-digit-positive [
run [
1:address:array:character/raw <- to-text 234
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [234]
]
]
scenario integer-to-decimal-digit-negative [
run [
1:address:array:character/raw <- to-text -1
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2 <- 2
3 <- 45 # '-'
4 <- 49 # '1'
]
]
def append a:address:array:character, b:address:array:character -> result:address:array:character [
local-scope
load-ingredients
# result = new character[a.length + b.length]
a-len:number <- length *a
b-len:number <- length *b
result-len:number <- add a-len, b-len
result <- new character:type, result-len
# copy a into result
result-idx:number <- copy 0
i:number <- copy 0
{
# while i < a.length
a-done?:boolean <- greater-or-equal i, a-len
break-if a-done?
# result[result-idx] = a[i]
in:character <- index *a, i
*result <- put-index *result, result-idx, in
i <- add i, 1
result-idx <- add result-idx, 1
loop
}
# copy b into result
i <- copy 0
{
# while i < b.length
b-done?:boolean <- greater-or-equal i, b-len
break-if b-done?
# result[result-idx] = a[i]
in:character <- index *b, i
*result <- put-index *result, result-idx, in
i <- add i, 1
result-idx <- add result-idx, 1
loop
}
]
scenario text-append-1 [
run [
1:address:array:character/raw <- new [hello,]
2:address:array:character/raw <- new [ world!]
3:address:array:character/raw <- append 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy *3:address:array:character/raw
]
memory-should-contain [
4:array:character <- [hello, world!]
]
]
scenario replace-character-in-text [
run [
1:address:array:character/raw <- new [abc]
1:address:array:character/raw <- replace 1:address:array:character/raw, 98/b, 122/z
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [azc]
]
]
def replace s:address:array:character, oldc:character, newc:character, from:number/optional -> s:address:array:character [
local-scope
load-ingredients
len:number <- length *s
i:number <- find-next s, oldc, from
done?:boolean <- greater-or-equal i, len
return-if done?, s/same-as-ingredient:0
*s <- put-index *s, i, newc
i <- add i, 1
s <- replace s, oldc, newc, i
]
scenario replace-character-at-start [
run [
1:address:array:character/raw <- new [abc]
1:address:array:character/raw <- replace 1:address:array:character/raw, 97/a, 122/z
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [zbc]
]
]
scenario replace-character-at-end [
run [
1:address:array:character/raw <- new [abc]
1:address:array:character/raw <- replace 1:address:array:character/raw, 99/c, 122/z
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [abz]
]
]
scenario replace-character-missing [
run [
1:address:array:character/raw <- new [abc]
1:address:array:character/raw <- replace 1:address:array:character/raw, 100/d, 122/z
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [abc]
]
]
scenario replace-all-characters [
run [
1:address:array:character/raw <- new [banana]
1:address:array:character/raw <- replace 1:address:array:character/raw, 97/a, 122/z
2:array:character/raw <- copy *1:address:array:character/raw
]
memory-should-contain [
2:array:character <- [bznznz]
]
]
# replace underscores in first with remaining args
def interpolate template:address:array:character -> result:address:array:character [
local-scope
load-ingredients # consume just the template
# compute result-len, space to allocate for result
tem-len:number <- length *template
result-len:number <- copy tem-len
{
# while ingredients remain
a:address:array:character, arg-received?:boolean <- next-ingredient
break-unless arg-received?
# result-len = result-len + arg.length - 1 (for the 'underscore' being replaced)
a-len:number <- length *a
result-len <- add result-len, a-len
result-len <- subtract result-len, 1
loop
}
rewind-ingredients
_ <- next-ingredient # skip template
result <- new character:type, result-len
# repeatedly copy sections of template and 'holes' into result
result-idx:number <- copy 0
i:number <- copy 0
{
# while arg received
a:address:array:character, arg-received?:boolean <- next-ingredient
break-unless arg-received?
# copy template into result until '_'
{
# while i < template.length
tem-done?:boolean <- greater-or-equal i, tem-len
break-if tem-done?, +done:label
# while template[i] != '_'
in:character <- index *template, i
underscore?:boolean <- equal in, 95/_
break-if underscore?
# result[result-idx] = template[i]
*result <- put-index *result, result-idx, in
i <- add i, 1
result-idx <- add result-idx, 1
loop
}
# copy 'a' into result
j:number <- copy 0
{
# while j < a.length
arg-done?:boolean <- greater-or-equal j, a-len
break-if arg-done?
# result[result-idx] = a[j]
in:character <- index *a, j
*result <- put-index *result, result-idx, in
j <- add j, 1
result-idx <- add result-idx, 1
loop
}
# skip '_' in template
i <- add i, 1
loop # interpolate next arg
}
+done
# done with holes; copy rest of template directly into result
{
# while i < template.length
tem-done?:boolean <- greater-or-equal i, tem-len
break-if tem-done?
# result[result-idx] = template[i]
in:character <- index *template, i
*result <- put-index *result, result-idx, in
i <- add i, 1
result-idx <- add result-idx, 1
loop
}
]
scenario interpolate-works [
run [
1:address:array:character/raw <- new [abc _]
2:address:array:character/raw <- new [def]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy *3:address:array:character/raw
]
memory-should-contain [
4:array:character <- [abc def]
]
]
scenario interpolate-at-start [
run [
1:address:array:character/raw <- new [_, hello!]
2:address:array:character/raw <- new [abc]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy *3:address:array:character/raw
]
memory-should-contain [
4:array:character <- [abc, hello!]
16 <- 0 # out of bounds
]
]
scenario interpolate-at-end [
run [
1:address:array:character/raw <- new [hello, _]
2:address:array:character/raw <- new [abc]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy *3:address:array:character/raw
]
memory-should-contain [
4:array:character <- [hello, abc]
]
]
# result:boolean <- space? c:character
def space? c:character -> result:boolean [
local-scope
load-ingredients
# most common case first
result <- equal c, 32/space
return-if result
result <- equal c, 10/newline
return-if result
result <- equal c, 9/tab
return-if result
result <- equal c, 13/carriage-return
return-if result
# remaining uncommon cases in sorted order
# http://unicode.org code-points in unicode-set Z and Pattern_White_Space
result <- equal c, 11/ctrl-k
return-if result
result <- equal c, 12/ctrl-l
return-if result
result <- equal c, 133/ctrl-0085
return-if result
result <- equal c, 160/no-break-space
return-if result
result <- equal c, 5760/ogham-space-mark
return-if result
result <- equal c, 8192/en-quad
return-if result
result <- equal c, 8193/em-quad
return-if result
result <- equal c, 8194/en-space
return-if result
result <- equal c, 8195/em-space
return-if result
result <- equal c, 8196/three-per-em-space
return-if result
result <- equal c, 8197/four-per-em-space
return-if result
result <- equal c, 8198/six-per-em-space
return-if result
result <- equal c, 8199/figure-space
return-if result
result <- equal c, 8200/punctuation-space
return-if result
result <- equal c, 8201/thin-space
return-if result
result <- equal c, 8202/hair-space
return-if result
result <- equal c, 8206/left-to-right
return-if result
result <- equal c, 8207/right-to-left
return-if result
result <- equal c, 8232/line-separator
return-if result
result <- equal c, 8233/paragraph-separator
return-if result
result <- equal c, 8239/narrow-no-break-space
return-if result
result <- equal c, 8287/medium-mathematical-space
return-if result
result <- equal c, 12288/ideographic-space
]
def trim s:address:array:character -> result:address:array:character [
local-scope
load-ingredients
len:number <- length *s
# left trim: compute start
start:number <- copy 0
{
{
at-end?:boolean <- greater-or-equal start, len
break-unless at-end?
result <- new character:type, 0
return
}
curr:character <- index *s, start
whitespace?:boolean <- space? curr
break-unless whitespace?
start <- add start, 1
loop
}
# right trim: compute end
end:number <- subtract len, 1
{
not-at-start?:boolean <- greater-than end, start
assert not-at-start?, [end ran up against start]
curr:character <- index *s, end
whitespace?:boolean <- space? curr
break-unless whitespace?
end <- subtract end, 1
loop
}
# result = new character[end+1 - start]
new-len:number <- subtract end, start, -1
result:address:array:character <- new character:type, new-len
# copy the untrimmed parts between start and end
i:number <- copy start
j:number <- copy 0
{
# while i <= end
done?:boolean <- greater-than i, end
break-if done?
# result[j] = s[i]
src:character <- index *s, i
*result <- put-index *result, j, src
i <- add i, 1
j <- add j, 1
loop
}
]
scenario trim-unmodified [
run [
1:address:array:character <- new [abc]
2:address:array:character <- trim 1:address:array:character
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [abc]
]
]
scenario trim-left [
run [
1:address:array:character <- new [ abc]
2:address:array:character <- trim 1:address:array:character
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [abc]
]
]
scenario trim-right [
run [
1:address:array:character <- new [abc ]
2:address:array:character <- trim 1:address:array:character
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [abc]
]
]
scenario trim-left-right [
run [
1:address:array:character <- new [ abc ]
2:address:array:character <- trim 1:address:array:character
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [abc]
]
]
scenario trim-newline-tab [
run [
1:address:array:character <- new [ abc
]
2:address:array:character <- trim 1:address:array:character
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [abc]
]
]
def find-next text:address:array:character, pattern:character, idx:number -> next-index:number [
local-scope
load-ingredients
len:number <- length *text
{
eof?:boolean <- greater-or-equal idx, len
break-if eof?
curr:character <- index *text, idx
found?:boolean <- equal curr, pattern
break-if found?
idx <- add idx, 1
loop
}
return idx
]
scenario text-find-next [
run [
1:address:array:character <- new [a/b]
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 1
]
]
scenario text-find-next-empty [
run [
1:address:array:character <- new []
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 0
]
]
scenario text-find-next-initial [
run [
1:address:array:character <- new [/abc]
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 0 # prefix match
]
]
scenario text-find-next-final [
run [
1:address:array:character <- new [abc/]
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 3 # suffix match
]
]
scenario text-find-next-missing [
run [
1:address:array:character <- new [abc]
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 3 # no match
]
]
scenario text-find-next-invalid-index [
run [
1:address:array:character <- new [abc]
2:number <- find-next 1:address:array:character, 47/slash, 4/start-index
]
memory-should-contain [
2 <- 4 # no change
]
]
scenario text-find-next-first [
run [
1:address:array:character <- new [ab/c/]
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
]
memory-should-contain [
2 <- 2 # first '/' of multiple
]
]
scenario text-find-next-second [
run [
1:address:array:character <- new [ab/c/]
2:number <- find-next 1:address:array:character, 47/slash, 3/start-index
]
memory-should-contain [
2 <- 4 # second '/' of multiple
]
]
# search for a pattern of multiple characters
# fairly dumb algorithm
def find-next text:address:array:character, pattern:address:array:character, idx:number -> next-index:number [
local-scope
load-ingredients
first:character <- index *pattern, 0
# repeatedly check for match at current idx
len:number <- length *text
{
# does some unnecessary work checking even when there isn't enough of text left
done?:boolean <- greater-or-equal idx, len
break-if done?
found?:boolean <- match-at text, pattern, idx
break-if found?
idx <- add idx, 1
# optimization: skip past indices that definitely won't match
idx <- find-next text, first, idx
loop
}
return idx
]
scenario find-next-text-1 [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [bc]
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 1
]
]
scenario find-next-text-2 [
run [
1:address:array:character <- new [abcd]
2:address:array:character <- new [bc]
3:number <- find-next 1:address:array:character, 2:address:array:character, 1
]
memory-should-contain [
3 <- 1
]
]
scenario find-next-no-match [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [bd]
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 3 # not found
]
]
scenario find-next-suffix-match [
run [
1:address:array:character <- new [abcd]
2:address:array:character <- new [cd]
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 2
]
]
scenario find-next-suffix-match-2 [
run [
1:address:array:character <- new [abcd]
2:address:array:character <- new [cde]
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 4 # not found
]
]
# checks if pattern matches at index 'idx'
def match-at text:address:array:character, pattern:address:array:character, idx:number -> result:boolean [
local-scope
load-ingredients
pattern-len:number <- length *pattern
# check that there's space left for the pattern
{
x:number <- length *text
x <- subtract x, pattern-len
enough-room?:boolean <- lesser-or-equal idx, x
break-if enough-room?
return 0/not-found
}
# check each character of pattern
pattern-idx:number <- copy 0
{
done?:boolean <- greater-or-equal pattern-idx, pattern-len
break-if done?
c:character <- index *text, idx
exp:character <- index *pattern, pattern-idx
{
match?:boolean <- equal c, exp
break-if match?
return 0/not-found
}
idx <- add idx, 1
pattern-idx <- add pattern-idx, 1
loop
}
return 1/found
]
scenario match-at-checks-pattern-at-index [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [ab]
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 1 # match found
]
]
scenario match-at-reflexive [
run [
1:address:array:character <- new [abc]
3:boolean <- match-at 1:address:array:character, 1:address:array:character, 0
]
memory-should-contain [
3 <- 1 # match found
]
]
scenario match-at-outside-bounds [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [a]
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 4
]
memory-should-contain [
3 <- 0 # never matches
]
]
scenario match-at-empty-pattern [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new []
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 1 # always matches empty pattern given a valid index
]
]
scenario match-at-empty-pattern-outside-bound [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new []
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 4
]
memory-should-contain [
3 <- 0 # no match
]
]
scenario match-at-empty-text [
run [
1:address:array:character <- new []
2:address:array:character <- new [abc]
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 0 # no match
]
]
scenario match-at-empty-against-empty [
run [
1:address:array:character <- new []
3:boolean <- match-at 1:address:array:character, 1:address:array:character, 0
]
memory-should-contain [
3 <- 1 # matches because pattern is also empty
]
]
scenario match-at-inside-bounds [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [bc]
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 1
]
memory-should-contain [
3 <- 1 # match
]
]
scenario match-at-inside-bounds-2 [
run [
1:address:array:character <- new [abc]
2:address:array:character <- new [bc]
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
]
memory-should-contain [
3 <- 0 # no match
]
]
def split s:address:array:character, delim:character -> result:address:array:address:array:character [
local-scope
load-ingredients
# empty text? return empty array
len:number <- length *s
{
empty?:boolean <- equal len, 0
break-unless empty?
result <- new {(address array character): type}, 0
return
}
# count #pieces we need room for
count:number <- copy 1 # n delimiters = n+1 pieces
idx:number <- copy 0
{
idx <- find-next s, delim, idx
done?:boolean <- greater-or-equal idx, len
break-if done?
idx <- add idx, 1
count <- add count, 1
loop
}
# allocate space
result <- new {(address array character): type}, count
# repeatedly copy slices start..end until delimiter into result[curr-result]
curr-result:number <- copy 0
start:number <- copy 0
{
# while next delim exists
done?:boolean <- greater-or-equal start, len
break-if done?
end:number <- find-next s, delim, start
# copy start..end into result[curr-result]
dest:address:array:character <- copy-range s, start, end
*result <- put-index *result, curr-result, dest
# slide over to next slice
start <- add end, 1
curr-result <- add curr-result, 1
loop
}
]
scenario text-split-1 [
run [
1:address:array:character <- new [a/b]
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
3:number <- length *2:address:array:address:array:character
4:address:array:character <- index *2:address:array:address:array:character, 0
5:address:array:character <- index *2:address:array:address:array:character, 1
10:array:character <- copy *4:address:array:character
20:array:character <- copy *5:address:array:character
]
memory-should-contain [
3 <- 2 # length of result
10:array:character <- [a]
20:array:character <- [b]
]
]
scenario text-split-2 [
run [
1:address:array:character <- new [a/b/c]
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
3:number <- length *2:address:array:address:array:character
4:address:array:character <- index *2:address:array:address:array:character, 0
5:address:array:character <- index *2:address:array:address:array:character, 1
6:address:array:character <- index *2:address:array:address:array:character, 2
10:array:character <- copy *4:address:array:character
20:array:character <- copy *5:address:array:character
30:array:character <- copy *6:address:array:character
]
memory-should-contain [
3 <- 3 # length of result
10:array:character <- [a]
20:array:character <- [b]
30:array:character <- [c]
]
]
scenario text-split-missing [
run [
1:address:array:character <- new [abc]
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
3:number <- length *2:address:array:address:array:character
4:address:array:character <- index *2:address:array:address:array:character, 0
10:array:character <- copy *4:address:array:character
]
memory-should-contain [
3 <- 1 # length of result
10:array:character <- [abc]
]
]
scenario text-split-empty [
run [
1:address:array:character <- new []
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
3:number <- length *2:address:array:address:array:character
]
memory-should-contain [
3 <- 0 # empty result
]
]
scenario text-split-empty-piece [
run [
1:address:array:character <- new [a/b//c]
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
3:number <- length *2:address:array:address:array:character
4:address:array:character <- index *2:address:array:address:array:character, 0
5:address:array:character <- index *2:address:array:address:array:character, 1
6:address:array:character <- index *2:address:array:address:array:character, 2
7:address:array:character <- index *2:address:array:address:array:character, 3
10:array:character <- copy *4:address:array:character
20:array:character <- copy *5:address:array:character
30:array:character <- copy *6:address:array:character
40:array:character <- copy *7:address:array:character
]
memory-should-contain [
3 <- 4 # length of result
10:array:character <- [a]
20:array:character <- [b]
30:array:character <- []
40:array:character <- [c]
]
]
def split-first text:address:array:character, delim:character -> x:address:array:character, y:address:array:character [
local-scope
load-ingredients
# empty text? return empty texts
len:number <- length *text
{
empty?:boolean <- equal len, 0
break-unless empty?
x:address:array:character <- new []
y:address:array:character <- new []
return
}
idx:number <- find-next text, delim, 0
x:address:array:character <- copy-range text, 0, idx
idx <- add idx, 1
y:address:array:character <- copy-range text, idx, len
]
scenario text-split-first [
run [
1:address:array:character <- new [a/b]
2:address:array:character, 3:address:array:character <- split-first 1:address:array:character, 47/slash
10:array:character <- copy *2:address:array:character
20:array:character <- copy *3:address:array:character
]
memory-should-contain [
10:array:character <- [a]
20:array:character <- [b]
]
]
def copy-range buf:address:array:character, start:number, end:number -> result:address:array:character [
local-scope
load-ingredients
# if end is out of bounds, trim it
len:number <- length *buf
end:number <- min len, end
# allocate space for result
len <- subtract end, start
result:address:array:character <- new character:type, len
# copy start..end into result[curr-result]
src-idx:number <- copy start
dest-idx:number <- copy 0
{
done?:boolean <- greater-or-equal src-idx, end
break-if done?
src:character <- index *buf, src-idx
*result <- put-index *result, dest-idx, src
src-idx <- add src-idx, 1
dest-idx <- add dest-idx, 1
loop
}
]
scenario text-copy-copies-partial-text [
run [
1:address:array:character <- new [abc]
2:address:array:character <- copy-range 1:address:array:character, 1, 3
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [bc]
]
]
scenario text-copy-out-of-bounds [
run [
1:address:array:character <- new [abc]
2:address:array:character <- copy-range 1:address:array:character, 2, 4
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- [c]
]
]
scenario text-copy-out-of-bounds-2 [
run [
1:address:array:character <- new [abc]
2:address:array:character <- copy-range 1:address:array:character, 3, 3
3:array:character <- copy *2:address:array:character
]
memory-should-contain [
3:array:character <- []
]
]
|