-
Notifications
You must be signed in to change notification settings - Fork 176
/
ops.c
2907 lines (2688 loc) · 125 KB
/
ops.c
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
#include "platform/memmem.h"
#include "platform/memmem32.h"
#include "moar.h"
#define MVM_DEBUG_STRANDS 0
#define MVM_string_KMP_max_pattern_length 8192
/* Max value possible for MVMuint32 MVMStringBody.num_graphs */
#define MAX_GRAPHEMES 0xFFFFFFFFLL
#if MVM_DEBUG_STRANDS
static void check_strand_sanity(MVMThreadContext *tc, MVMString *s) {
MVMGraphemeIter gi;
MVMuint32 len;
MVM_string_gi_init(tc, &gi, s);
len = 0;
while (MVM_string_gi_has_more(tc, &gi)) {
MVM_string_gi_get_grapheme(tc, &gi);
len++;
}
if (len != MVM_string_graphs(tc, s))
MVM_exception_throw_adhoc(tc,
"Strand sanity check failed (strand length %d != num_graphs %d)",
len, MVM_string_graphs(tc, s));
}
#define STRAND_CHECK(tc, s) check_strand_sanity(tc, s);
#else
#define STRAND_CHECK(tc, s)
#endif
static MVMString * re_nfg(MVMThreadContext *tc, MVMString *in);
#if MVM_DEBUG_NFG
static char * NFG_check_make_debug_string (MVMThreadContext *tc, MVMGrapheme32 g) {
char *result = NULL;
char *picked = NULL;
if (g == '\r')
picked = "\\r";
else if (g == '\n')
picked = "\\n";
else if (g == MVM_nfg_crlf_grapheme(tc))
picked = "\\r\\n";
else if (0 <= g && !MVM_string_is_control_full(tc, g))
result = MVM_string_utf8_encode_C_string(tc, MVM_string_chr(tc, g));
else if (g < 0) {
MVMNFGSynthetic *synth = MVM_nfg_get_synthetic_info(tc, g);
char *format_str = " with num_codes = ";
char *format_str2 = " first, second cp = ";
char *synthtype_str = synth->is_utf8_c8 ? "utf8-8 Synthetic" : "Normal Synthetic";
int this_len = strlen(format_str) + strlen(synthtype_str) + 6 + strlen(format_str2) + 11 + 1 + 11 + 1;
result = MVM_malloc(this_len);
if (2 <= synth->num_codes)
sprintf(result, "%s%s%5i%s%.10"PRIi32",%.10"PRIi32"", synthtype_str, format_str, synth->num_codes, format_str2, synth->codes[0], synth->codes[1]);
else
sprintf(result, "WARNING synth has less than 2 codes");
fprintf(stderr, "synth numcodes %i %"PRIi32"\n",
MVM_nfg_get_synthetic_info(tc, synth->codes[1])->num_codes, MVM_nfg_get_synthetic_info(tc, synth->codes[1])->codes[0]
);
}
else
picked = "[Control]";
if (picked) {
result = MVM_malloc(sizeof(char) * (strlen(picked) + 1));
strcpy(result, picked);
}
if (!result) {
result = MVM_malloc(sizeof(char) * 1);
result[0] = 0;
}
return result;
}
static char * NFG_checker (MVMThreadContext *tc, MVMString *orig, char *varname);
void NFG_check (MVMThreadContext *tc, MVMString *orig, char *varname) {
char *out = NFG_checker(tc, orig, varname);
char *waste[2] = { out, NULL };
if (!out)
return;
MVM_exception_throw_adhoc_free(tc, waste, "%s", out);
}
static char * NFG_checker (MVMThreadContext *tc, MVMString *orig, char *varname) {
MVMString *renorm = NULL;
MVMStringIndex orig_graphs = MVM_string_graphs(tc, orig),
renorm_graphs = -1;
MVMROOT2(tc, orig, renorm, {
renorm = re_nfg(tc, orig);
renorm_graphs = MVM_string_graphs(tc, renorm);
});
if (MVM_DEBUG_NFG_STRICT || orig_graphs != renorm_graphs) {
MVMGraphemeIter orig_gi, renorm_gi;
MVMint64 index = 0;
MVM_string_gi_init(tc, &orig_gi, orig);
MVM_string_gi_init(tc, &renorm_gi, renorm);
while (MVM_string_gi_has_more(tc, &orig_gi) && MVM_string_gi_has_more(tc, &renorm_gi)) {
MVMGrapheme32 orig_g = MVM_string_gi_get_grapheme(tc, &orig_gi),
renorm_g = MVM_string_gi_get_grapheme(tc, &renorm_gi);
if (orig_g != renorm_g) {
char *orig_render = NFG_check_make_debug_string(tc, orig_g),
*renorm_render = NFG_check_make_debug_string(tc, renorm_g);
char *format = "NFG failure. Got different grapheme count of %s. "
"Got %i but after re_nfg got %i\n"
"Differing grapheme at index %"PRIi64"\n"
"orig: %"PRIi32" (%s) after re_nfg: %"PRIi32" (%s)\n";
int out_size = strlen(orig_render) + strlen(renorm_render)
+ strlen(varname) + strlen(format) + (5 * 7) + 1;
char *out = MVM_malloc(sizeof(char) * out_size);
char *waste[] = {orig_render, renorm_render, NULL};
char **w = waste;
snprintf(out, out_size,
format,
varname,
orig_graphs, renorm_graphs,
index,
orig_g, orig_render, renorm_g, renorm_render);
MVM_free(orig_render);
MVM_free(renorm_render);
return out;
}
index++;
}
}
return NULL;
}
void NFG_check_concat (MVMThreadContext *tc, MVMString *result, MVMString *a, MVMString *b, char *varname) {
char *a_out = NFG_checker(tc, a, "string ‘a’");
char *b_out = NFG_checker(tc, b, "string ‘b’");
char *out = NFG_checker(tc, result, varname);
char *strings[] = { a_out, b_out, out };
char *names[] = { "\nconcat string ‘a’: ", "\nconcat string ‘b’: ", "\nconcat result: " };
int i = 0, elems = 4;
int rtrn = 0;
char * empty = "";
if (!a_out && !b_out && !out)
return;
else {
MVMGrapheme32 last_a = MVM_string_get_grapheme_at_nocheck(tc, a, a->body.num_graphs - 1),
first_b = MVM_string_get_grapheme_at_nocheck(tc, b, 0);
char *debug_a = NFG_check_make_debug_string(tc, last_a),
*debug_b = NFG_check_make_debug_string(tc, first_b),
*escaped_a = MVM_string_utf8_encode_C_string(tc, MVM_string_escape(tc, a)),
*escaped_b = MVM_string_utf8_encode_C_string(tc, MVM_string_escape(tc, b)),
*escaped_result = MVM_string_utf8_encode_C_string(tc, MVM_string_escape(tc, result));
char *waste[] = { out, debug_a, debug_b, escaped_a, escaped_b, escaped_result, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"In concat: a graphs: %"PRIi32" b graphs: %"PRIi32"\n"
"last_a: %"PRIi32" (%s) first_b %"PRIi32" (%s)\n"
"a: “%s”\n"
"b: “%s”\n"
"result: “%s”\n"
"%s%s%s%s%s%s",
MVM_string_graphs(tc, a), MVM_string_graphs(tc, b),
last_a, debug_a, first_b, debug_b,
escaped_a,
escaped_b,
escaped_result,
(a_out?names[0]:""), (a_out?a_out:""),
(b_out?names[1]:""), (b_out?b_out:""),
(out?names[2]:""), (out?out:""));
}
}
#endif
MVM_STATIC_INLINE MVMint64 string_equal_at_ignore_case_INTERNAL_loop(MVMThreadContext *tc, void *Hs_or_gic, MVMString *needle_fc, MVMint64 H_start, MVMint64 H_graphs, MVMint64 n_fc_graphs, int ignoremark, int ignorecase, int is_gic);
static MVMint64 knuth_morris_pratt_string_index (MVMThreadContext *tc, MVMString *needle, MVMString *Haystack, MVMint64 H_offset);
/* Allocates strand storage. */
static MVMStringStrand * allocate_strands(MVMThreadContext *tc, MVMuint16 num_strands) {
return MVM_malloc(num_strands * sizeof(MVMStringStrand));
}
/* Copies strands from one strand string to another. */
static void copy_strands(MVMThreadContext *tc, const MVMString *from, MVMuint16 from_offset,
MVMString *to, MVMuint16 to_offset, MVMuint16 num_strands) {
assert(from->body.storage_type == MVM_STRING_STRAND);
assert(to->body.storage_type == MVM_STRING_STRAND);
memcpy(
to->body.storage.strands + to_offset,
from->body.storage.strands + from_offset,
num_strands * sizeof(MVMStringStrand));
}
/* Move strands inside the same strand string. */
static void move_strands(MVMThreadContext *tc, const MVMString *from, MVMuint16 from_offset,
MVMString *to, MVMuint16 to_offset, MVMuint16 num_strands) {
assert(from->body.storage_type == MVM_STRING_STRAND);
assert(to->body.storage_type == MVM_STRING_STRAND);
memmove(
to->body.storage.strands + to_offset,
from->body.storage.strands + from_offset,
num_strands * sizeof(MVMStringStrand));
}
#define can_fit_into_8bit(g) ((-128 <= (g) && (g) <= 127))
MVM_STATIC_INLINE int can_fit_into_ascii (MVMGrapheme32 g) {
return 0 <= g && g <= 127;
}
/* If a string is currently using 32bit storage, turn it into using
* 8 bit storage. Doesn't do any checks at all. */
static void turn_32bit_into_8bit_unchecked(MVMThreadContext *tc, MVMString *str) {
MVMGrapheme32 *old_buf = str->body.storage.blob_32;
MVMStringIndex i;
MVMGrapheme8 *dest_buf = NULL;
MVMStringIndex num_graphs = MVM_string_graphs_nocheck(tc, str);
str->body.storage_type = MVM_STRING_GRAPHEME_8;
dest_buf = str->body.storage.blob_8 = MVM_malloc(str->body.num_graphs * sizeof(MVMGrapheme8));
MVM_VECTORIZE_LOOP
for (i = 0; i < num_graphs; i++) {
dest_buf[i] = old_buf[i];
}
MVM_free(old_buf);
}
/* Checks if the next num_graphs graphemes in the iterator can fit into 8 bits.
* This was written to take advantage of SIMD vectorization, so we use a multiple
* bitwise operations to check, and biwise OR it with val. Care must be taken
* to not use any variables altered by the loop outside of the loop and to not
* have any branching or funcion calls. `i` is not used outside the loop
* `val` is allowed as biwise OR works with the vectorization well.
* NOTE: GraphemeIter is not modified by this function. */
static int string_can_be_8bit(MVMThreadContext *tc, MVMGraphemeIter *gi_orig, MVMStringIndex num_graphs) {
MVMStringIndex pos = 0;
MVMGraphemeIter gi;
memcpy(&gi, gi_orig, sizeof(MVMGraphemeIter));
while (1) {
MVMStringIndex strand_len = MVM_string_gi_graphs_left_in_strand(tc, &gi);
MVMStringIndex togo = num_graphs - pos < strand_len
? num_graphs - pos
: strand_len;
if (MVM_string_gi_blob_type(tc, &gi) == MVM_STRING_GRAPHEME_32) {
if (!MVM_string_buf32_can_fit_into_8bit(MVM_string_gi_active_blob_32_pos(tc, &gi), togo))
return 0;
}
pos += togo;
if (num_graphs == pos || !MVM_string_gi_has_more_strands_rep(tc, &gi)) {
break;
}
MVM_string_gi_next_strand_rep(tc, &gi);
}
return 1;
}
/* Accepts an allocated string that should have body.num_graphs set but the blob
* unallocated. This function will allocate the space for the blob and iterate
* the supplied grapheme iterator for the length of body.num_graphs. Very fast
* since compilers will convert them to SIMD vector operations. */
static void iterate_gi_into_string(MVMThreadContext *tc, MVMGraphemeIter *gi, MVMString *result, MVMString *orig, MVMStringIndex num) {
int result_pos = 0;
MVMGrapheme8 *result8 = NULL;
MVMGrapheme32 *result32 = NULL;
MVMStringIndex result_graphs = MVM_string_graphs_nocheck(tc, result);
if (!result_graphs)
return;
if (string_can_be_8bit(tc, gi, result_graphs)) {
MVMStringIndex result_pos = 0;
result->body.storage_type = MVM_STRING_GRAPHEME_8;
result8 = result->body.storage.blob_8 =
MVM_malloc(result_graphs * sizeof(MVMGrapheme8));
while (1) {
MVMStringIndex strand_len =
MVM_string_gi_graphs_left_in_strand(tc, gi);
MVMStringIndex to_copy = result_graphs - result_pos < strand_len
? result_graphs - result_pos
: strand_len;
MVMGrapheme8 *result_blob8 = result8 + result_pos;
switch (MVM_string_gi_blob_type(tc, gi)) {
case MVM_STRING_GRAPHEME_32: {
MVMStringIndex i;
MVMGrapheme32 *active_blob =
MVM_string_gi_active_blob_32_pos(tc, gi);
MVM_VECTORIZE_LOOP
for (i = 0; i < to_copy; i++) {
result_blob8[i] = active_blob[i];
}
break;
}
case MVM_STRING_GRAPHEME_8:
case MVM_STRING_GRAPHEME_ASCII: {
memcpy(
result_blob8,
MVM_string_gi_active_blob_8_pos(tc, gi),
to_copy * sizeof(MVMGrapheme8)
);
break;
}
default:
MVM_exception_throw_adhoc(tc,
"Internal error, string corruption in iterate_gi_into_string\n");
}
result_pos += to_copy;
if (result_graphs <= result_pos || !MVM_string_gi_has_more_strands_rep(tc, gi)) {
break;
}
MVM_string_gi_next_strand_rep(tc, gi);
}
}
else {
MVMStringIndex result_pos = 0;
result->body.storage_type = MVM_STRING_GRAPHEME_32;
result32 = result->body.storage.blob_32 =
result_graphs ? MVM_malloc(result_graphs * sizeof(MVMGrapheme32)) : NULL;
while (1) {
MVMStringIndex strand_len = MVM_string_gi_graphs_left_in_strand(tc, gi);
MVMStringIndex to_copy = result_graphs - result_pos < strand_len
? result_graphs - result_pos
: strand_len;
switch (MVM_string_gi_blob_type(tc, gi)) {
case MVM_STRING_GRAPHEME_8:
case MVM_STRING_GRAPHEME_ASCII: {
MVMGrapheme8 *active_blob =
MVM_string_gi_active_blob_8_pos(tc, gi);
MVMGrapheme32 *result_blob32 = result32 + result_pos;
MVMStringIndex i;
MVM_VECTORIZE_LOOP
for (i = 0; i < to_copy; i++) {
result_blob32[i] = active_blob[i];
}
break;
}
case MVM_STRING_GRAPHEME_32: {
memcpy(
result32 + result_pos,
MVM_string_gi_active_blob_32_pos(tc, gi),
to_copy * sizeof(MVMGrapheme32)
);
break;
}
default:
MVM_exception_throw_adhoc(tc,
"Internal error, string corruption in iterate_gi_into_string\n");
}
result_pos += to_copy;
if (result_graphs <= result_pos || !MVM_string_gi_has_more_strands_rep(tc, gi)) {
break;
}
MVM_string_gi_next_strand_rep(tc, gi);
}
}
}
#define copy_strands_memcpy(BLOB_TYPE, SIZEOF_TYPE, STORAGE_TYPE) { \
result->body.storage.BLOB_TYPE = MVM_malloc(sizeof(SIZEOF_TYPE) * MVM_string_graphs_nocheck(tc, orig)); \
for (i = 0; i < orig->body.num_strands; i++) { \
size_t graphs_this_strand = orig->body.storage.strands[i].end - orig->body.storage.strands[i].start; \
/* If it's 8bit format and there's only one grapheme */ \
if ((STORAGE_TYPE == MVM_STRING_GRAPHEME_ASCII || STORAGE_TYPE == MVM_STRING_GRAPHEME_8) && graphs_this_strand == 1) { \
/* If there are not repetitions we can directly set the grapheme */ \
if (!orig->body.storage.strands[i].repetitions) \
result->body.storage.BLOB_TYPE[graphs_so_far] = orig->body.storage.strands[i].blob_string->body.storage.BLOB_TYPE[orig->body.storage.strands[i].start]; \
/* Otherwise, use memset for the correct number of repetitions */ \
else { \
graphs_this_strand += orig->body.storage.strands[i].repetitions; \
memset(graphs_so_far + result->body.storage.BLOB_TYPE, \
orig->body.storage.strands[i].blob_string->body.storage.BLOB_TYPE[orig->body.storage.strands[i].start], \
graphs_this_strand \
); \
} \
graphs_so_far += graphs_this_strand; \
} \
else { \
int j = 0; \
for (; j <= orig->body.storage.strands[i].repetitions; j++) { \
memcpy(graphs_so_far + result->body.storage.BLOB_TYPE, \
orig->body.storage.strands[i].blob_string->body.storage.BLOB_TYPE + orig->body.storage.strands[i].start, \
sizeof(SIZEOF_TYPE) * graphs_this_strand \
); \
graphs_so_far += graphs_this_strand; \
} \
} \
} \
}
/* Collapses a bunch of strands into a single blob string. */
static MVMString * collapse_strands(MVMThreadContext *tc, MVMString *orig) {
MVMString *result = NULL;
size_t graphs_so_far = 0;
/* If it's not a strand, just return it */
if (orig->body.storage_type != MVM_STRING_STRAND)
return orig;
/* If the original string is a STRAND and all the composite strands are
* of the same type, then we will collapse it using memcpy instead of
* using a grapheme iterator. */
else {
size_t i;
MVMint32 common_storage_type = orig->body.storage.strands[0].blob_string->body.storage_type;
MVMROOT(tc, orig, {
result = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString);
result->body.num_graphs = MVM_string_graphs(tc, orig);
for (i = 1; i < orig->body.num_strands; i++) {
if (common_storage_type != orig->body.storage.strands[i].blob_string->body.storage_type) {
common_storage_type = -1;
break;
}
}
result->body.storage_type = common_storage_type;
switch (common_storage_type) {
case MVM_STRING_GRAPHEME_32:
copy_strands_memcpy(blob_32, MVMGrapheme32, MVM_STRING_GRAPHEME_32);
break;
case MVM_STRING_GRAPHEME_ASCII:
case MVM_STRING_GRAPHEME_8:
copy_strands_memcpy(blob_8, MVMGrapheme8, MVM_STRING_GRAPHEME_8);
break;
default: {
MVMGraphemeIter gi;
MVM_string_gi_init(tc, &gi, orig);
iterate_gi_into_string(tc, &gi, result, orig, 0);
}
}
});
}
#if (MVM_DEBUG_STRANDS || MVM_DEBUG_NFG)
if (!MVM_string_equal(tc, result, orig))
MVM_exception_throw_adhoc(tc, "result and original were not eq in collapse_strands");
#endif
return result;
}
/* Takes a string that is no longer in NFG form after some concatenation-style
* operation, and returns a new string that is in NFG. Note that we could do a
* much, much, smarter thing in the future that doesn't involve all of this
* copying and allocation and re-doing the whole string, but cases like this
* should be fairly rare anyway, so go with simplicity for now. */
static MVMString * re_nfg(MVMThreadContext *tc, MVMString *in) {
MVMNormalizer norm;
MVMCodepointIter ci;
MVMint32 ready;
MVMString *out = NULL;
MVMuint32 bufsize = in->body.num_graphs;
/* Create the output buffer. We used to believe it can't ever be bigger
* than the initial estimate, but utf8-c8 showed us otherwise. */
MVMGrapheme32 *out_buffer = MVM_malloc(bufsize * sizeof(MVMGrapheme32));
MVMint64 out_pos = 0;
/* Iterate codepoints and normalizer. */
MVM_unicode_normalizer_init(tc, &norm, MVM_NORMALIZE_NFG);
/* Codepoint iterator that passes back utf8-c8 graphemes unchanged */
MVM_string_ci_init(tc, &ci, in, 0, 1);
while (MVM_string_ci_has_more(tc, &ci)) {
MVMGrapheme32 g;
ready = MVM_unicode_normalizer_process_codepoint_to_grapheme(tc, &norm, MVM_string_ci_get_codepoint(tc, &ci), &g);
if (ready) {
if (out_pos + ready > bufsize) {
/* Doubling up the buffer size seems excessive, so just
* add a generous amount of storage */
bufsize += ready + 32;
out_buffer = MVM_realloc(out_buffer, bufsize * sizeof(MVMGrapheme32));
}
out_buffer[out_pos++] = g;
while (--ready > 0) {
out_buffer[out_pos++] = MVM_unicode_normalizer_get_grapheme(tc, &norm);
}
}
}
MVM_unicode_normalizer_eof(tc, &norm);
ready = MVM_unicode_normalizer_available(tc, &norm);
if (out_pos + ready > bufsize) {
bufsize += ready + 1;
out_buffer = MVM_realloc(out_buffer, bufsize * sizeof(MVMGrapheme32));
}
while (ready--) {
out_buffer[out_pos++] = MVM_unicode_normalizer_get_grapheme(tc, &norm);
}
MVM_unicode_normalizer_cleanup(tc, &norm);
/* Build result string. */
out = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString);
out->body.storage.blob_32 = out_buffer;
out->body.storage_type = MVM_STRING_GRAPHEME_32;
out->body.num_graphs = out_pos;
return out;
}
/* Returns nonzero if two substrings are equal, doesn't check bounds */
MVMint64 MVM_string_substrings_equal_nocheck(MVMThreadContext *tc, MVMString *a,
MVMint64 starta, MVMint64 length, MVMString *b, MVMint64 startb) {
MVMint64 i;
/* Fast paths when storage types are identical. */
switch (a->body.storage_type) {
case MVM_STRING_GRAPHEME_32:
if (b->body.storage_type == MVM_STRING_GRAPHEME_32)
return 0 == memcmp(
a->body.storage.blob_32 + starta,
b->body.storage.blob_32 + startb,
length * sizeof(MVMGrapheme32));
break;
case MVM_STRING_GRAPHEME_ASCII:
case MVM_STRING_GRAPHEME_8:
if (b->body.storage_type == MVM_STRING_GRAPHEME_ASCII ||
b->body.storage_type == MVM_STRING_GRAPHEME_8)
return 0 == memcmp(
a->body.storage.blob_8 + starta,
b->body.storage.blob_8 + startb,
length);
break;
}
/* If both are flat, use MVM_string_get_grapheme_at_nocheck on both for speed */
if (a->body.storage_type != MVM_STRING_STRAND && b->body.storage_type != MVM_STRING_STRAND) {
for (i = 0; i < length; i++)
if (MVM_string_get_grapheme_at_nocheck(tc, a, starta + i)
!= MVM_string_get_grapheme_at_nocheck(tc, b, startb + i))
return 0;
return 1;
}
else if (a->body.storage_type == MVM_STRING_STRAND && b->body.storage_type == MVM_STRING_STRAND) {
MVMGraphemeIter gia, gib;
/* Normal path, for the rest of the time. */
MVM_string_gi_init(tc, &gia, a);
MVM_string_gi_init(tc, &gib, b);
/* Move the grapheme iterator if start is not 0 */
if (starta) MVM_string_gi_move_to(tc, &gia, starta);
if (startb) MVM_string_gi_move_to(tc, &gib, startb);
for (i = 0; i < length; i++)
if (MVM_string_gi_get_grapheme(tc, &gia) != MVM_string_gi_get_grapheme(tc, &gib))
return 0;
return 1;
}
else {
MVMGraphemeIter gi_y;
MVMString *y = NULL, *z = NULL;
MVMint64 starty, startz;
if (a->body.storage_type == MVM_STRING_STRAND) {
y = a; z = b;
starty = starta; startz = startb;
}
else {
y = b; z = a;
starty = startb; startz = starta;
}
MVM_string_gi_init(tc, &gi_y, y);
if (starty) MVM_string_gi_move_to(tc, &gi_y, starty);
for (i = 0; i < length; i++)
if (MVM_string_gi_get_grapheme(tc, &gi_y) != MVM_string_get_grapheme_at_nocheck(tc, z, startz + i))
return 0;
return 1;
}
}
static MVMint64 MVM_string_memmem_grapheme32 (MVMThreadContext *tc, MVMGrapheme32 *H_blob32, MVMGrapheme32 *n_blob32, MVMint64 H_start, MVMStringIndex H_graphs, MVMStringIndex n_graphs) {
MVMGrapheme32 * rtrn = memmem_uint32(H_blob32 + H_start, H_graphs - H_start, n_blob32, n_graphs);
return rtrn == NULL ? -1 : rtrn - H_blob32;
}
static MVMint64 MVM_string_memmem_grapheme32str (MVMThreadContext *tc, MVMString *Haystack, MVMString *needle, MVMint64 H_start, MVMStringIndex H_graphs, MVMStringIndex n_graphs) {
MVMGrapheme32 *needle_buf = NULL;
MVMint64 rtrn;
if (needle->body.storage_type != MVM_STRING_GRAPHEME_32) {
MVMStringIndex i;
MVMGraphemeIter n_gi;
needle_buf = MVM_malloc(needle->body.num_graphs * sizeof(MVMGrapheme32));
if (needle->body.storage_type != MVM_STRING_GRAPHEME_8) MVM_string_gi_init(tc, &n_gi, needle);
for (i = 0; i < needle->body.num_graphs; i++) {
needle_buf[i] = needle->body.storage_type == MVM_STRING_GRAPHEME_8 ? needle->body.storage.blob_8[i] : MVM_string_gi_get_grapheme(tc, &n_gi);
}
}
rtrn = MVM_string_memmem_grapheme32(tc, Haystack->body.storage.blob_32, needle_buf ? needle_buf : needle->body.storage.blob_32, H_start, H_graphs, n_graphs);
if (needle_buf) MVM_free(needle_buf);
return rtrn;
}
/* Returns the location of one string in another or -1 */
MVMint64 MVM_string_index(MVMThreadContext *tc, MVMString *Haystack, MVMString *needle, MVMint64 start) {
size_t index = (size_t)start;
MVMStringIndex H_graphs, n_graphs;
MVM_string_check_arg(tc, Haystack, "index search target");
MVM_string_check_arg(tc, needle, "index search term");
H_graphs = MVM_string_graphs_nocheck(tc, Haystack);
n_graphs = MVM_string_graphs_nocheck(tc, needle);
if (!n_graphs)
return start <= H_graphs ? start : -1; /* the empty string is in any other string */
if (!H_graphs)
return -1;
if (start < 0 || H_graphs <= start)
return -1;
if (H_graphs < n_graphs || n_graphs < 1)
return -1;
/* Fast paths when storage types are identical. Uses memmem function, which
* uses Knuth-Morris-Pratt algorithm on Linux and on others
* Crochemore+Perrin two-way string matching */
switch (Haystack->body.storage_type) {
case MVM_STRING_GRAPHEME_32:
if (needle->body.storage_type == MVM_STRING_GRAPHEME_32 || needle->body.num_graphs < 100) {
return MVM_string_memmem_grapheme32str(tc, Haystack, needle, start, H_graphs, n_graphs);
}
break;
case MVM_STRING_GRAPHEME_8:
if (needle->body.storage_type == MVM_STRING_GRAPHEME_8 || needle->body.num_graphs < 100) {
void *mm_return_8 = NULL;
MVMGrapheme8 *needle_buf = NULL;
if (needle->body.storage_type != MVM_STRING_GRAPHEME_8) {
MVMStringIndex i;
MVMGraphemeIter n_gi;
needle_buf = MVM_malloc(needle->body.num_graphs * sizeof(MVMGrapheme8));
if (needle->body.storage_type != MVM_STRING_GRAPHEME_32) MVM_string_gi_init(tc, &n_gi, needle);
for (i = 0; i < needle->body.num_graphs; i++) {
MVMGrapheme32 g = needle->body.storage_type == MVM_STRING_GRAPHEME_32
? needle->body.storage.blob_32[i]
: MVM_string_gi_get_grapheme(tc, &n_gi);
/* Haystack is 8 bit, needle is 32 bit. if we encounter a non8bit grapheme
* it's impossible to match */
if (!can_fit_into_8bit(g)) {
MVM_free(needle_buf);
return -1;
}
needle_buf[i] = g;
}
}
mm_return_8 = MVM_memmem(
Haystack->body.storage.blob_8 + start, /* start position */
(H_graphs - start) * sizeof(MVMGrapheme8), /* length of Haystack from start position to end */
needle_buf ? needle_buf : needle->body.storage.blob_8, /* needle start */
n_graphs * sizeof(MVMGrapheme8) /* needle length */
);
if (needle_buf) MVM_free(needle_buf);
if (mm_return_8 == NULL)
return -1;
else
return (MVMGrapheme8*)mm_return_8 - Haystack->body.storage.blob_8;
}
break;
}
/* Minimal code version for needles of size 1 */
if (n_graphs == 1) {
MVMGraphemeIter H_gi;
MVMGrapheme32 n_g = MVM_string_get_grapheme_at_nocheck(tc, needle, 0);
MVM_string_gi_init(tc, &H_gi, Haystack);
if (index) MVM_string_gi_move_to(tc, &H_gi, index);
while (index < H_graphs) {
if (n_g == MVM_string_gi_get_grapheme(tc, &H_gi))
return (MVMint64)index;
index++;
}
}
else if (n_graphs <= MVM_string_KMP_max_pattern_length)
return knuth_morris_pratt_string_index(tc, needle, Haystack, start);
else {
int is_gic = Haystack->body.storage_type == MVM_STRING_STRAND ? 1 : 0;
void *Hs_or_gic = Haystack;
/* If Haystack is a strand allocate space for a MVMGraphemeIter_cached
* and initialize it */
if (is_gic) {
Hs_or_gic = alloca(sizeof(MVMGraphemeIter_cached));
MVM_string_gi_cached_init(tc, Hs_or_gic, Haystack, start);
}
/* For needles > MVM_string_KMP_max_pattern_length we must revert to brute force for now.
* Eventually we can implement brute force after it matches the whole needle OR
* allocate more space for the pattern on reaching the end of the pattern */
while (index <= H_graphs - n_graphs) {
if (string_equal_at_ignore_case_INTERNAL_loop(tc, Hs_or_gic, needle, index, H_graphs, n_graphs, 0, 0, is_gic) != -1)
return (MVMint64)index;
index++;
}
}
return -1;
}
/* Returns the location of one string in another or -1 */
MVMint64 MVM_string_index_from_end(MVMThreadContext *tc, MVMString *Haystack, MVMString *needle, MVMint64 start) {
MVMint64 result = -1;
size_t index;
MVMStringIndex H_graphs, n_graphs;
MVM_string_check_arg(tc, Haystack, "rindex search target");
MVM_string_check_arg(tc, needle, "rindex search term");
H_graphs = MVM_string_graphs_nocheck(tc, Haystack);
n_graphs = MVM_string_graphs_nocheck(tc, needle);
if (!n_graphs) {
if (0 <= start)
return start <= H_graphs ? start : -1; /* the empty string is in any other string */
else
return H_graphs; /* no start, so return end */
}
if (!H_graphs)
return -1;
if (H_graphs < n_graphs || n_graphs < 1)
return -1;
if (start == -1)
start = H_graphs - n_graphs;
if (start < 0 || H_graphs <= start)
/* maybe return -1 instead? */
MVM_exception_throw_adhoc(tc, "index start offset out of range");
index = start;
if (H_graphs < index + n_graphs) {
index = H_graphs - n_graphs;
}
/* brute force for now. horrible, yes. halp. */
do {
if (MVM_string_substrings_equal_nocheck(tc, needle, 0, n_graphs, Haystack, index)) {
result = (MVMint64)index;
break;
}
} while (0 < index--);
return result;
}
/* Returns a substring of the given string */
MVMString * MVM_string_substring(MVMThreadContext *tc, MVMString *a, MVMint64 offset, MVMint64 length) {
MVMString *result;
MVMint64 start_pos, end_pos;
MVMint64 agraphs;
MVM_string_check_arg(tc, a, "substring");
/* convert to signed to avoid implicit arithmetic conversions */
agraphs = (MVMint64)MVM_string_graphs_nocheck(tc, a);
/* -1 signifies go to the end of the string; anything less is a bug */
if (length < -1)
MVM_exception_throw_adhoc(tc, "Substring length (%"PRId64") cannot be negative", length);
/* negative offsets count from the end */
start_pos = offset < 0 ? offset + agraphs : offset;
end_pos = length == -1 ? agraphs : start_pos + length;
/* return an empty string if start_pos is out of bounds but positive */
if (agraphs < start_pos) {
start_pos = 0;
end_pos = 0;
}
if (end_pos < 0)
MVM_exception_throw_adhoc(tc, "Substring end (%"PRId64") cannot be less than 0", end_pos);
/* Ensure we're within bounds. */
if (start_pos < 0)
start_pos = 0;
if (agraphs < end_pos)
end_pos = agraphs;
/* Check trivial cases: empty string and whole string. */
if (start_pos == end_pos)
return tc->instance->str_consts.empty;
if (start_pos == 0 && end_pos == agraphs)
return a;
/* Construct a result; how we efficiently do so will vary based on the
* input string. */
MVMROOT(tc, a, {
result = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString);
result->body.num_graphs = end_pos - start_pos;
if (a->body.storage_type != MVM_STRING_STRAND) {
/* It's some kind of buffer. Construct a strand view into it. */
result->body.storage_type = MVM_STRING_STRAND;
result->body.storage.strands = allocate_strands(tc, 1);
result->body.num_strands = 1;
result->body.storage.strands[0].blob_string = a;
result->body.storage.strands[0].start = start_pos;
result->body.storage.strands[0].end = end_pos;
result->body.storage.strands[0].repetitions = 0;
}
else if (a->body.num_strands == 1 && a->body.storage.strands[0].repetitions == 0) {
/* Single strand string; quite possibly already a substring. We'll
* just produce an updated view. */
MVMStringStrand *orig_strand = &(a->body.storage.strands[0]);
result->body.storage_type = MVM_STRING_STRAND;
result->body.storage.strands = allocate_strands(tc, 1);
result->body.num_strands = 1;
result->body.storage.strands[0].blob_string = orig_strand->blob_string;
result->body.storage.strands[0].start = orig_strand->start + start_pos;
result->body.storage.strands[0].end = orig_strand->start + end_pos;
result->body.storage.strands[0].repetitions = 0;
}
else {
/* Produce a new blob string, collapsing the strands. */
MVMGraphemeIter gi;
MVM_string_gi_init(tc, &gi, a);
MVM_string_gi_move_to(tc, &gi, start_pos);
iterate_gi_into_string(tc, &gi, result, a, start_pos);
}
});
STRAND_CHECK(tc, result);
return result;
}
MVMString * MVM_string_replace(MVMThreadContext *tc, MVMString *original, MVMint64 start, MVMint64 count, MVMString *replacement) {
/* XXX this could probably be done more efficiently directly. */
MVMString *first_part = NULL;
MVMString *rest_part = NULL;
MVMString *result = NULL;
MVM_gc_root_temp_push(tc, (MVMCollectable **)&replacement);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&original);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&first_part);
first_part = MVM_string_substring(tc, original, 0, start);
rest_part = MVM_string_substring(tc, original, start + count, -1);
rest_part = MVM_string_concatenate(tc, replacement, rest_part);
result = MVM_string_concatenate(tc, first_part, rest_part);
STRAND_CHECK(tc, result);
NFG_CHECK(tc, result, "MVM_string_replace");
MVM_gc_root_temp_pop_n(tc, 3);
return result;
}
static MVMString * string_from_strand_at_index(MVMThreadContext *tc, MVMString *a, MVMuint16 index) {
MVMStringStrand *ss = &(a->body.storage.strands[index]);
return MVM_string_substring(tc, ss->blob_string, ss->start, ss->end - ss->start);
}
static MVMuint16 final_strand_match_with_repetition_count(MVMThreadContext *tc, MVMString *a, MVMString *b) {
if (a->body.storage_type == MVM_STRING_STRAND) {
MVMStringStrand *sa = &(a->body.storage.strands[a->body.num_strands - 1]);
/* If the final strand of a eq b, we'll just increment the final strand of a's repetitions. */
if (sa->end - sa->start == MVM_string_graphs_nocheck(tc, b)) {
if (MVM_string_equal_at(tc, sa->blob_string, b, sa->start))
return 1;
}
/* If the final strand of a eq the first (and only) strand of b, we'll just add b's repetitions
* (plus 1 for the strand itself) to the final strand of a's repetitions. */
else if (b->body.storage_type == MVM_STRING_STRAND && b->body.num_strands == 1) {
MVMStringStrand *sb = &(b->body.storage.strands[0]);
if (sa->end - sa->start == sb->end - sb->start)
if (MVM_string_equal(tc, string_from_strand_at_index(tc, a, a->body.num_strands - 1), string_from_strand_at_index(tc, b, 0)))
return b->body.storage.strands[0].repetitions + 1;
}
}
return 0;
}
/* Append one string to another. */
MVMString * MVM_string_concatenate(MVMThreadContext *tc, MVMString *a, MVMString *b) {
MVMString *result = NULL, *renormalized_section = NULL;
int renormalized_section_graphs = 0, consumed_a = 0, consumed_b = 0;
MVMuint32 agraphs, bgraphs;
MVMuint64 total_graphs;
int lost_strands = 0;
int is_concat_stable = 0;
int index_ss_b;
MVMuint16 matching_repetition_count;
MVM_string_check_arg(tc, a, "concatenate");
MVM_string_check_arg(tc, b, "concatenate");
/* Simple empty-string cases. */
agraphs = MVM_string_graphs_nocheck(tc, a);
if (agraphs == 0)
return b;
bgraphs = MVM_string_graphs_nocheck(tc, b);
if (bgraphs == 0)
return a;
is_concat_stable = MVM_nfg_is_concat_stable(tc, a, b);
/* If is_concat_stable equals 0 and a and b are not repetitions. */
if (is_concat_stable == 0 && !(a->body.storage_type == MVM_STRING_STRAND && a->body.storage.strands[a->body.num_strands - 1].repetitions)
&& !(b->body.storage_type == MVM_STRING_STRAND && b->body.storage.strands[0].repetitions)) {
MVMCodepoint last_a_first_b[2] = {
MVM_string_get_grapheme_at_nocheck(tc, a, a->body.num_graphs - 1),
MVM_string_get_grapheme_at_nocheck(tc, b, 0)
};
MVMROOT2(tc, a, b, {
/* If both are not synthetics, we can can pass those values unchanged
* instead of iterating by codepoint */
if (0 <= last_a_first_b[0] && 0 <= last_a_first_b[1]) {
renormalized_section = MVM_unicode_codepoints_c_array_to_nfg_string(tc, last_a_first_b, 2);
consumed_a = 1; consumed_b = 1;
}
else {
MVMCodepointIter last_a_ci;
MVMCodepointIter first_b_ci;
MVMuint32 a_codes = MVM_string_grapheme_ci_init(tc, &last_a_ci, last_a_first_b[0], 1);
MVMuint32 b_codes = MVM_string_grapheme_ci_init(tc, &first_b_ci, last_a_first_b[1], 1);
/* MSVC doesn't allow variable length arrays so use alloca to allocate onto the stack */
MVMCodepoint *last_a_first_b_codes = alloca((a_codes + b_codes) * sizeof(MVMCodepoint));
MVMuint32 i = 0;
for (; MVM_string_grapheme_ci_has_more(tc, &last_a_ci); i++) {
last_a_first_b_codes[i] = MVM_string_grapheme_ci_get_codepoint(tc, &last_a_ci);
}
for (; MVM_string_grapheme_ci_has_more(tc, &first_b_ci); i++) {
last_a_first_b_codes[i] = MVM_string_grapheme_ci_get_codepoint(tc, &first_b_ci);
}
renormalized_section = MVM_unicode_codepoints_c_array_to_nfg_string(tc, last_a_first_b_codes, a_codes + b_codes);
consumed_a = 1; consumed_b = 1;
}
});
if (renormalized_section) {
if (agraphs == consumed_a && bgraphs == consumed_b) {
NFG_CHECK_CONCAT(tc, renormalized_section, a, b, "renormalized_section");
return renormalized_section;
}
renormalized_section_graphs = MVM_string_graphs_nocheck(tc, renormalized_section);
}
}
/* Total size of the resulting string can't be bigger than an MVMString is allowed to be. */
total_graphs = (MVMuint64)agraphs + (MVMuint64)bgraphs;
if (MAX_GRAPHEMES < total_graphs)
MVM_exception_throw_adhoc(tc,
"Can't concatenate strings, required number of graphemes %"PRIu64" > max allowed of %lld",
total_graphs, MAX_GRAPHEMES);
/* Otherwise, we'll assemble a result string. */
MVMROOT4(tc, a, b, renormalized_section, result, {
/* Allocate it. */
result = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString);
/* Total graphemes is trivial; just total up inputs. */
result->body.num_graphs = (MVMuint32)total_graphs;
/* Result string will be made of strands. */
result->body.storage_type = MVM_STRING_STRAND;
/* Detect the wonderful case where we're repeatedly concating the same
* string again and again, and thus can just bump a repetition. */
if (is_concat_stable == 1 && (matching_repetition_count = final_strand_match_with_repetition_count(tc, a, b))) {
/* We have it; just copy the strands to a new string and bump the
* repetitions count of the last one. */
result->body.storage.strands = allocate_strands(tc, a->body.num_strands);
copy_strands(tc, a, 0, result, 0, a->body.num_strands);
result->body.storage.strands[a->body.num_strands - 1].repetitions += matching_repetition_count;
result->body.num_strands = a->body.num_strands;
}
/* Otherwise, construct a new strand string. */
else {
/* See if we have too many strands between the two. If so, we will
* collapse the biggest side. */
MVMuint16 strands_a = a->body.storage_type == MVM_STRING_STRAND
? a->body.num_strands
: 1;
MVMuint16 strands_b = b->body.storage_type == MVM_STRING_STRAND
? b->body.num_strands
: 1;
MVMString *effective_a = a;
MVMString *effective_b = b;
if (MVM_STRING_MAX_STRANDS < strands_a + strands_b) {
MVMROOT(tc, result, {
if (strands_b <= strands_a) {
effective_a = collapse_strands(tc, effective_a);
strands_a = 1;
}
else {
effective_b = collapse_strands(tc, effective_b);
strands_b = 1;
}
});
}
/* Assemble the result. */
result->body.num_strands = strands_a + strands_b + (renormalized_section_graphs ? 1 : 0);
result->body.storage.strands = allocate_strands(tc, result->body.num_strands);
/* START 1 */
if (effective_a->body.storage_type == MVM_STRING_STRAND) {
copy_strands(tc, effective_a, 0, result, 0, strands_a);
}
else {
int index_ss_a = 0;
MVMStringStrand *ss_a = &(result->body.storage.strands[index_ss_a]);
ss_a->blob_string = effective_a;
ss_a->start = 0;
ss_a->end = effective_a->body.num_graphs;
ss_a->repetitions = 0;
}
if (renormalized_section) {
int index_ss_re;
int index_ss_a = strands_a - 1;
/* Tweak the end of the last strand of string a. Since if a is made up of multiple strands, we can't just refer to index 0 and instead erfer to strands_a - 1 */
MVMStringStrand *ss_a = &(result->body.storage.strands[index_ss_a]);
MVMStringStrand *ss_re = NULL;
ss_a->end -= consumed_a;
/* If the strands ends up to be zero length we need to reduce the number of strand_index and also incease lost_strands so the next operation writes over it */
if (ss_a->start == ss_a->end)
lost_strands++;
/* END 1 */
/* START 1.5 (only triggered in some cases) */
index_ss_re = strands_a - lost_strands;
ss_re = &(result->body.storage.strands[index_ss_re]);
/* Add the renormalized section in as a strand */
ss_re->blob_string = renormalized_section;
ss_re->start = 0;
ss_re->end = renormalized_section->body.num_graphs;
ss_re->repetitions = 0;
if (ss_re->start == ss_re->end) {
MVM_exception_throw_adhoc(tc, "Unexpected error in concatenation: renormalized_section is 0 graphemes.\n");
/* renormalized_section should always be at least one grapheme
* in length so throw if it does not (zero length is an error
* we shouldn't lost_strands++ unlike the other strands */
}
/* END 1.5 */
}
/* START 2 */
index_ss_b = strands_a - lost_strands + (renormalized_section_graphs ? 1 : 0 );
if (effective_b->body.storage_type == MVM_STRING_STRAND) {
copy_strands(tc, effective_b, 0, result, index_ss_b, strands_b);
}
else {
MVMStringStrand *ss_b = &(result->body.storage.strands[index_ss_b]);
ss_b->blob_string = effective_b;
ss_b->start = 0;