-
Notifications
You must be signed in to change notification settings - Fork 17
/
list.cc
1349 lines (1158 loc) · 29.7 KB
/
list.cc
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
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include "my-ctype.h"
#include "my-string.h"
#include "bf_register.h"
#include "collection.h"
#include "config.h"
#include "functions.h"
#include "list.h"
#include "log.h"
#include "map.h"
#include "options.h"
#include "pattern.h"
#include "streams.h"
#include "storage.h"
#include "structures.h"
#include "unparse.h"
#include "utils.h"
#include "server.h"
Var
new_list(int size)
{
Var list;
Var *ptr;
if (size == 0) {
static Var emptylist;
if (emptylist.v.list == NULL) {
if ((ptr = (Var *)mymalloc(1 * sizeof(Var), M_LIST)) == NULL)
panic("EMPTY_LIST: mymalloc failed");
emptylist.type = TYPE_LIST;
emptylist.v.list = ptr;
emptylist.v.list[0].type = TYPE_INT;
emptylist.v.list[0].v.num = 0;
}
#ifdef ENABLE_GC
assert(gc_get_color(emptylist.v.list) == GC_GREEN);
#endif
addref(emptylist.v.list);
return emptylist;
}
if ((ptr = (Var *)mymalloc((size + 1) * sizeof(Var), M_LIST)) == NULL)
panic("EMPTY_LIST: mymalloc failed");
list.type = TYPE_LIST;
list.v.list = ptr;
list.v.list[0].type = TYPE_INT;
list.v.list[0].v.num = size;
#ifdef ENABLE_GC
gc_set_color(list.v.list, GC_YELLOW);
#endif
return list;
}
/* called from utils.c */
void
destroy_list(Var list)
{
int i;
Var *pv;
for (i = list.v.list[0].v.num, pv = list.v.list + 1; i > 0; i--, pv++)
free_var(*pv);
/* Since this list could possibly be the root of a cycle, final
* destruction is handled in the garbage collector if garbage
* collection is enabled.
*/
#ifndef ENABLE_GC
myfree(list.v.list, M_LIST);
#endif
}
/* called from utils.c */
Var
list_dup(Var list)
{
int i, n = list.v.list[0].v.num;
Var _new = new_list(n);
for (i = 1; i <= n; i++)
_new.v.list[i] = var_ref(list.v.list[i]);
gc_set_color(_new.v.list, gc_get_color(list.v.list));
return _new;
}
int
listforeach(Var list, listfunc func, void *data)
{ /* does NOT consume `list' */
int i, n;
int first = 1;
int ret;
for (i = 1, n = list.v.list[0].v.num; i <= n; i++) {
if ((ret = (*func)(list.v.list[i], data, first)))
return ret;
first = 0;
}
return 0;
}
Var
setadd(Var list, Var value)
{
if (ismember(value, list, 0)) {
free_var(value);
return list;
}
return listappend(list, value);
}
Var
setremove(Var list, Var value)
{
int i;
if ((i = ismember(value, list, 0)) != 0) {
return listdelete(list, i);
} else {
return list;
}
}
Var
listset(Var list, Var value, int pos)
{ /* consumes `list', `value' */
Var _new = list;
if (var_refcount(list) > 1) {
_new = var_dup(list);
free_var(list);
}
#ifdef MEMO_VALUE_BYTES
/* reset the memoized size */
((int *)(_new.v.list))[-2] = 0;
#endif
free_var(_new.v.list[pos]);
_new.v.list[pos] = value;
#ifdef ENABLE_GC
gc_set_color(_new.v.list, GC_YELLOW);
#endif
return _new;
}
static Var
doinsert(Var list, Var value, int pos)
{
Var _new;
int i;
int size = list.v.list[0].v.num + 1;
if (var_refcount(list) == 1 && pos == size) {
list.v.list = (Var *) myrealloc(list.v.list, (size + 1) * sizeof(Var), M_LIST);
#ifdef MEMO_VALUE_BYTES
/* reset the memoized size */
((int *)(list.v.list))[-2] = 0;
#endif
list.v.list[0].v.num = size;
list.v.list[pos] = value;
#ifdef ENABLE_GC
gc_set_color(list.v.list, GC_YELLOW);
#endif
return list;
}
_new = new_list(size);
for (i = 1; i < pos; i++)
_new.v.list[i] = var_ref(list.v.list[i]);
_new.v.list[pos] = value;
for (i = pos; i <= list.v.list[0].v.num; i++)
_new.v.list[i + 1] = var_ref(list.v.list[i]);
free_var(list);
#ifdef ENABLE_GC
gc_set_color(_new.v.list, GC_YELLOW);
#endif
return _new;
}
Var
listinsert(Var list, Var value, int pos)
{
if (pos <= 0)
pos = 1;
else if (pos > list.v.list[0].v.num)
pos = list.v.list[0].v.num + 1;
return doinsert(list, value, pos);
}
Var
listappend(Var list, Var value)
{
return doinsert(list, value, list.v.list[0].v.num + 1);
}
Var
listdelete(Var list, int pos)
{
Var _new;
int i;
int size = list.v.list[0].v.num - 1;
_new = new_list(size);
for (i = 1; i < pos; i++) {
_new.v.list[i] = var_ref(list.v.list[i]);
}
for (i = pos + 1; i <= list.v.list[0].v.num; i++)
_new.v.list[i - 1] = var_ref(list.v.list[i]);
free_var(list);
#ifdef ENABLE_GC
if (size > 0) /* only non-empty lists */
gc_set_color(_new.v.list, GC_YELLOW);
#endif
return _new;
}
Var
listconcat(Var first, Var second)
{
int lsecond = second.v.list[0].v.num;
int lfirst = first.v.list[0].v.num;
Var _new;
int i;
_new = new_list(lsecond + lfirst);
for (i = 1; i <= lfirst; i++)
_new.v.list[i] = var_ref(first.v.list[i]);
for (i = 1; i <= lsecond; i++)
_new.v.list[i + lfirst] = var_ref(second.v.list[i]);
free_var(first);
free_var(second);
#ifdef ENABLE_GC
if (lsecond + lfirst > 0) /* only non-empty lists */
gc_set_color(_new.v.list, GC_YELLOW);
#endif
return _new;
}
Var
listrangeset(Var base, int from, int to, Var value)
{
/* base and value are free'd */
int index, offset = 0;
int val_len = value.v.list[0].v.num;
int base_len = base.v.list[0].v.num;
int lenleft = (from > 1) ? from - 1 : 0;
int lenmiddle = val_len;
int lenright = (base_len > to) ? base_len - to : 0;
int newsize = lenleft + lenmiddle + lenright;
Var ans;
ans = new_list(newsize);
for (index = 1; index <= lenleft; index++)
ans.v.list[++offset] = var_ref(base.v.list[index]);
for (index = 1; index <= lenmiddle; index++)
ans.v.list[++offset] = var_ref(value.v.list[index]);
for (index = 1; index <= lenright; index++)
ans.v.list[++offset] = var_ref(base.v.list[to + index]);
free_var(base);
free_var(value);
#ifdef ENABLE_GC
if (newsize > 0) /* only non-empty lists */
gc_set_color(ans.v.list, GC_YELLOW);
#endif
return ans;
}
Var
sublist(Var list, int lower, int upper)
{
if (lower > upper) {
free_var(list);
return new_list(0);
} else {
Var r;
int i;
r = new_list(upper - lower + 1);
for (i = lower; i <= upper; i++)
r.v.list[i - lower + 1] = var_ref(list.v.list[i]);
free_var(list);
#ifdef ENABLE_GC
gc_set_color(r.v.list, GC_YELLOW);
#endif
return r;
}
}
int
listequal(Var lhs, Var rhs, int case_matters)
{
if (lhs.v.list == rhs.v.list)
return 1;
if (lhs.v.list[0].v.num != rhs.v.list[0].v.num)
return 0;
int i, c = lhs.v.list[0].v.num;
for (i = 1; i <= c; i++) {
if (!equality(lhs.v.list[i], rhs.v.list[i], case_matters))
return 0;
}
return 1;
}
static void
stream_add_tostr(Stream * s, Var v)
{
switch (v.type) {
case TYPE_INT:
stream_printf(s, "%d", v.v.num);
break;
case TYPE_OBJ:
stream_printf(s, "#%d", v.v.obj);
break;
case TYPE_STR:
stream_add_string(s, v.v.str);
break;
case TYPE_ERR:
stream_add_string(s, unparse_error(v.v.err));
break;
case TYPE_FLOAT:
stream_printf(s, "%g", *v.v.fnum);
break;
case TYPE_MAP:
stream_add_string(s, "[map]");
break;
case TYPE_LIST:
stream_add_string(s, "{list}");
break;
case TYPE_ANON:
stream_add_string(s, "*anonymous*");
break;
default:
panic("STREAM_ADD_TOSTR: Unknown Var type");
}
}
const char *
value2str(Var value)
{
if (value.type == TYPE_STR) {
/* do this case separately to avoid two copies
* and to ensure that the stream never grows */
return str_ref(value.v.str);
}
else {
static Stream *s = 0;
if (!s)
s = new_stream(32);
stream_add_tostr(s, value);
return str_dup(reset_stream(s));
}
}
static int
print_map_to_stream(Var key, Var value, void *sptr, int first)
{
Stream *s = (Stream *)sptr;
if (!first) {
stream_add_string(s, ", ");
}
unparse_value(s, key);
stream_add_string(s, " -> ");
unparse_value(s, value);
return 0;
}
void
unparse_value(Stream * s, Var v)
{
switch (v.type) {
case TYPE_INT:
stream_printf(s, "%d", v.v.num);
break;
case TYPE_OBJ:
stream_printf(s, "#%d", v.v.obj);
break;
case TYPE_ERR:
stream_add_string(s, error_name(v.v.err));
break;
case TYPE_FLOAT:
stream_printf(s, "%g", *v.v.fnum);
break;
case TYPE_STR:
{
const char *str = v.v.str;
stream_add_char(s, '"');
while (*str) {
switch (*str) {
case '"':
case '\\':
stream_add_char(s, '\\');
/* fall thru */
default:
stream_add_char(s, *str++);
}
}
stream_add_char(s, '"');
}
break;
case TYPE_LIST:
{
const char *sep = "";
int len, i;
stream_add_char(s, '{');
len = v.v.list[0].v.num;
for (i = 1; i <= len; i++) {
stream_add_string(s, sep);
sep = ", ";
unparse_value(s, v.v.list[i]);
}
stream_add_char(s, '}');
}
break;
case TYPE_MAP:
{
stream_add_char(s, '[');
mapforeach(v, print_map_to_stream, (void *)s);
stream_add_char(s, ']');
}
break;
case TYPE_ANON:
stream_add_string(s, "*anonymous*");
break;
default:
errlog("UNPARSE_VALUE: Unknown Var type = %d\n", v.type);
stream_add_string(s, ">>Unknown value<<");
}
}
/* called from utils.c */
int
list_sizeof(Var *list)
{
int i, len, size;
#ifdef MEMO_VALUE_BYTES
if ((size = (((int *)(list))[-2])))
return size;
#endif
size = sizeof(Var); /* for the `length' element */
len = list[0].v.num;
for (i = 1; i <= len; i++) {
size += value_bytes(list[i]);
}
#ifdef MEMO_VALUE_BYTES
(((int *)(list))[-2]) = size;
#endif
return size;
}
Var
strrangeset(Var base, int from, int to, Var value)
{
/* base and value are free'd */
int index, offset = 0;
int val_len = memo_strlen(value.v.str);
int base_len = memo_strlen(base.v.str);
int lenleft = (from > 1) ? from - 1 : 0;
int lenmiddle = val_len;
int lenright = (base_len > to) ? base_len - to : 0;
int newsize = lenleft + lenmiddle + lenright;
Var ans;
char *s;
ans.type = TYPE_STR;
s = (char *)mymalloc(sizeof(char) * (newsize + 1), M_STRING);
for (index = 0; index < lenleft; index++)
s[offset++] = base.v.str[index];
for (index = 0; index < lenmiddle; index++)
s[offset++] = value.v.str[index];
for (index = 0; index < lenright; index++)
s[offset++] = base.v.str[index + to];
s[offset] = '\0';
ans.v.str = s;
free_var(base);
free_var(value);
return ans;
}
Var
substr(Var str, int lower, int upper)
{
Var r;
r.type = TYPE_STR;
if (lower > upper)
r.v.str = str_dup("");
else {
int loop, index = 0;
char *s = (char *)mymalloc(upper - lower + 2, M_STRING);
for (loop = lower - 1; loop < upper; loop++)
s[index++] = str.v.str[loop];
s[index] = '\0';
r.v.str = s;
}
free_var(str);
return r;
}
Var
strget(Var str, int i)
{
Var r;
char *s;
r.type = TYPE_STR;
s = str_dup(" ");
s[0] = str.v.str[i - 1];
r.v.str = s;
return r;
}
/**** helpers for catching overly large allocations ****/
#define TRY_STREAM enable_stream_exceptions()
#define ENDTRY_STREAM disable_stream_exceptions()
static package
make_space_pack()
{
if (server_flag_option_cached(SVO_MAX_CONCAT_CATCHABLE))
return make_error_pack(E_QUOTA);
else
return make_abort_pack(ABORT_SECONDS);
}
/**** built in functions ****/
static package
bf_length(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
switch (arglist.v.list[1].type) {
case TYPE_LIST:
r.type = TYPE_INT;
r.v.num = arglist.v.list[1].v.list[0].v.num;
break;
case TYPE_MAP:
r.type = TYPE_INT;
r.v.num = maplength(arglist.v.list[1]);
break;
case TYPE_STR:
r.type = TYPE_INT;
r.v.num = memo_strlen(arglist.v.list[1].v.str);
break;
default:
free_var(arglist);
return make_error_pack(E_TYPE);
break;
}
free_var(arglist);
return make_var_pack(r);
}
static package
bf_setadd(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
Var lst = var_ref(arglist.v.list[1]);
Var elt = var_ref(arglist.v.list[2]);
free_var(arglist);
r = setadd(lst, elt);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))
return make_var_pack(r);
else {
free_var(r);
return make_space_pack();
}
}
static package
bf_setremove(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
r = setremove(var_ref(arglist.v.list[1]), arglist.v.list[2]);
free_var(arglist);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))
return make_var_pack(r);
else {
free_var(r);
return make_space_pack();
}
}
static package
insert_or_append(Var arglist, int append1)
{
int pos;
Var r;
Var lst = var_ref(arglist.v.list[1]);
Var elt = var_ref(arglist.v.list[2]);
if (arglist.v.list[0].v.num == 2)
pos = append1 ? lst.v.list[0].v.num + 1 : 1;
else {
pos = arglist.v.list[3].v.num + append1;
if (pos <= 0)
pos = 1;
else if (pos > lst.v.list[0].v.num + 1)
pos = lst.v.list[0].v.num + 1;
}
free_var(arglist);
r = doinsert(lst, elt, pos);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))
return make_var_pack(r);
else {
free_var(r);
return make_space_pack();
}
}
static package
bf_listappend(Var arglist, Byte next, void *vdata, Objid progr)
{
return insert_or_append(arglist, 1);
}
static package
bf_listinsert(Var arglist, Byte next, void *vdata, Objid progr)
{
return insert_or_append(arglist, 0);
}
static package
bf_listdelete(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
if (arglist.v.list[2].v.num <= 0
|| arglist.v.list[2].v.num > arglist.v.list[1].v.list[0].v.num) {
free_var(arglist);
return make_error_pack(E_RANGE);
}
r = listdelete(var_ref(arglist.v.list[1]), arglist.v.list[2].v.num);
free_var(arglist);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))
return make_var_pack(r);
else {
free_var(r);
return make_space_pack();
}
}
static package
bf_listset(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
Var lst = var_ref(arglist.v.list[1]);
Var elt = var_ref(arglist.v.list[2]);
int pos = arglist.v.list[3].v.num;
free_var(arglist);
if (pos <= 0 || pos > listlength(lst))
return make_error_pack(E_RANGE);
r = listset(lst, elt, pos);
if (value_bytes(r) <= server_int_option_cached(SVO_MAX_LIST_VALUE_BYTES))
return make_var_pack(r);
else {
free_var(r);
return make_space_pack();
}
}
static package
bf_equal(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
r.type = TYPE_INT;
r.v.num = equality(arglist.v.list[1], arglist.v.list[2], 1);
free_var(arglist);
return make_var_pack(r);
}
static package
bf_strsub(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (source, what, with [, case-matters]) */
int case_matters = 0;
Stream *s;
package p;
if (arglist.v.list[0].v.num == 4)
case_matters = is_true(arglist.v.list[4]);
if (arglist.v.list[2].v.str[0] == '\0') {
free_var(arglist);
return make_error_pack(E_INVARG);
}
s = new_stream(100);
TRY_STREAM;
try {
Var r;
stream_add_strsub(s, arglist.v.list[1].v.str, arglist.v.list[2].v.str,
arglist.v.list[3].v.str, case_matters);
r.type = TYPE_STR;
r.v.str = str_dup(stream_contents(s));
p = make_var_pack(r);
}
catch (stream_too_big& exception) {
p = make_space_pack();
}
ENDTRY_STREAM;
free_stream(s);
free_var(arglist);
return p;
}
static int
signum(int x)
{
return x < 0 ? -1 : (x > 0 ? 1 : 0);
}
static package
bf_strcmp(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (string1, string2) */
Var r;
r.type = TYPE_INT;
r.v.num = signum(strcmp(arglist.v.list[1].v.str, arglist.v.list[2].v.str));
free_var(arglist);
return make_var_pack(r);
}
static package
bf_strtr(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (subject, from, to [, case_matters]) */
Var r;
int case_matters = 0;
if (arglist.v.list[0].v.num > 3)
case_matters = is_true(arglist.v.list[4]);
r.type = TYPE_STR;
r.v.str = str_dup(strtr(arglist.v.list[1].v.str, memo_strlen(arglist.v.list[1].v.str),
arglist.v.list[2].v.str, memo_strlen(arglist.v.list[2].v.str),
arglist.v.list[3].v.str, memo_strlen(arglist.v.list[3].v.str),
case_matters));
free_var(arglist);
return make_var_pack(r);
}
static package
bf_index(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (source, what [, case-matters [, offset]]) */
Var r;
int case_matters = 0;
int offset = 0;
if (arglist.v.list[0].v.num > 2)
case_matters = is_true(arglist.v.list[3]);
if (arglist.v.list[0].v.num > 3)
offset = arglist.v.list[4].v.num;
if (offset < 0) {
free_var(arglist);
return make_error_pack(E_INVARG);
}
r.type = TYPE_INT;
r.v.num = strindex(arglist.v.list[1].v.str + offset, memo_strlen(arglist.v.list[1].v.str) - offset,
arglist.v.list[2].v.str, memo_strlen(arglist.v.list[2].v.str),
case_matters);
free_var(arglist);
return make_var_pack(r);
}
static package
bf_rindex(Var arglist, Byte next, void *vdata, Objid progr)
{ /* (source, what [, case-matters [, offset]]) */
Var r;
int case_matters = 0;
int offset = 0;
if (arglist.v.list[0].v.num > 2)
case_matters = is_true(arglist.v.list[3]);
if (arglist.v.list[0].v.num > 3)
offset = arglist.v.list[4].v.num;
if (offset > 0) {
free_var(arglist);
return make_error_pack(E_INVARG);
}
r.type = TYPE_INT;
r.v.num = strrindex(arglist.v.list[1].v.str, memo_strlen(arglist.v.list[1].v.str) + offset,
arglist.v.list[2].v.str, memo_strlen(arglist.v.list[2].v.str),
case_matters);
free_var(arglist);
return make_var_pack(r);
}
static package
bf_tostr(Var arglist, Byte next, void *vdata, Objid progr)
{
package p;
Stream *s = new_stream(100);
TRY_STREAM;
try {
Var r;
int i;
for (i = 1; i <= arglist.v.list[0].v.num; i++) {
stream_add_tostr(s, arglist.v.list[i]);
}
r.type = TYPE_STR;
r.v.str = str_dup(stream_contents(s));
p = make_var_pack(r);
}
catch (stream_too_big& exception) {
p = make_space_pack();
}
ENDTRY_STREAM;
free_stream(s);
free_var(arglist);
return p;
}
static package
bf_toliteral(Var arglist, Byte next, void *vdata, Objid progr)
{
package p;
Stream *s = new_stream(100);
TRY_STREAM;
try {
Var r;
unparse_value(s, arglist.v.list[1]);
r.type = TYPE_STR;
r.v.str = str_dup(stream_contents(s));
p = make_var_pack(r);
}
catch (stream_too_big& exception) {
p = make_space_pack();
}
ENDTRY_STREAM;
free_stream(s);
free_var(arglist);
return p;
}
struct pat_cache_entry {
char *string;
int case_matters;
Pattern pattern;
struct pat_cache_entry *next;
};
static struct pat_cache_entry *pat_cache;
static struct pat_cache_entry pat_cache_entries[PATTERN_CACHE_SIZE];
static void
setup_pattern_cache()
{
int i;
for (i = 0; i < PATTERN_CACHE_SIZE; i++) {
pat_cache_entries[i].string = 0;
pat_cache_entries[i].pattern.ptr = 0;
}
for (i = 0; i < PATTERN_CACHE_SIZE - 1; i++)
pat_cache_entries[i].next = &(pat_cache_entries[i + 1]);
pat_cache_entries[PATTERN_CACHE_SIZE - 1].next = 0;
pat_cache = &(pat_cache_entries[0]);
}
static Pattern
get_pattern(const char *string, int case_matters)
{
struct pat_cache_entry *entry, **entry_ptr;
entry = pat_cache;
entry_ptr = &pat_cache;
while (1) {
if (entry->string && !strcmp(string, entry->string)
&& case_matters == entry->case_matters) {
/* A cache hit; move this entry to the front of the cache. */
break;
} else if (!entry->next) {
/* A cache miss; this is the last entry in the cache, so reuse that
* one for this pattern, moving it to the front of the cache iff
* the compilation succeeds.
*/
if (entry->string) {
free_str(entry->string);
free_pattern(entry->pattern);
}
entry->pattern = new_pattern(string, case_matters);
entry->case_matters = case_matters;
if (!entry->pattern.ptr)
entry->string = 0;
else
entry->string = str_dup(string);
break;
} else {
/* not done searching the cache... */
entry_ptr = &(entry->next);
entry = entry->next;
}
}
*entry_ptr = entry->next;
entry->next = pat_cache;
pat_cache = entry;
return entry->pattern;
}
Var
do_match(Var arglist, int reverse)
{
const char *subject, *pattern;
int i;
Pattern pat;
Var ans;