-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
3265 lines (2890 loc) · 64.8 KB
/
string.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
/**********************************************************************
string.c -
$Author$
$Date$
created at: Mon Aug 9 17:12:58 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 "re.h"
#define BEG(no) regs->beg[no]
#define END(no) regs->end[no]
#include <math.h>
#include <ctype.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
VALUE rb_cString;
#define STR_ASSOC FL_USER3
#define RESIZE_CAPA(str,capacity) do {\
REALLOC_N(RSTRING(str)->ptr, char, (capacity)+1);\
RSTRING(str)->aux.capa = (capacity);\
} while (0)
VALUE rb_fs;
static VALUE
rb_str_s_alloc(klass)
VALUE klass;
{
NEWOBJ(str, struct RString);
OBJSETUP(str, klass, T_STRING);
str->ptr = 0;
str->len = 0;
str->aux.capa = 0;
return (VALUE)str;
}
static VALUE
str_new(klass, ptr, len)
VALUE klass;
const char *ptr;
long len;
{
VALUE str = rb_obj_alloc(klass);
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
RSTRING(str)->len = len;
RSTRING(str)->aux.capa = len;
RSTRING(str)->ptr = ALLOC_N(char,len+1);
if (ptr) {
memcpy(RSTRING(str)->ptr, ptr, len);
}
RSTRING(str)->ptr[len] = '\0';
return str;
}
VALUE
rb_str_new(ptr, len)
const char *ptr;
long len;
{
return str_new(rb_cString, ptr, len);
}
VALUE
rb_str_new2(ptr)
const char *ptr;
{
if (!ptr) {
rb_raise(rb_eArgError, "NULL pointer given");
}
return rb_str_new(ptr, strlen(ptr));
}
VALUE
rb_tainted_str_new(ptr, len)
const char *ptr;
long len;
{
VALUE str = rb_str_new(ptr, len);
OBJ_TAINT(str);
return str;
}
VALUE
rb_tainted_str_new2(ptr)
const char *ptr;
{
VALUE str = rb_str_new2(ptr);
OBJ_TAINT(str);
return str;
}
static VALUE
str_new3(klass, str)
VALUE klass, str;
{
VALUE str2 = rb_obj_alloc(klass);
RSTRING(str2)->len = RSTRING(str)->len;
RSTRING(str2)->ptr = RSTRING(str)->ptr;
RSTRING(str2)->aux.shared = str;
FL_SET(str2, ELTS_SHARED);
OBJ_INFECT(str2, str);
return str2;
}
VALUE
rb_str_new3(str)
VALUE str;
{
return str_new3(rb_obj_class(str), str);
}
VALUE
rb_str_new4(orig)
VALUE orig;
{
VALUE klass, str;
klass = rb_obj_class(orig);
if (FL_TEST(orig, ELTS_SHARED)) {
str = str_new3(klass, RSTRING(orig)->aux.shared);
}
else if (FL_TEST(orig, STR_ASSOC)) {
str = str_new(klass, RSTRING(orig)->ptr, RSTRING(orig)->len);
}
else {
str = rb_obj_alloc(klass);
RSTRING(str)->len = RSTRING(orig)->len;
RSTRING(str)->ptr = RSTRING(orig)->ptr;
RSTRING(orig)->aux.shared = str;
FL_SET(orig, ELTS_SHARED);
}
OBJ_INFECT(str, orig);
OBJ_FREEZE(str);
return str;
}
VALUE
rb_str_new5(obj, ptr, len)
VALUE obj;
const char *ptr;
long len;
{
return str_new(rb_obj_class(obj), ptr, len);
}
#define STR_BUF_MIN_SIZE 128
VALUE
rb_str_buf_new(capa)
long capa;
{
VALUE str = rb_obj_alloc(rb_cString);
if (capa < STR_BUF_MIN_SIZE)
capa = STR_BUF_MIN_SIZE;
RSTRING(str)->ptr = 0;
RSTRING(str)->len = 0;
RSTRING(str)->aux.capa = capa;
RSTRING(str)->ptr = ALLOC_N(char, capa+1);
RSTRING(str)->ptr[0] = '\0';
return str;
}
VALUE
rb_str_buf_new2(ptr)
const char *ptr;
{
VALUE str;
long len = strlen(ptr);
str = rb_str_buf_new(len + STR_BUF_MIN_SIZE);
rb_str_cat(str, ptr, len);
return str;
}
VALUE
rb_str_to_str(str)
VALUE str;
{
return rb_convert_type(str, T_STRING, "String", "to_str");
}
static void
rb_str_become(str, str2)
VALUE str, str2;
{
if (str == str2) return;
if (NIL_P(str2)) {
RSTRING(str)->ptr = 0;
RSTRING(str)->len = 0;
RSTRING(str)->aux.capa = 0;
return;
}
if (FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr);
RSTRING(str)->ptr = RSTRING(str2)->ptr;
RSTRING(str)->len = RSTRING(str2)->len;
if (FL_TEST(str2, ELTS_SHARED|STR_ASSOC)) {
FL_SET(str, RBASIC(str2)->flags & (ELTS_SHARED|STR_ASSOC));
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
}
else {
RSTRING(str)->aux.capa = RSTRING(str2)->aux.capa;
}
RSTRING(str2)->ptr = 0; /* abandon str2 */
RSTRING(str2)->len = 0;
RSTRING(str2)->aux.capa = 0;
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
if (OBJ_TAINTED(str2)) OBJ_TAINT(str);
}
void
rb_str_associate(str, add)
VALUE str, add;
{
if (FL_TEST(str, STR_ASSOC)) {
/* already associated */
rb_ary_concat(RSTRING(str)->aux.shared, add);
}
else {
if (FL_TEST(str, ELTS_SHARED)) {
rb_str_modify(str);
}
else if (RSTRING(str)->aux.shared) {
/* str_buf */
if (RSTRING(str)->aux.capa != RSTRING(str)->len) {
RESIZE_CAPA(str, RSTRING(str)->len);
}
}
RSTRING(str)->aux.shared = add;
FL_UNSET(str, ELTS_SHARED);
FL_SET(str, STR_ASSOC);
}
}
VALUE
rb_str_associated(str)
VALUE str;
{
if (FL_TEST(str, STR_ASSOC)) {
return RSTRING(str)->aux.shared;
}
return Qfalse;
}
static ID id_to_s;
VALUE
rb_obj_as_string(obj)
VALUE obj;
{
VALUE str;
if (TYPE(obj) == T_STRING) {
return obj;
}
str = rb_funcall(obj, id_to_s, 0);
if (TYPE(str) != T_STRING)
return rb_any_to_s(obj);
if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
return str;
}
static VALUE
str_copy(str, clone)
VALUE str;
int clone;
{
VALUE str2;
VALUE klass;
int flags;
StringValue(str);
if (FL_TEST(str, ELTS_SHARED)) {
str2 = rb_str_new3(RSTRING(str)->aux.shared);
}
else if (FL_TEST(str, STR_ASSOC)) {
str2 = str_new(RSTRING(str)->ptr, RSTRING(str)->len);
RSTRING(str2)->aux.shared = RSTRING(str)->aux.shared;
}
else if (OBJ_FROZEN(str)) {
str2 = rb_str_new3(str);
}
else {
str2 = rb_str_new3(rb_str_new4(str));
}
flags = FL_TEST(str2, ELTS_SHARED|STR_ASSOC);
if (clone) {
CLONESETUP(str2, str);
}
else {
DUPSETUP(str2, str);
}
if (flags) FL_SET(str2, flags);
return str2;
}
VALUE
rb_str_dup(str)
VALUE str;
{
return str_copy(str, Qfalse);
}
static VALUE
rb_str_clone(str)
VALUE str;
{
return str_copy(str, Qtrue);
}
static VALUE rb_str_replace _((VALUE, VALUE));
static VALUE
rb_str_init(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE orig;
if (rb_scan_args(argc, argv, "01", &orig) == 1)
rb_str_replace(str, orig);
return str;
}
static VALUE
rb_str_length(str)
VALUE str;
{
return INT2NUM(RSTRING(str)->len);
}
static VALUE
rb_str_empty(str)
VALUE str;
{
if (RSTRING(str)->len == 0)
return Qtrue;
return Qfalse;
}
VALUE
rb_str_plus(str1, str2)
VALUE str1, str2;
{
VALUE str3;
StringValue(str2);
str3 = rb_str_new(0, RSTRING(str1)->len+RSTRING(str2)->len);
memcpy(RSTRING(str3)->ptr, RSTRING(str1)->ptr, RSTRING(str1)->len);
memcpy(RSTRING(str3)->ptr + RSTRING(str1)->len,
RSTRING(str2)->ptr, RSTRING(str2)->len);
RSTRING(str3)->ptr[RSTRING(str3)->len] = '\0';
if (OBJ_TAINTED(str1) || OBJ_TAINTED(str2))
OBJ_TAINT(str3);
return str3;
}
VALUE
rb_str_times(str, times)
VALUE str;
VALUE times;
{
VALUE str2;
long i, len;
len = NUM2LONG(times);
if (len == 0) return rb_str_new5(str,0,0);
if (len < 0) {
rb_raise(rb_eArgError, "negative argument");
}
if (LONG_MAX/len < RSTRING(str)->len) {
rb_raise(rb_eArgError, "argument too big");
}
str2 = rb_str_new5(str,0, RSTRING(str)->len*len);
for (i=0; i<len; i++) {
memcpy(RSTRING(str2)->ptr+(i*RSTRING(str)->len),
RSTRING(str)->ptr, RSTRING(str)->len);
}
RSTRING(str2)->ptr[RSTRING(str2)->len] = '\0';
OBJ_INFECT(str2, str);
return str2;
}
static VALUE
rb_str_format(str, arg)
VALUE str, arg;
{
VALUE *argv;
if (TYPE(arg) == T_ARRAY) {
argv = ALLOCA_N(VALUE, RARRAY(arg)->len + 1);
argv[0] = str;
MEMCPY(argv+1, RARRAY(arg)->ptr, VALUE, RARRAY(arg)->len);
return rb_f_sprintf(RARRAY(arg)->len+1, argv);
}
argv = ALLOCA_N(VALUE, 2);
argv[0] = str;
argv[1] = arg;
return rb_f_sprintf(2, argv);
}
static int
str_independent(str)
VALUE str;
{
if (OBJ_FROZEN(str)) rb_error_frozen("string");
if (!OBJ_TAINTED(str) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify string");
if (!FL_TEST(str, ELTS_SHARED)) return 1;
return 0;
}
static void
str_make_independent(str)
VALUE str;
{
char *ptr;
ptr = ALLOC_N(char, RSTRING(str)->len+1);
if (RSTRING(str)->ptr) {
memcpy(ptr, RSTRING(str)->ptr, RSTRING(str)->len);
}
ptr[RSTRING(str)->len] = 0;
RSTRING(str)->ptr = ptr;
RSTRING(str)->aux.capa = RSTRING(str)->len;
FL_UNSET(str, ELTS_SHARED|STR_ASSOC);
}
void
rb_str_modify(str)
VALUE str;
{
if (!str_independent(str))
str_make_independent(str);
}
VALUE
rb_string_value(ptr)
volatile VALUE *ptr;
{
return *ptr = rb_str_to_str(*ptr);
}
VALUE
rb_str_substr(str, beg, len)
VALUE str;
long beg, len;
{
VALUE str2;
if (len < 0) return Qnil;
if (beg > RSTRING(str)->len) return Qnil;
if (beg < 0) {
beg += RSTRING(str)->len;
if (beg < 0) return Qnil;
}
if (beg + len > RSTRING(str)->len) {
len = RSTRING(str)->len - beg;
}
if (len < 0) {
len = 0;
}
if (len == 0) return rb_str_new5(str,0,0);
str2 = rb_str_new5(str,RSTRING(str)->ptr+beg, len);
OBJ_INFECT(str2, str);
return str2;
}
VALUE
rb_str_freeze(str)
VALUE str;
{
return rb_obj_freeze(str);
}
VALUE
rb_str_dup_frozen(str)
VALUE str;
{
if (FL_TEST(str, ELTS_SHARED)) {
OBJ_FREEZE(RSTRING(str)->aux.shared);
return RSTRING(str)->aux.shared;
}
if (OBJ_FROZEN(str)) return str;
str = rb_str_dup(str);
OBJ_FREEZE(str);
return str;
}
VALUE
rb_str_resize(str, len)
VALUE str;
long len;
{
if (len != RSTRING(str)->len) {
rb_str_modify(str);
if (len >= 0) {
if (RSTRING(str)->len < len || RSTRING(str)->len - len > 1024) {
RESIZE_CAPA(str, len);
}
RSTRING(str)->len = len;
RSTRING(str)->ptr[len] = '\0'; /* sentinel */
}
}
return str;
}
VALUE
rb_str_buf_cat(str, ptr, len)
VALUE str;
const char *ptr;
long len;
{
long i, capa, total;
if (FL_TEST(str, ELTS_SHARED)) {
rb_str_modify(str);
}
capa = RSTRING(str)->aux.capa;
total = RSTRING(str)->len+len;
if (capa <= total) {
while (total > capa) {
capa = (capa + 1) * 2;
}
RESIZE_CAPA(str, capa);
}
memcpy(RSTRING(str)->ptr + RSTRING(str)->len, ptr, len);
RSTRING(str)->len = total;
RSTRING(str)->ptr[total] = '\0'; /* sentinel */
return str;
}
VALUE
rb_str_buf_cat2(str, ptr)
VALUE str;
const char *ptr;
{
return rb_str_buf_cat(str, ptr, strlen(ptr));
}
VALUE
rb_str_cat(str, ptr, len)
VALUE str;
const char *ptr;
long len;
{
long i, capa;
rb_str_modify(str);
if (len > 0) {
if (!FL_TEST(str, ELTS_SHARED) && !FL_TEST(str, STR_ASSOC)) {
return rb_str_buf_cat(str, ptr, len);
}
RESIZE_CAPA(str, RSTRING(str)->len + len);
if (ptr) {
memcpy(RSTRING(str)->ptr + RSTRING(str)->len, ptr, len);
}
else {
MEMZERO(RSTRING(str)->ptr + RSTRING(str)->len, char, len);
}
RSTRING(str)->len += len;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
}
return str;
}
VALUE
rb_str_cat2(str, ptr)
VALUE str;
const char *ptr;
{
return rb_str_cat(str, ptr, strlen(ptr));
}
VALUE
rb_str_buf_append(str, str2)
VALUE str, str2;
{
long i, capa, len;
if (FL_TEST(str, ELTS_SHARED)) {
rb_str_modify(str);
}
capa = RSTRING(str)->aux.capa;
len = RSTRING(str)->len+RSTRING(str2)->len;
if (capa <= len) {
while (len > capa) {
capa = (capa + 1) * 2;
}
RESIZE_CAPA(str, capa);
}
memcpy(RSTRING(str)->ptr + RSTRING(str)->len,
RSTRING(str2)->ptr, RSTRING(str2)->len);
RSTRING(str)->len += RSTRING(str2)->len;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
return str;
}
VALUE
rb_str_append(str, str2)
VALUE str, str2;
{
long i, capa, len;
StringValue(str2);
rb_str_modify(str);
if (RSTRING(str2)->len > 0) {
len = RSTRING(str)->len+RSTRING(str2)->len;
if (!FL_TEST(str, ELTS_SHARED) && !FL_TEST(str, STR_ASSOC)) {
rb_str_buf_append(str, str2);
OBJ_INFECT(str, str2);
return str;
}
RESIZE_CAPA(str, len);
memcpy(RSTRING(str)->ptr + RSTRING(str)->len,
RSTRING(str2)->ptr, RSTRING(str2)->len);
RSTRING(str)->len += RSTRING(str2)->len;
RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; /* sentinel */
}
OBJ_INFECT(str, str2);
return str;
}
VALUE
rb_str_concat(str1, str2)
VALUE str1, str2;
{
if (FIXNUM_P(str2)) {
int i = FIX2INT(str2);
if (0 <= i && i <= 0xff) { /* byte */
char c = i;
return rb_str_cat(str1, &c, 1);
}
}
str1 = rb_str_append(str1, str2);
return str1;
}
int
rb_str_hash(str)
VALUE str;
{
register long len = RSTRING(str)->len;
register char *p = RSTRING(str)->ptr;
register int key = 0;
#ifdef HASH_ELFHASH
register unsigned int g;
while (len--) {
key = (key << 4) + *p++;
if (g = key & 0xF0000000)
key ^= g >> 24;
key &= ~g;
}
#elif HASH_PERL
while (len--) {
key = key*33 + *p++;
}
key = key + (key>>5);
#else
while (len--) {
key = key*65599 + *p;
p++;
}
key = key + (key>>5);
#endif
return key;
}
static VALUE
rb_str_hash_m(str)
VALUE str;
{
int key = rb_str_hash(str);
return INT2FIX(key);
}
#define lesser(a,b) (((a)>(b))?(b):(a))
int
rb_str_cmp(str1, str2)
VALUE str1, str2;
{
long len;
int retval;
len = lesser(RSTRING(str1)->len, RSTRING(str2)->len);
retval = rb_memcmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr, len);
if (retval == 0) {
if (RSTRING(str1)->len == RSTRING(str2)->len) return 0;
if (RSTRING(str1)->len > RSTRING(str2)->len) return 1;
return -1;
}
if (retval > 0) return 1;
return -1;
}
static VALUE
rb_str_equal(str1, str2)
VALUE str1, str2;
{
if (str1 == str2) return Qtrue;
if (TYPE(str2) != T_STRING) {
str2 = rb_check_convert_type(str2, T_STRING, "String", "to_str");
if (NIL_P(str2)) return Qfalse;
}
if (RSTRING(str1)->len == RSTRING(str2)->len
&& rb_str_cmp(str1, str2) == 0) {
return Qtrue;
}
return Qfalse;
}
static VALUE
rb_str_eql(str1, str2)
VALUE str1, str2;
{
if (TYPE(str2) != T_STRING || RSTRING(str1)->len != RSTRING(str2)->len)
return Qfalse;
if (memcmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr,
lesser(RSTRING(str1)->len, RSTRING(str2)->len)) == 0)
return Qtrue;
return Qfalse;
}
static VALUE
rb_str_cmp_m(str1, str2)
VALUE str1, str2;
{
int result;
StringValue(str2);
result = rb_str_cmp(str1, str2);
return INT2FIX(result);
}
static VALUE
rb_str_casecmp(str1, str2)
VALUE str1, str2;
{
long len;
int retval;
StringValue(str2);
len = lesser(RSTRING(str1)->len, RSTRING(str2)->len);
retval = rb_memcicmp(RSTRING(str1)->ptr, RSTRING(str2)->ptr, len);
if (retval == 0) {
if (RSTRING(str1)->len == RSTRING(str2)->len) return INT2FIX(0);
if (RSTRING(str1)->len > RSTRING(str2)->len) return INT2FIX(1);
return INT2FIX(-1);
}
if (retval == 0) return INT2FIX(0);
if (retval > 0) return INT2FIX(1);
return INT2FIX(-1);
}
static VALUE
rb_str_match(x, y)
VALUE x, y;
{
VALUE reg;
long start;
switch (TYPE(y)) {
case T_REGEXP:
return rb_reg_match(y, x);
case T_STRING:
reg = rb_reg_regcomp(y);
start = rb_reg_search(reg, x, 0, 0);
if (start == -1) {
return Qnil;
}
return INT2NUM(start);
default:
return rb_funcall(y, rb_intern("=~"), 1, x);
}
}
static VALUE
rb_str_match2(str)
VALUE str;
{
StringValue(str);
return rb_reg_match2(rb_reg_regcomp(str));
}
static VALUE
rb_str_match_m(str, re)
VALUE str, re;
{
VALUE str2 = rb_check_convert_type(re, T_STRING, "String", "to_str");
if (!NIL_P(str2)) {
StringValue(re);
re = rb_reg_regcomp(re);
}
return rb_funcall(re, rb_intern("match"), 1, str);
}
static long
rb_str_index(str, sub, offset)
VALUE str, sub;
long offset;
{
char *s, *e, *p;
long len;
if (offset < 0) {
offset += RSTRING(str)->len;
if (offset < 0) return -1;
}
if (RSTRING(str)->len - offset < RSTRING(sub)->len) return -1;
s = RSTRING(str)->ptr+offset;
p = RSTRING(sub)->ptr;
len = RSTRING(sub)->len;
if (len == 0) return offset;
e = RSTRING(str)->ptr + RSTRING(str)->len - len + 1;
while (s < e) {
if (rb_memcmp(s, p, len) == 0) {
return (s-(RSTRING(str)->ptr));
}
s++;
}
return -1;
}
static VALUE
rb_str_index_m(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE sub;
VALUE initpos;
long pos;
if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) {
pos = NUM2LONG(initpos);
}
else {
pos = 0;
}
if (pos < 0) {
pos += RSTRING(str)->len;
if (pos < 0) {
if (TYPE(sub) == T_REGEXP) {
rb_backref_set(Qnil);
}
return Qnil;
}
}
switch (TYPE(sub)) {
case T_REGEXP:
pos = rb_reg_adjust_startpos(sub, str, pos, 0);
pos = rb_reg_search(sub, str, pos, 0);
break;
case T_STRING:
pos = rb_str_index(str, sub, pos);
break;
case T_FIXNUM:
{
int c = FIX2INT(sub);
long len = RSTRING(str)->len;
char *p = RSTRING(str)->ptr;
for (;pos<len;pos++) {
if (p[pos] == c) return INT2NUM(pos);
}
return Qnil;
}
default:
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_class2name(CLASS_OF(sub)));
}
if (pos == -1) return Qnil;
return INT2NUM(pos);
}
static VALUE
rb_str_rindex(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE sub;
VALUE position;
int pos, len;
char *s, *sbeg, *t;
if (rb_scan_args(argc, argv, "11", &sub, &position) == 2) {
pos = NUM2INT(position);
if (pos < 0) {
pos += RSTRING(str)->len;
if (pos < 0) {
if (TYPE(sub) == T_REGEXP) {
rb_backref_set(Qnil);
}
return Qnil;
}
}
if (pos > RSTRING(str)->len) pos = RSTRING(str)->len;
}
else {
pos = RSTRING(str)->len;
}
switch (TYPE(sub)) {
case T_REGEXP:
if (RREGEXP(sub)->len) {
pos = rb_reg_adjust_startpos(sub, str, pos, 1);
pos = rb_reg_search(sub, str, pos, 1);
}
if (pos >= 0) return INT2NUM(pos);
break;
case T_STRING:
len = RSTRING(sub)->len;
/* substring longer than string */
if (RSTRING(str)->len < len) return Qnil;
if (RSTRING(str)->len - pos < len) {
pos = RSTRING(str)->len - len;
}
sbeg = RSTRING(str)->ptr;
s = RSTRING(str)->ptr + pos;
t = RSTRING(sub)->ptr;
if (len) {
while (sbeg <= s) {
if (rb_memcmp(s, t, len) == 0) {
return INT2NUM(s - RSTRING(str)->ptr);
}
s--;
}
}
else {
return INT2NUM(pos);
}
break;
case T_FIXNUM:
{
int c = FIX2INT(sub);
char *p = RSTRING(str)->ptr + pos;
char *pbeg = RSTRING(str)->ptr;
while (pbeg <= p) {
if (*p == c) return INT2NUM(p - RSTRING(str)->ptr);