forked from mruby/mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
3101 lines (2820 loc) · 78.8 KB
/
string.c
File metadata and controls
3101 lines (2820 loc) · 78.8 KB
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 - String class
**
** See Copyright Notice in mruby.h
*/
#ifdef _MSC_VER
# define _CRT_NONSTDC_NO_DEPRECATE
#endif
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/range.h>
#include <mruby/string.h>
#include <mruby/numeric.h>
#include <mruby/internal.h>
#include <mruby/presym.h>
#include <string.h>
typedef struct mrb_shared_string {
int refcnt;
mrb_int capa;
char *ptr;
} mrb_shared_string;
const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
#define mrb_obj_alloc_string(mrb) MRB_OBJ_ALLOC((mrb), MRB_TT_STRING, (mrb)->string_class)
#ifndef MRB_STR_LENGTH_MAX
#define MRB_STR_LENGTH_MAX 1048576
#endif
static void
str_check_length(mrb_state *mrb, mrb_int len)
{
if (len < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative (or overflowed) string size");
}
#if MRB_STR_LENGTH_MAX != 0
if (len > MRB_STR_LENGTH_MAX-1) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "string too long (len=%i max=" MRB_STRINGIZE(MRB_STR_LENGTH_MAX) ")", len);
}
#endif
}
static struct RString*
str_init_normal_capa(mrb_state *mrb, struct RString *s,
const char *p, mrb_int len, mrb_int capa)
{
str_check_length(mrb, capa);
char *dst = (char*)mrb_malloc(mrb, capa + 1);
if (p) memcpy(dst, p, len);
dst[len] = '\0';
s->as.heap.ptr = dst;
s->as.heap.len = len;
s->as.heap.aux.capa = capa;
RSTR_UNSET_TYPE_FLAG(s);
return s;
}
static struct RString*
str_init_normal(mrb_state *mrb, struct RString *s, const char *p, mrb_int len)
{
return str_init_normal_capa(mrb, s, p, len, len);
}
static struct RString*
str_init_embed(struct RString *s, const char *p, mrb_int len)
{
mrb_assert(len >= 0);
if (p) memcpy(RSTR_EMBED_PTR(s), p, len);
RSTR_EMBED_PTR(s)[len] = '\0';
RSTR_SET_TYPE_FLAG(s, EMBED);
RSTR_SET_EMBED_LEN(s, len);
return s;
}
static struct RString*
str_init_nofree(struct RString *s, const char *p, mrb_int len)
{
s->as.heap.ptr = (char*)p;
s->as.heap.len = len;
s->as.heap.aux.capa = 0; /* nofree */
RSTR_SET_TYPE_FLAG(s, NOFREE);
return s;
}
static struct RString*
str_init_shared(mrb_state *mrb, const struct RString *orig, struct RString *s, mrb_shared_string *shared)
{
if (shared) {
shared->refcnt++;
}
else {
shared = (mrb_shared_string*)mrb_malloc(mrb, sizeof(mrb_shared_string));
shared->refcnt = 1;
shared->ptr = orig->as.heap.ptr;
shared->capa = orig->as.heap.aux.capa;
}
s->as.heap.ptr = orig->as.heap.ptr;
s->as.heap.len = orig->as.heap.len;
s->as.heap.aux.shared = shared;
RSTR_SET_TYPE_FLAG(s, SHARED);
return s;
}
static struct RString*
str_init_fshared(const struct RString *orig, struct RString *s, struct RString *fshared)
{
s->as.heap.ptr = orig->as.heap.ptr;
s->as.heap.len = orig->as.heap.len;
s->as.heap.aux.fshared = fshared;
RSTR_SET_TYPE_FLAG(s, FSHARED);
return s;
}
static struct RString*
str_init_modifiable(mrb_state *mrb, struct RString *s, const char *p, mrb_int len)
{
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(s, p, len);
}
return str_init_normal(mrb, s, p, len);
}
static struct RString*
str_new_static(mrb_state *mrb, const char *p, mrb_int len)
{
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
}
return str_init_nofree(mrb_obj_alloc_string(mrb), p, len);
}
static struct RString*
str_new(mrb_state *mrb, const char *p, mrb_int len)
{
str_check_length(mrb, len);
if (RSTR_EMBEDDABLE_P(len)) {
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
}
if (p && mrb_ro_data_p(p)) {
return str_init_nofree(mrb_obj_alloc_string(mrb), p, len);
}
return str_init_normal(mrb, mrb_obj_alloc_string(mrb), p, len);
}
MRB_API mrb_value
mrb_str_new_capa(mrb_state *mrb, mrb_int capa)
{
struct RString *s = mrb_obj_alloc_string(mrb);
if (RSTR_EMBEDDABLE_P(capa)) {
s = str_init_embed(s, NULL, 0);
}
else {
s = str_init_normal_capa(mrb, s, NULL, 0, capa);
}
return mrb_obj_value(s);
}
static void
resize_capa(mrb_state *mrb, struct RString *s, mrb_int capacity)
{
if (RSTR_EMBED_P(s)) {
if (!RSTR_EMBEDDABLE_P(capacity)) {
str_init_normal_capa(mrb, s, RSTR_EMBED_PTR(s), RSTR_EMBED_LEN(s), capacity);
}
}
else {
str_check_length(mrb, capacity);
s->as.heap.ptr = (char*)mrb_realloc(mrb, RSTR_PTR(s), capacity+1);
s->as.heap.aux.capa = (mrb_ssize)capacity;
}
}
MRB_API mrb_value
mrb_str_new(mrb_state *mrb, const char *p, mrb_int len)
{
return mrb_obj_value(str_new(mrb, p, len));
}
MRB_API mrb_value
mrb_str_new_cstr(mrb_state *mrb, const char *p)
{
struct RString *s;
mrb_int len;
if (p) {
len = strlen(p);
}
else {
len = 0;
}
s = str_new(mrb, p, len);
return mrb_obj_value(s);
}
MRB_API mrb_value
mrb_str_new_static(mrb_state *mrb, const char *p, mrb_int len)
{
struct RString *s = str_new_static(mrb, p, len);
return mrb_obj_value(s);
}
static void
str_decref(mrb_state *mrb, mrb_shared_string *shared)
{
shared->refcnt--;
if (shared->refcnt == 0) {
mrb_free(mrb, shared->ptr);
mrb_free(mrb, shared);
}
}
static void
str_modify_keep_ascii(mrb_state *mrb, struct RString *s)
{
if (RSTR_SHARED_P(s)) {
mrb_shared_string *shared = s->as.heap.aux.shared;
if (shared->refcnt == 1 && s->as.heap.ptr == shared->ptr) {
s->as.heap.aux.capa = shared->capa;
s->as.heap.ptr[s->as.heap.len] = '\0';
RSTR_UNSET_SHARED_FLAG(s);
mrb_free(mrb, shared);
}
else {
str_init_modifiable(mrb, s, s->as.heap.ptr, s->as.heap.len);
str_decref(mrb, shared);
}
}
else if (RSTR_NOFREE_P(s) || RSTR_FSHARED_P(s)) {
str_init_modifiable(mrb, s, s->as.heap.ptr, s->as.heap.len);
}
}
static void
check_null_byte(mrb_state *mrb, struct RString *str)
{
if (memchr(RSTR_PTR(str), '\0', RSTR_LEN(str))) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
}
}
void
mrb_gc_free_str(mrb_state *mrb, struct RString *str)
{
if (RSTR_EMBED_P(str))
/* no code */;
else if (RSTR_SHARED_P(str))
str_decref(mrb, str->as.heap.aux.shared);
else if (!RSTR_NOFREE_P(str) && !RSTR_FSHARED_P(str))
mrb_free(mrb, str->as.heap.ptr);
}
#ifdef MRB_UTF8_STRING
#define utf8_islead(c) ((unsigned char)((c)&0xc0) != 0x80)
extern const char mrb_utf8len_table[];
const char mrb_utf8len_table[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
};
mrb_int
mrb_utf8len(const char* p, const char* e)
{
mrb_int len = mrb_utf8len_table[(unsigned char)p[0] >> 3];
if (len > e - p) return 1;
switch (len) {
case 1:
return 1;
case 4:
if (utf8_islead(p[3])) return 1;
case 3:
if (utf8_islead(p[2])) return 1;
case 2:
if (utf8_islead(p[1])) return 1;
}
return len;
}
mrb_int
mrb_utf8_strlen(const char *str, mrb_int byte_len)
{
mrb_int len = 0;
const char *p = str;
const char *e = p + byte_len;
while (p < e) {
if (utf8_islead(*p)) len++;
p++;
}
return len;
}
static mrb_int
utf8_strlen(mrb_value str)
{
struct RString *s = mrb_str_ptr(str);
mrb_int byte_len = RSTR_LEN(s);
if (RSTR_ASCII_P(s)) {
return byte_len;
}
else {
mrb_int utf8_len = mrb_utf8_strlen(RSTR_PTR(s), byte_len);
if (byte_len == utf8_len) RSTR_SET_ASCII_FLAG(s);
return utf8_len;
}
}
#define RSTRING_CHAR_LEN(s) utf8_strlen(s)
/* map character index to byte offset index */
static mrb_int
chars2bytes(mrb_value s, mrb_int off, mrb_int idx)
{
if (RSTR_ASCII_P(mrb_str_ptr(s))) {
return idx;
}
else {
mrb_int i, b, n;
const char *p = RSTRING_PTR(s) + off;
const char *e = RSTRING_END(s);
for (b=i=0; p<e && i<idx; i++) {
n = mrb_utf8len(p, e);
b += n;
p += n;
}
return b;
}
}
/* map byte offset to character index */
static mrb_int
bytes2chars(char *p, mrb_int len, mrb_int bi)
{
const char *e = p + (size_t)len;
const char *pivot = p + bi;
mrb_int i;
for (i = 0; p < pivot; i++) {
p += mrb_utf8len(p, e);
}
if (p != pivot) return -1;
return i;
}
static const char*
char_adjust(const char *beg, const char *end, const char *ptr)
{
ptrdiff_t len = end - ptr;
if (len < 1 || utf8_islead(ptr[0])) return ptr;
if (len > 1 && utf8_islead(ptr[1])) return ptr+1;
if (len > 2 && utf8_islead(ptr[2])) return ptr+2;
if (len > 3 && utf8_islead(ptr[3])) return ptr+3;
return ptr;
}
static const char*
char_backtrack(const char *ptr, const char *end)
{
ptrdiff_t len = end - ptr;
if (len < 1 || utf8_islead(end[-1])) return end-1;
if (len > 1 && utf8_islead(end[-2])) return end-2;
if (len > 2 && utf8_islead(end[-3])) return end-3;
if (len > 3 && utf8_islead(end[-4])) return end-4;
return end - 1;
}
static mrb_int
str_index_str_by_char_search(mrb_state *mrb, const char *p, const char *pend, const char *s, const mrb_int slen, mrb_int off)
{
/* Based on Quick Search algorithm (Boyer-Moore-Horspool algorithm) */
ptrdiff_t qstable[1 << CHAR_BIT];
/* Preprocessing */
{
mrb_int i;
for (i = 0; i < 1 << CHAR_BIT; i++) {
qstable[i] = slen;
}
for (i = 0; i < slen; i++) {
qstable[(unsigned char)s[i]] = slen - (i + 1);
}
}
/* Searching */
while (p < pend && pend - p >= slen) {
const char *pivot;
if (memcmp(p, s, slen) == 0) {
return off;
}
pivot = p + qstable[(unsigned char)p[slen - 1]];
if (pivot >= pend || pivot < p /* overflowed */) { return -1; }
do {
p += mrb_utf8len(p, pend);
off++;
} while (p < pivot);
}
return -1;
}
static mrb_int
str_index_str_by_char(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
{
const char *p = RSTRING_PTR(str);
const char *pend = p + RSTRING_LEN(str);
const char *s = RSTRING_PTR(sub);
const mrb_int slen = RSTRING_LEN(sub);
mrb_int off = pos;
for (; pos > 0; pos --) {
if (pend - p < 1) { return -1; }
p += mrb_utf8len(p, pend);
}
if (slen < 1) { return off; }
return str_index_str_by_char_search(mrb, p, pend, s, slen, off);
}
#define BYTES_ALIGN_CHECK(pos) if (pos < 0) return mrb_nil_value();
#else
#define RSTRING_CHAR_LEN(s) RSTRING_LEN(s)
#define chars2bytes(p, off, ci) (ci)
#define bytes2chars(p, end, bi) (bi)
#define char_adjust(beg, end, ptr) (ptr)
#define char_backtrack(ptr, end) ((end) - 1)
#define BYTES_ALIGN_CHECK(pos)
#define str_index_str_by_char(mrb, str, sub, pos) str_index_str(mrb, str, sub, pos)
#endif
#ifndef MRB_QS_SHORT_STRING_LENGTH
#define MRB_QS_SHORT_STRING_LENGTH 2048
#endif
static inline mrb_int
mrb_memsearch_qs(const unsigned char *xs, mrb_int m, const unsigned char *ys, mrb_int n)
{
if (n + m < MRB_QS_SHORT_STRING_LENGTH) {
const unsigned char *y = ys;
const unsigned char *ye = ys+n-m+1;
for (;;) {
y = (const unsigned char*)memchr(y, xs[0], (size_t)(ye-y));
if (y == NULL) return -1;
if (memcmp(xs, y, m) == 0) {
return (mrb_int)(y - ys);
}
y++;
}
return -1;
}
else {
const unsigned char *x = xs, *xe = xs + m;
const unsigned char *y = ys;
int i;
ptrdiff_t qstable[256];
/* Preprocessing */
for (i = 0; i < 256; i++)
qstable[i] = m + 1;
for (; x < xe; x++)
qstable[*x] = xe - x;
/* Searching */
for (; y + m <= ys + n; y += *(qstable + y[m])) {
if (*xs == *y && memcmp(xs, y, m) == 0)
return (mrb_int)(y - ys);
}
return -1;
}
}
static mrb_int
mrb_memsearch(const void *x0, mrb_int m, const void *y0, mrb_int n)
{
const unsigned char *x = (const unsigned char*)x0, *y = (const unsigned char*)y0;
if (m > n) return -1;
else if (m == n) {
return memcmp(x0, y0, m) == 0 ? 0 : -1;
}
else if (m < 1) {
return 0;
}
else if (m == 1) {
const unsigned char *ys = (const unsigned char*)memchr(y, *x, n);
if (ys)
return (mrb_int)(ys - y);
else
return -1;
}
return mrb_memsearch_qs((const unsigned char*)x0, m, (const unsigned char*)y0, n);
}
static void
str_share(mrb_state *mrb, struct RString *orig, struct RString *s)
{
size_t len = (size_t)orig->as.heap.len;
mrb_assert(!RSTR_EMBED_P(orig));
if (RSTR_NOFREE_P(orig)) {
str_init_nofree(s, orig->as.heap.ptr, len);
}
else if (RSTR_SHARED_P(orig)) {
str_init_shared(mrb, orig, s, orig->as.heap.aux.shared);
}
else if (RSTR_FSHARED_P(orig)) {
str_init_fshared(orig, s, orig->as.heap.aux.fshared);
}
else {
if (orig->as.heap.aux.capa > orig->as.heap.len) {
orig->as.heap.ptr = (char*)mrb_realloc(mrb, orig->as.heap.ptr, len+1);
orig->as.heap.aux.capa = (mrb_ssize)len;
}
str_init_shared(mrb, orig, s, NULL);
str_init_shared(mrb, orig, orig, s->as.heap.aux.shared);
}
}
mrb_value
mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
{
struct RString *orig, *s;
orig = mrb_str_ptr(str);
s = mrb_obj_alloc_string(mrb);
if (RSTR_EMBEDDABLE_P(len)) {
str_init_embed(s, RSTR_PTR(orig)+beg, len);
}
else {
str_share(mrb, orig, s);
s->as.heap.ptr += (mrb_ssize)beg;
s->as.heap.len = (mrb_ssize)len;
}
RSTR_COPY_ASCII_FLAG(s, orig);
return mrb_obj_value(s);
}
static void
str_range_to_bytes(mrb_value str, mrb_int *pos, mrb_int *len)
{
*pos = chars2bytes(str, 0, *pos);
*len = chars2bytes(str, *pos, *len);
}
#ifdef MRB_UTF8_STRING
static inline mrb_value
str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
{
str_range_to_bytes(str, &beg, &len);
return mrb_str_byte_subseq(mrb, str, beg, len);
}
#else
#define str_subseq(mrb, str, beg, len) mrb_str_byte_subseq(mrb, str, beg, len)
#endif
mrb_bool
mrb_str_beg_len(mrb_int str_len, mrb_int *begp, mrb_int *lenp)
{
if (str_len < *begp || *lenp < 0) return FALSE;
if (*begp < 0) {
*begp += str_len;
if (*begp < 0) return FALSE;
}
if (*lenp > str_len - *begp)
*lenp = str_len - *begp;
if (*lenp <= 0) {
*lenp = 0;
}
return TRUE;
}
static mrb_value
str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
{
return mrb_str_beg_len(RSTRING_CHAR_LEN(str), &beg, &len) ?
str_subseq(mrb, str, beg, len) : mrb_nil_value();
}
MRB_API mrb_int
mrb_str_index(mrb_state *mrb, mrb_value str, const char *sptr, mrb_int slen, mrb_int offset)
{
mrb_int pos;
char *s;
mrb_int len;
len = RSTRING_LEN(str);
if (offset < 0) {
offset += len;
if (offset < 0) return -1;
}
if (len - offset < slen) return -1;
s = RSTRING_PTR(str);
if (offset) {
s += offset;
}
if (slen == 0) return offset;
/* need proceed one character at a time */
len = RSTRING_LEN(str) - offset;
pos = mrb_memsearch(sptr, slen, s, len);
if (pos < 0) return pos;
return pos + offset;
}
static mrb_int
str_index_str(mrb_state *mrb, mrb_value str, mrb_value str2, mrb_int offset)
{
const char *ptr;
mrb_int len;
ptr = RSTRING_PTR(str2);
len = RSTRING_LEN(str2);
return mrb_str_index(mrb, str, ptr, len, offset);
}
static mrb_value
str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
{
size_t len;
mrb_check_frozen(mrb, s1);
if (s1 == s2) return mrb_obj_value(s1);
RSTR_COPY_ASCII_FLAG(s1, s2);
if (RSTR_SHARED_P(s1)) {
str_decref(mrb, s1->as.heap.aux.shared);
}
else if (!RSTR_EMBED_P(s1) && !RSTR_NOFREE_P(s1) && !RSTR_FSHARED_P(s1)
&& s1->as.heap.ptr) {
mrb_free(mrb, s1->as.heap.ptr);
}
len = (size_t)RSTR_LEN(s2);
if (RSTR_EMBEDDABLE_P(len)) {
str_init_embed(s1, RSTR_PTR(s2), len);
}
else {
str_share(mrb, s2, s1);
}
return mrb_obj_value(s1);
}
static mrb_int
str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
{
const char *s, *sbeg, *send, *t;
struct RString *ps = mrb_str_ptr(str);
mrb_int len = RSTRING_LEN(sub);
mrb_int slen = RSTR_LEN(ps);
/* substring longer than string */
if (slen < len) return -1;
if (slen - pos < len) {
pos = slen - len;
}
sbeg = RSTR_PTR(ps);
send = sbeg + slen;
s = sbeg + pos;
t = RSTRING_PTR(sub);
if (len) {
s = char_adjust(sbeg, send, s);
while (sbeg <= s) {
if ((mrb_int)(send - s) >= len && memcmp(s, t, len) == 0) {
return (mrb_int)(s - sbeg);
}
s = char_backtrack(sbeg, s);
}
return -1;
}
else {
return pos;
}
}
#ifdef _WIN32
#include <windows.h>
char*
mrb_utf8_from_locale(const char *str, int len)
{
wchar_t* wcsp;
char* mbsp;
int mbssize, wcssize;
if (len == 0)
return strdup("");
if (len == -1)
len = (int)strlen(str);
wcssize = MultiByteToWideChar(GetACP(), 0, str, len, NULL, 0);
wcsp = (wchar_t*) malloc((wcssize + 1) * sizeof(wchar_t));
if (!wcsp)
return NULL;
wcssize = MultiByteToWideChar(GetACP(), 0, str, len, wcsp, wcssize + 1);
wcsp[wcssize] = 0;
mbssize = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) wcsp, -1, NULL, 0, NULL, NULL);
mbsp = (char*) malloc((mbssize + 1));
if (!mbsp) {
free(wcsp);
return NULL;
}
mbssize = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) wcsp, -1, mbsp, mbssize, NULL, NULL);
mbsp[mbssize] = 0;
free(wcsp);
return mbsp;
}
char*
mrb_locale_from_utf8(const char *utf8, int len)
{
wchar_t* wcsp;
char* mbsp;
int mbssize, wcssize;
if (len == 0)
return strdup("");
if (len == -1)
len = (int)strlen(utf8);
wcssize = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
wcsp = (wchar_t*) malloc((wcssize + 1) * sizeof(wchar_t));
if (!wcsp)
return NULL;
wcssize = MultiByteToWideChar(CP_UTF8, 0, utf8, len, wcsp, wcssize + 1);
wcsp[wcssize] = 0;
mbssize = WideCharToMultiByte(GetACP(), 0, (LPCWSTR) wcsp, -1, NULL, 0, NULL, NULL);
mbsp = (char*) malloc((mbssize + 1));
if (!mbsp) {
free(wcsp);
return NULL;
}
mbssize = WideCharToMultiByte(GetACP(), 0, (LPCWSTR) wcsp, -1, mbsp, mbssize, NULL, NULL);
mbsp[mbssize] = 0;
free(wcsp);
return mbsp;
}
#endif
MRB_API void
mrb_str_modify_keep_ascii(mrb_state *mrb, struct RString *s)
{
mrb_check_frozen(mrb, s);
str_modify_keep_ascii(mrb, s);
}
MRB_API void
mrb_str_modify(mrb_state *mrb, struct RString *s)
{
mrb_str_modify_keep_ascii(mrb, s);
RSTR_UNSET_ASCII_FLAG(s);
}
MRB_API mrb_value
mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len)
{
mrb_int slen;
struct RString *s = mrb_str_ptr(str);
str_check_length(mrb, len);
mrb_str_modify(mrb, s);
slen = RSTR_LEN(s);
if (len != slen) {
if (slen < len || slen - len > 256) {
resize_capa(mrb, s, len);
}
RSTR_SET_LEN(s, len);
RSTR_PTR(s)[len] = '\0'; /* sentinel */
}
return str;
}
MRB_API char*
mrb_str_to_cstr(mrb_state *mrb, mrb_value str0)
{
struct RString *s;
const char *p = RSTRING_PTR(str0);
mrb_int len = RSTRING_LEN(str0);
check_null_byte(mrb, RSTRING(str0));
s = str_init_modifiable(mrb, mrb_obj_alloc_string(mrb), p, len);
return RSTR_PTR(s);
}
MRB_API void
mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other)
{
other = mrb_obj_as_string(mrb, other);
mrb_str_cat_str(mrb, self, other);
}
MRB_API mrb_value
mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b)
{
struct RString *s = mrb_str_ptr(a);
struct RString *s2 = mrb_str_ptr(b);
struct RString *t;
mrb_int slen = RSTR_LEN(s);
mrb_int s2len = RSTR_LEN(s2);
const char *p = RSTR_PTR(s);
const char *p2 = RSTR_PTR(s2);
t = str_new(mrb, 0, slen + s2len);
char *pt = RSTR_PTR(t);
memcpy(pt, p, slen);
memcpy(pt + slen, p2, s2len);
return mrb_obj_value(t);
}
/* 15.2.10.5.2 */
/*
* call-seq:
* str + other_str -> new_str
*
* Concatenation---Returns a new <code>String</code> containing
* <i>other_str</i> concatenated to <i>str</i>.
*
* "Hello from " + self.to_s #=> "Hello from main"
*/
static mrb_value
mrb_str_plus_m(mrb_state *mrb, mrb_value self)
{
mrb_value str;
mrb_get_args(mrb, "S", &str);
return mrb_str_plus(mrb, self, str);
}
/* 15.2.10.5.26 */
/* 15.2.10.5.33 */
/*
* call-seq:
* "abcd".size => int
*
* Returns the length of string.
*/
static mrb_value
mrb_str_size(mrb_state *mrb, mrb_value self)
{
mrb_int len = RSTRING_CHAR_LEN(self);
return mrb_int_value(mrb, len);
}
static mrb_value
mrb_str_bytesize(mrb_state *mrb, mrb_value self)
{
mrb_int len = RSTRING_LEN(self);
return mrb_int_value(mrb, len);
}
/* 15.2.10.5.1 */
/*
* call-seq:
* str * integer => new_str
*
* Copy---Returns a new <code>String</code> containing <i>integer</i> copies of
* the receiver.
*
* "Ho! " * 3 #=> "Ho! Ho! Ho! "
*/
static mrb_value
mrb_str_times(mrb_state *mrb, mrb_value self)
{
mrb_int n, len, times;
struct RString *str2;
char *p;
mrb_get_args(mrb, "i", ×);
if (times < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
}
if (mrb_int_mul_overflow(RSTRING_LEN(self), times, &len)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "argument too big");
}
str2 = str_new(mrb, 0, len);
p = RSTR_PTR(str2);
if (len > 0) {
n = RSTRING_LEN(self);
memcpy(p, RSTRING_PTR(self), n);
while (n <= len/2) {
memcpy(p + n, p, n);
n *= 2;
}
memcpy(p + n, p, len-n);
}
p[RSTR_LEN(str2)] = '\0';
RSTR_COPY_ASCII_FLAG(str2, mrb_str_ptr(self));
return mrb_obj_value(str2);
}
/* -------------------------------------------------------------- */
#define lesser(a,b) (((a)>(b))?(b):(a))
/* ---------------------------*/
/*
* call-seq:
* mrb_value str1 <=> mrb_value str2 => int
* > 1
* = 0
* < -1
*/
MRB_API int
mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2)
{
mrb_int len, len1, len2;
mrb_int retval;
struct RString *s1 = mrb_str_ptr(str1);
struct RString *s2 = mrb_str_ptr(str2);
len1 = RSTR_LEN(s1);
len2 = RSTR_LEN(s2);
len = lesser(len1, len2);
retval = memcmp(RSTR_PTR(s1), RSTR_PTR(s2), len);
if (retval == 0) {
if (len1 == len2) return 0;
if (len1 > len2) return 1;
return -1;
}
if (retval > 0) return 1;
return -1;
}
/* 15.2.10.5.3 */
/*
* call-seq:
* str <=> other_str => -1, 0, +1
*
* Comparison---Returns -1 if <i>other_str</i> is less than, 0 if
* <i>other_str</i> is equal to, and +1 if <i>other_str</i> is greater than
* <i>str</i>. If the strings are of different lengths, and the strings are
* equal when compared up to the shortest length, then the longer string is
* considered greater than the shorter one. If the variable <code>$=</code> is
* <code>false</code>, the comparison is based on comparing the binary values
* of each character in the string. In older versions of Ruby, setting
* <code>$=</code> allowed case-insensitive comparisons; this is now deprecated
* in favor of using <code>String#casecmp</code>.
*
* <code><=></code> is the basis for the methods <code><</code>,
* <code><=</code>, <code>></code>, <code>>=</code>, and <code>between?</code>,
* included from module <code>Comparable</code>. The method
* <code>String#==</code> does not use <code>Comparable#==</code>.
*
* "abcdef" <=> "abcde" #=> 1
* "abcdef" <=> "abcdef" #=> 0
* "abcdef" <=> "abcdefg" #=> -1
* "abcdef" <=> "ABCDEF" #=> 1
*/
static mrb_value
mrb_str_cmp_m(mrb_state *mrb, mrb_value str1)
{
mrb_value str2 = mrb_get_arg1(mrb);
mrb_int result;
if (!mrb_string_p(str2)) {
return mrb_nil_value();
}
else {
result = mrb_str_cmp(mrb, str1, str2);
}
return mrb_int_value(mrb, result);
}
static mrb_bool
str_eql(mrb_state *mrb, const mrb_value str1, const mrb_value str2)
{
const mrb_int len = RSTRING_LEN(str1);
if (len != RSTRING_LEN(str2)) return FALSE;
if (memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), (size_t)len) == 0)
return TRUE;
return FALSE;
}
MRB_API mrb_bool
mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2)
{
if (!mrb_string_p(str2)) return FALSE;
return str_eql(mrb, str1, str2);
}
/* 15.2.10.5.4 */
/*
* call-seq: