-
Notifications
You must be signed in to change notification settings - Fork 3
/
array.c
1897 lines (1643 loc) · 38.2 KB
/
array.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
/**********************************************************************
array.c -
$Author: matz $
$Date: 2002/09/03 05:20:06 $
created at: Fri Aug 6 09:46:12 JST 1993
Copyright (C) 1993-2002 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby.h"
#include "util.h"
#include "st.h"
VALUE rb_cArray;
static ID id_cmp;
#define ARY_DEFAULT_SIZE 16
void
rb_mem_clear(mem, size)
register VALUE *mem;
register long size;
{
while (size--) {
*mem++ = Qnil;
}
}
static void
memfill(mem, size, val)
register VALUE *mem;
register long size;
register VALUE val;
{
while (size--) {
*mem++ = val;
}
}
#define ARY_TMPLOCK FL_USER1
static void
rb_ary_modify_check(ary)
VALUE ary;
{
if (OBJ_FROZEN(ary)) rb_error_frozen("array");
if (FL_TEST(ary, ARY_TMPLOCK))
rb_raise(rb_eTypeError, "can't modify array during sort");
if (!OBJ_TAINTED(ary) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
}
static void
rb_ary_modify(ary)
VALUE ary;
{
VALUE *ptr;
rb_ary_modify_check(ary);
if (FL_TEST(ary, ELTS_SHARED)) {
ptr = ALLOC_N(VALUE, RARRAY(ary)->len);
FL_UNSET(ary, ELTS_SHARED);
RARRAY(ary)->aux.capa = RARRAY(ary)->len;
MEMCPY(ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
RARRAY(ary)->ptr = ptr;
}
}
VALUE
rb_ary_freeze(ary)
VALUE ary;
{
return rb_obj_freeze(ary);
}
static VALUE
rb_ary_frozen_p(ary)
VALUE ary;
{
if (FL_TEST(ary, FL_FREEZE|ARY_TMPLOCK))
return Qtrue;
return Qfalse;
}
static VALUE
rb_ary_s_alloc(klass)
VALUE klass;
{
NEWOBJ(ary, struct RArray);
OBJSETUP(ary, klass, T_ARRAY);
ary->len = 0;
ary->ptr = 0;
ary->aux.capa = 0;
return (VALUE)ary;
}
static VALUE
ary_new(klass, len)
VALUE klass;
long len;
{
VALUE ary = rb_obj_alloc(klass);
if (len < 0) {
rb_raise(rb_eArgError, "negative array size (or size too big)");
}
if (len > 0 && len * sizeof(VALUE) <= len) {
rb_raise(rb_eArgError, "array size too big");
}
if (len == 0) len++;
RARRAY(ary)->ptr = ALLOC_N(VALUE, len);
RARRAY(ary)->aux.capa = len;
return ary;
}
VALUE
rb_ary_new2(len)
long len;
{
return ary_new(rb_cArray, len);
}
VALUE
rb_ary_new()
{
return rb_ary_new2(ARY_DEFAULT_SIZE);
}
#ifdef HAVE_STDARG_PROTOTYPES
#include <stdarg.h>
#define va_init_list(a,b) va_start(a,b)
#else
#include <varargs.h>
#define va_init_list(a,b) va_start(a)
#endif
VALUE
#ifdef HAVE_STDARG_PROTOTYPES
rb_ary_new3(long n, ...)
#else
rb_ary_new3(n, va_alist)
long n;
va_dcl
#endif
{
va_list ar;
VALUE ary;
long i;
ary = rb_ary_new2(n);
va_init_list(ar, n);
for (i=0; i<n; i++) {
RARRAY(ary)->ptr[i] = va_arg(ar, VALUE);
}
va_end(ar);
RARRAY(ary)->len = n;
return ary;
}
VALUE
rb_ary_new4(n, elts)
long n;
const VALUE *elts;
{
VALUE ary;
ary = rb_ary_new2(n);
if (n > 0 && elts) {
MEMCPY(RARRAY(ary)->ptr, elts, VALUE, n);
}
RARRAY(ary)->len = n;
return ary;
}
VALUE
rb_assoc_new(car, cdr)
VALUE car, cdr;
{
VALUE ary;
ary = rb_ary_new2(2);
RARRAY(ary)->ptr[0] = car;
RARRAY(ary)->ptr[1] = cdr;
RARRAY(ary)->len = 2;
return ary;
}
static VALUE
to_ary(ary)
VALUE ary;
{
return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
}
static VALUE rb_ary_replace _((VALUE, VALUE));
static VALUE
rb_ary_initialize(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
long len;
VALUE size, val;
rb_ary_modify(ary);
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
RARRAY(ary)->len = 0;
if (rb_block_given_p()) {
rb_warning("given block not used");
}
return ary;
}
if (argc == 1 && !FIXNUM_P(size)) {
val = rb_check_convert_type(size, T_ARRAY, "Array", "to_ary");
if (!NIL_P(val)) {
rb_ary_replace(ary, val);
return ary;
}
}
len = NUM2LONG(size);
if (len < 0) {
rb_raise(rb_eArgError, "negative array size");
}
if (len > 0 && len * (long)sizeof(VALUE) <= len) {
rb_raise(rb_eArgError, "array size too big");
}
if (len > RARRAY(ary)->aux.capa) {
REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
RARRAY(ary)->aux.capa = len;
}
if (rb_block_given_p()) {
long i;
if (argc > 1) {
rb_raise(rb_eArgError, "wrong number of arguments");
}
for (i=0; i<len; i++) {
RARRAY(ary)->ptr[i] = rb_yield(LONG2NUM(i));
RARRAY(ary)->len = i + 1;
}
}
else {
memfill(RARRAY(ary)->ptr, len, val);
RARRAY(ary)->len = len;
}
return ary;
}
static VALUE
rb_ary_s_create(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE ary = rb_obj_alloc(klass);
if (argc < 0) {
rb_raise(rb_eArgError, "negative number of arguments");
}
if (argc > 0) {
RARRAY(ary)->ptr = ALLOC_N(VALUE, argc);
MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
}
RARRAY(ary)->len = RARRAY(ary)->aux.capa = argc;
return ary;
}
void
rb_ary_store(ary, idx, val)
VALUE ary;
long idx;
VALUE val;
{
rb_ary_modify(ary);
if (idx < 0) {
idx += RARRAY(ary)->len;
if (idx < 0) {
rb_raise(rb_eIndexError, "index %ld out of array",
idx - RARRAY(ary)->len);
}
}
if (idx >= RARRAY(ary)->aux.capa) {
long new_capa = RARRAY(ary)->aux.capa / 2;
if (new_capa < ARY_DEFAULT_SIZE) {
new_capa = ARY_DEFAULT_SIZE;
}
new_capa += idx;
if (new_capa * (long)sizeof(VALUE) <= new_capa) {
rb_raise(rb_eArgError, "index too big");
}
REALLOC_N(RARRAY(ary)->ptr, VALUE, new_capa);
RARRAY(ary)->aux.capa = new_capa;
}
if (idx > RARRAY(ary)->len) {
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len,
idx-RARRAY(ary)->len + 1);
}
if (idx >= RARRAY(ary)->len) {
RARRAY(ary)->len = idx + 1;
}
RARRAY(ary)->ptr[idx] = val;
}
VALUE
rb_ary_push(ary, item)
VALUE ary;
VALUE item;
{
rb_ary_store(ary, RARRAY(ary)->len, item);
return ary;
}
static VALUE
rb_ary_push_m(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
if (argc <= 0) {
rb_raise(rb_eArgError, "wrong number arguments (at least 1)");
}
while (argc--) {
rb_ary_push(ary, *argv++);
}
return ary;
}
VALUE
rb_ary_pop(ary)
VALUE ary;
{
rb_ary_modify_check(ary);
if (RARRAY(ary)->len == 0) return Qnil;
if (!FL_TEST(ary, ELTS_SHARED) &&
RARRAY(ary)->len * 2 < RARRAY(ary)->aux.capa &&
RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
RARRAY(ary)->aux.capa = RARRAY(ary)->len * 2;
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
}
return RARRAY(ary)->ptr[--RARRAY(ary)->len];
}
static void
ary_make_shared(ary)
VALUE ary;
{
if (!FL_TEST(ary, ELTS_SHARED)) {
NEWOBJ(shared, struct RArray);
OBJSETUP(shared, rb_cArray, T_ARRAY);
shared->len = RARRAY(ary)->len;
shared->ptr = RARRAY(ary)->ptr;
shared->aux.capa = RARRAY(ary)->aux.capa;
RARRAY(ary)->aux.shared = (VALUE)shared;
FL_SET(ary, ELTS_SHARED);
}
}
VALUE
rb_ary_shift(ary)
VALUE ary;
{
VALUE top;
rb_ary_modify_check(ary);
if (RARRAY(ary)->len == 0) return Qnil;
top = RARRAY(ary)->ptr[0];
ary_make_shared(ary);
RARRAY(ary)->ptr++; /* shift ptr */
RARRAY(ary)->len--;
return top;
}
VALUE
rb_ary_unshift(ary, item)
VALUE ary, item;
{
rb_ary_modify(ary);
if (RARRAY(ary)->len == RARRAY(ary)->aux.capa) {
long capa_inc = RARRAY(ary)->aux.capa / 2;
if (capa_inc < ARY_DEFAULT_SIZE) {
capa_inc = ARY_DEFAULT_SIZE;
}
RARRAY(ary)->aux.capa += capa_inc;
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
}
/* sliding items */
MEMMOVE(RARRAY(ary)->ptr + 1, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
RARRAY(ary)->len++;
RARRAY(ary)->ptr[0] = item;
return ary;
}
static VALUE
rb_ary_unshift_m(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
long len = RARRAY(ary)->len;
if (argc <= 0) {
rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
}
/* make rooms by setting the last item */
rb_ary_store(ary, len + argc - 1, Qnil);
/* sliding items */
MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
return ary;
}
VALUE
rb_ary_entry(ary, offset)
VALUE ary;
long offset;
{
if (RARRAY(ary)->len == 0) return Qnil;
if (offset < 0) {
offset += RARRAY(ary)->len;
}
if (offset < 0 || RARRAY(ary)->len <= offset) {
return Qnil;
}
return RARRAY(ary)->ptr[offset];
}
static VALUE
rb_ary_subseq(ary, beg, len)
VALUE ary;
long beg, len;
{
VALUE klass, ary2;
if (beg > RARRAY(ary)->len) return Qnil;
if (beg < 0 || len < 0) return Qnil;
if (beg + len > RARRAY(ary)->len) {
len = RARRAY(ary)->len - beg;
if (len < 0)
len = 0;
}
klass = rb_obj_class(ary);
if (len == 0) return ary_new(klass, 0);
ary_make_shared(ary);
ary2 = rb_obj_alloc(klass);
RARRAY(ary2)->ptr = RARRAY(ary)->ptr + beg;
RARRAY(ary2)->len = len;
RARRAY(ary2)->aux.shared = RARRAY(ary)->aux.shared;
FL_SET(ary2, ELTS_SHARED);
return ary2;
}
VALUE
rb_ary_aref(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
VALUE arg;
long beg, len;
if (argc == 2) {
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
if (beg < 0) {
beg += RARRAY(ary)->len;
}
return rb_ary_subseq(ary, beg, len);
}
if (argc != 1) {
rb_scan_args(argc, argv, "11", 0, 0);
}
arg = argv[0];
/* special case - speeding up */
if (FIXNUM_P(arg)) {
return rb_ary_entry(ary, FIX2LONG(arg));
}
/* check if idx is Range */
switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
return rb_ary_subseq(ary, beg, len);
}
return rb_ary_entry(ary, NUM2LONG(arg));
}
static VALUE
rb_ary_at(ary, pos)
VALUE ary, pos;
{
return rb_ary_entry(ary, NUM2LONG(pos));
}
static VALUE
rb_ary_first(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return Qnil;
return RARRAY(ary)->ptr[0];
}
static VALUE
rb_ary_last(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return Qnil;
return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
}
static VALUE
rb_ary_fetch(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
VALUE pos, ifnone;
long idx;
rb_scan_args(argc, argv, "11", &pos, &ifnone);
idx = NUM2LONG(pos);
if (idx < 0) {
idx += RARRAY(ary)->len;
}
if (idx < 0 || RARRAY(ary)->len <= idx) {
if (rb_block_given_p()) {
if (argc > 1) {
rb_raise(rb_eArgError, "wrong number of arguments");
}
return rb_yield(pos);
}
if (argc == 1) {
rb_raise(rb_eIndexError, "index %ld out of array", idx);
}
return ifnone;
}
return RARRAY(ary)->ptr[idx];
}
static VALUE
rb_ary_index(ary, val)
VALUE ary;
VALUE val;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
if (rb_equal(RARRAY(ary)->ptr[i], val))
return LONG2NUM(i);
}
return Qnil;
}
static VALUE
rb_ary_rindex(ary, val)
VALUE ary;
VALUE val;
{
long i = RARRAY(ary)->len;
while (i--) {
if (rb_equal(RARRAY(ary)->ptr[i], val))
return LONG2NUM(i);
}
return Qnil;
}
static VALUE
rb_ary_indexes(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
VALUE new_ary;
long i;
rb_warn("Array#%s is deprecated; use Array#select",
rb_id2name(rb_frame_last_func()));
new_ary = rb_ary_new2(argc);
for (i=0; i<argc; i++) {
rb_ary_push(new_ary, rb_ary_aref(1, argv+i, ary));
}
return new_ary;
}
VALUE
rb_ary_to_ary(obj)
VALUE obj;
{
if (NIL_P(obj)) return rb_ary_new2(0);
if (TYPE(obj) == T_ARRAY) {
return obj;
}
if (rb_respond_to(obj, rb_intern("to_ary"))) {
return rb_convert_type(obj, T_ARRAY, "Array", "to_ary");
}
return rb_ary_new3(1, obj);
}
static void
rb_ary_update(ary, beg, len, rpl)
VALUE ary;
long beg, len;
VALUE rpl;
{
long rlen;
rpl = rb_ary_to_ary(rpl);
rlen = RARRAY(rpl)->len;
if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
if (beg < 0) {
beg += RARRAY(ary)->len;
if (beg < 0) {
beg -= RARRAY(ary)->len;
rb_raise(rb_eIndexError, "index %ld out of array", beg);
}
}
if (beg + len > RARRAY(ary)->len) {
len = RARRAY(ary)->len - beg;
}
rb_ary_modify(ary);
if (beg >= RARRAY(ary)->len) {
len = beg + rlen;
if (len >= RARRAY(ary)->aux.capa) {
REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
RARRAY(ary)->aux.capa = len;
}
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, beg - RARRAY(ary)->len);
MEMCPY(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
RARRAY(ary)->len = len;
}
else {
long alen;
if (beg + len > RARRAY(ary)->len) {
len = RARRAY(ary)->len - beg;
}
alen = RARRAY(ary)->len + rlen - len;
if (alen >= RARRAY(ary)->aux.capa) {
REALLOC_N(RARRAY(ary)->ptr, VALUE, alen);
RARRAY(ary)->aux.capa = alen;
}
if (len != rlen) {
MEMMOVE(RARRAY(ary)->ptr + beg + rlen, RARRAY(ary)->ptr + beg + len,
VALUE, RARRAY(ary)->len - (beg + len));
RARRAY(ary)->len = alen;
}
MEMMOVE(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
}
}
static VALUE
rb_ary_aset(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
long offset, beg, len;
if (argc == 3) {
rb_ary_update(ary, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
return argv[2];
}
if (argc != 2) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
}
if (FIXNUM_P(argv[0])) {
offset = FIX2LONG(argv[0]);
goto fixnum;
}
if (rb_range_beg_len(argv[0], &beg, &len, RARRAY(ary)->len, 1)) {
/* check if idx is Range */
rb_ary_update(ary, beg, len, argv[1]);
return argv[1];
}
offset = NUM2LONG(argv[0]);
fixnum:
rb_ary_store(ary, offset, argv[1]);
return argv[1];
}
static VALUE
rb_ary_insert(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
long pos;
if (argc < 2) {
rb_raise(rb_eArgError, "wrong number of arguments (at least 2)");
}
pos = NUM2LONG(argv[0]);
if (pos == -1) {
pos = RARRAY(ary)->len;
}
else if (pos < 0) {
pos++;
}
rb_ary_update(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1));
return ary;
}
VALUE
rb_ary_each(ary)
VALUE ary;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
rb_yield(RARRAY(ary)->ptr[i]);
}
return ary;
}
static VALUE
rb_ary_each_index(ary)
VALUE ary;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
rb_yield(LONG2NUM(i));
}
return ary;
}
static VALUE
rb_ary_reverse_each(ary)
VALUE ary;
{
long len = RARRAY(ary)->len;
while (len--) {
rb_yield(RARRAY(ary)->ptr[len]);
}
return ary;
}
static VALUE
rb_ary_length(ary)
VALUE ary;
{
return LONG2NUM(RARRAY(ary)->len);
}
static VALUE
rb_ary_empty_p(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0)
return Qtrue;
return Qfalse;
}
VALUE
rb_ary_dup(ary)
VALUE ary;
{
VALUE dup = rb_ary_new2(RARRAY(ary)->len);
DUPSETUP(dup, ary);
MEMCPY(RARRAY(dup)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
RARRAY(dup)->len = RARRAY(ary)->len;
return dup;
}
extern VALUE rb_output_fs;
static VALUE
inspect_join(ary, arg)
VALUE ary;
VALUE *arg;
{
return rb_ary_join(arg[0], arg[1]);
}
VALUE
rb_ary_join(ary, sep)
VALUE ary, sep;
{
long len = 1, i;
int taint = Qfalse;
VALUE result, tmp;
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = Qtrue;
for (i=0; i<RARRAY(ary)->len; i++) {
if (TYPE(RARRAY(ary)->ptr[i]) == T_STRING) {
len += RSTRING(RARRAY(ary)->ptr[i])->len;
}
else {
len += 10;
}
}
if (!NIL_P(sep)) {
StringValue(sep);
len += RSTRING(sep)->len * (RARRAY(ary)->len - 1);
}
result = rb_str_buf_new(len);
for (i=0; i<RARRAY(ary)->len; i++) {
tmp = RARRAY(ary)->ptr[i];
switch (TYPE(tmp)) {
case T_STRING:
break;
case T_ARRAY:
if (rb_inspecting_p(tmp)) {
tmp = rb_str_new2("[...]");
}
else {
VALUE args[2];
args[0] = tmp;
args[1] = sep;
tmp = rb_protect_inspect(inspect_join, ary, (VALUE)args);
}
break;
default:
tmp = rb_obj_as_string(tmp);
}
if (i > 0 && !NIL_P(sep))
rb_str_buf_append(result, sep);
rb_str_buf_append(result, tmp);
if (OBJ_TAINTED(tmp)) taint = Qtrue;
}
if (taint) OBJ_TAINT(result);
return result;
}
static VALUE
rb_ary_join_m(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
VALUE sep;
rb_scan_args(argc, argv, "01", &sep);
if (NIL_P(sep)) sep = rb_output_fs;
return rb_ary_join(ary, sep);
}
VALUE
rb_ary_to_s(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
return rb_ary_join(ary, rb_output_fs);
}
static ID inspect_key;
struct inspect_arg {
VALUE (*func)();
VALUE arg1, arg2;
};
static VALUE
inspect_call(arg)
struct inspect_arg *arg;
{
return (*arg->func)(arg->arg1, arg->arg2);
}
static VALUE
inspect_ensure(obj)
VALUE obj;
{
VALUE inspect_tbl;
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
rb_ary_pop(inspect_tbl);
return 0;
}
VALUE
rb_protect_inspect(func, obj, arg)
VALUE (*func)(ANYARGS);
VALUE obj, arg;
{
struct inspect_arg iarg;
VALUE inspect_tbl;
VALUE id;
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
if (NIL_P(inspect_tbl)) {
inspect_tbl = rb_ary_new();
rb_thread_local_aset(rb_thread_current(), inspect_key, inspect_tbl);
}
id = rb_obj_id(obj);
if (rb_ary_includes(inspect_tbl, id)) {
return (*func)(obj, arg);
}
rb_ary_push(inspect_tbl, id);
iarg.func = func;
iarg.arg1 = obj;
iarg.arg2 = arg;
return rb_ensure(inspect_call, (VALUE)&iarg, inspect_ensure, obj);
}
VALUE
rb_inspecting_p(obj)
VALUE obj;
{
VALUE inspect_tbl;
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
if (NIL_P(inspect_tbl)) return Qfalse;
return rb_ary_includes(inspect_tbl, rb_obj_id(obj));
}
static VALUE
inspect_ary(ary)
VALUE ary;
{
int tainted = OBJ_TAINTED(ary);
long i;
VALUE s, str;
str = rb_str_buf_new2("[");
for (i=0; i<RARRAY(ary)->len; i++) {
s = rb_inspect(RARRAY(ary)->ptr[i]);
if (OBJ_TAINTED(s)) tainted = Qtrue;
if (i > 0) rb_str_buf_cat2(str, ", ");
rb_str_buf_append(str, s);
}
rb_str_buf_cat2(str, "]");
if (tainted) OBJ_TAINT(str);
return str;
}
static VALUE
rb_ary_inspect(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return rb_str_new2("[]");
if (rb_inspecting_p(ary)) return rb_str_new2("[...]");
return rb_protect_inspect(inspect_ary, ary, 0);
}
static VALUE
rb_ary_to_a(ary)
VALUE ary;
{
return ary;
}
VALUE
rb_ary_reverse(ary)
VALUE ary;
{
VALUE *p1, *p2;
VALUE tmp;