FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mruby/src/string.c at master · tmash06/mruby · GitHub
tmash06
/
mruby
Public
forked from
mruby/mruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
mruby
/
src
/
string.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2617 lines (2366 loc) · 65.6 KB
Breadcrumbs
mruby
/
src
/
string.c
Copy path
File metadata and controls
2617 lines (2366 loc) · 65.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
*/
#include
<ctype.h>
#include
<limits.h>
#include
<stddef.h>
#include
<stdlib.h>
#include
<string.h>
#include
"mruby.h"
#include
"mruby/array.h"
#include
"mruby/class.h"
#include
"mruby/range.h"
#include
"mruby/string.h"
#include
"re.h"
#define
STR_EMBED_P
(
s
) ((s)->flags & MRB_STR_EMBED)
#define
STR_SET_EMBED_FLAG
(
s
) ((s)->flags |= MRB_STR_EMBED)
#define
STR_UNSET_EMBED_FLAG
(
s
) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
#define
STR_SET_EMBED_LEN
(
s
,
n
) do {\
mrb_int tmp_n = (n);\
s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
} while (0)
#define
STR_SET_LEN
(
s
,
n
) do {\
if (STR_EMBED_P(s)) {\
STR_SET_EMBED_LEN((s),(n));\
} else {\
s->as.heap.len = (n);\
}\
} while (0)
#define
RSTRING_EMBED_LEN
(
s
) \
(mrb_int)((RSTRING(s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define
STR_EMBED_LEN
(
s
)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define
STR_PTR
(
s
) ((STR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define
STR_LEN
(
s
) ((STR_EMBED_P(s)) ? STR_EMBED_LEN(s) : (s)->as.heap.len)
const
char
mrb_digitmap
[]
=
"0123456789abcdefghijklmnopqrstuvwxyz"
;
typedef
struct
mrb_shared_string
{
mrb_bool
nofree
:
1
;
int
refcnt
;
char
*
ptr
;
mrb_int
len
;
}
mrb_shared_string
;
#define
STR_SHARED_P
(
s
) ((s)->flags & MRB_STR_SHARED)
#define
STR_SET_SHARED_FLAG
(
s
) ((s)->flags |= MRB_STR_SHARED)
#define
STR_UNSET_SHARED_FLAG
(
s
) ((s)->flags &= ~MRB_STR_SHARED)
static
mrb_value
str_replace
(
mrb_state
*
mrb
,
struct
RString
*
s1
,
struct
RString
*
s2
);
static
mrb_value
mrb_str_subseq
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_int
beg
,
mrb_int
len
);
mrb_int
mrb_str_strlen
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
mrb_int
i
,
max
=
STR_LEN
(
s
);
char
*
p
=
STR_PTR
(
s
);
if
(!
p
)
return
0
;
for
(
i
=
0
;
i
<
max
;
i
++
) {
if
(
p
[
i
]
==
'\0'
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string contains null byte"
);
}
}
return
max
;
}
#define
RESIZE_CAPA
(
s
,
capacity
) do {\
if (STR_EMBED_P(s)) {\
if (RSTRING_EMBED_LEN_MAX < (capacity)) {\
char *const tmp = (char *)mrb_malloc(mrb, (capacity)+1);\
const mrb_int len = STR_EMBED_LEN(s);\
memcpy(tmp, s->as.ary, len);\
STR_UNSET_EMBED_FLAG(s);\
s->as.heap.ptr = tmp;\
s->as.heap.len = len;\
s->as.heap.aux.capa = (capacity);\
}\
} else {\
s->as.heap.ptr = (char *)mrb_realloc(mrb, STR_PTR(s), (capacity)+1);\
s->as.heap.aux.capa = capacity;\
}\
} while(0)
static
void
str_decref
(
mrb_state
*
mrb
,
mrb_shared_string
*
shared
)
{
shared
->
refcnt
--
;
if
(
shared
->
refcnt
==
0
) {
if
(!
shared
->
nofree
) {
mrb_free
(
mrb
,
shared
->
ptr
);
}
mrb_free
(
mrb
,
shared
);
}
}
void
mrb_str_modify
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
if
(
STR_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
.
ptr
=
shared
->
ptr
;
s
->
as
.
heap
.
aux
.
capa
=
shared
->
len
;
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
mrb_free
(
mrb
,
shared
);
}
else
{
char
*
ptr
,
*
p
;
mrb_int
len
;
p
=
STR_PTR
(
s
);
len
=
s
->
as
.
heap
.
len
;
ptr
=
(
char
*
)
mrb_malloc
(
mrb
, (
size_t
)
len
+
1
);
if
(
p
) {
memcpy
(
ptr
,
p
,
len
);
}
ptr
[
len
]
=
'\0'
;
s
->
as
.
heap
.
ptr
=
ptr
;
s
->
as
.
heap
.
aux
.
capa
=
len
;
str_decref
(
mrb
,
shared
);
}
STR_UNSET_SHARED_FLAG
(
s
);
return
;
}
if
(
s
->
flags
&
MRB_STR_NOFREE
) {
char
*
p
=
STR_PTR
(
s
);
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
, (
size_t
)
s
->
as
.
heap
.
len
+
1
);
if
(
p
) {
memcpy
(
STR_PTR
(
s
),
p
,
s
->
as
.
heap
.
len
);
}
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
s
->
as
.
heap
.
aux
.
capa
=
s
->
as
.
heap
.
len
;
s
->
flags
&= ~
MRB_STR_NOFREE
;
return
;
}
}
mrb_value
mrb_str_resize
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_int
len
)
{
int
slen
;
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
slen
=
STR_LEN
(
s
);
if
(
len
!=
slen
) {
if
(
slen
<
len
||
slen
-
len
>
256
) {
RESIZE_CAPA
(
s
,
len
);
}
STR_SET_LEN
(
s
,
len
);
STR_PTR
(
s
)[
len
]
=
'\0'
;
/* sentinel */
}
return
str
;
}
#define
mrb_obj_alloc_string
(
mrb
) ((struct RString*)mrb_obj_alloc((mrb), MRB_TT_STRING, (mrb)->string_class))
/* char offset to byte offset */
int
mrb_str_offset
(
mrb_state
*
mrb
,
mrb_value
str
,
int
pos
)
{
return
pos
;
}
static
struct
RString
*
str_new
(
mrb_state
*
mrb
,
const
char
*
p
,
mrb_int
len
)
{
struct
RString
*
s
;
s
=
mrb_obj_alloc_string
(
mrb
);
if
(
len
<
RSTRING_EMBED_LEN_MAX
) {
STR_SET_EMBED_FLAG
(
s
);
STR_SET_EMBED_LEN
(
s
,
len
);
if
(
p
) {
memcpy
(
s
->
as
.
ary
,
p
,
len
);
}
}
else
{
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
aux
.
capa
=
len
;
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
, (
size_t
)
len
+
1
);
if
(
p
) {
memcpy
(
s
->
as
.
heap
.
ptr
,
p
,
len
);
}
}
STR_PTR
(
s
)[
len
]
=
'\0'
;
return
s
;
}
static
void
str_with_class
(
mrb_state
*
mrb
,
struct
RString
*
s
,
mrb_value
obj
)
{
s
->
c
=
mrb_str_ptr
(
obj
)
->
c
;
}
static
mrb_value
mrb_str_new_empty
(
mrb_state
*
mrb
,
mrb_value
str
)
{
struct
RString
*
s
=
str_new
(
mrb
,
0
,
0
);
str_with_class
(
mrb
,
s
,
str
);
return
mrb_obj_value
(
s
);
}
#ifndef
MRB_STR_BUF_MIN_SIZE
# define
MRB_STR_BUF_MIN_SIZE
128
#endif
mrb_value
mrb_str_buf_new
(
mrb_state
*
mrb
,
mrb_int
capa
)
{
struct
RString
*
s
;
s
=
mrb_obj_alloc_string
(
mrb
);
if
(
capa
<
MRB_STR_BUF_MIN_SIZE
) {
capa
=
MRB_STR_BUF_MIN_SIZE
;
}
s
->
as
.
heap
.
len
=
0
;
s
->
as
.
heap
.
aux
.
capa
=
capa
;
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
capa
+
1
);
STR_PTR
(
s
)[
0
]
=
'\0'
;
return
mrb_obj_value
(
s
);
}
static
void
str_buf_cat
(
mrb_state
*
mrb
,
struct
RString
*
s
,
const
char
*
ptr
,
size_t
len
)
{
mrb_int
capa
;
mrb_int
total
;
ptrdiff_t
off
=
-1
;
mrb_str_modify
(
mrb
,
s
);
if
(
ptr
>=
STR_PTR
(
s
)
&&
ptr
<=
STR_PTR
(
s
)
+
STR_LEN
(
s
)) {
off
=
ptr
-
STR_PTR
(
s
);
}
if
(
len
==
0
)
return
;
if
(
STR_EMBED_P
(
s
))
capa
=
RSTRING_EMBED_LEN_MAX
;
else
capa
=
s
->
as
.
heap
.
aux
.
capa
;
if
(
STR_LEN
(
s
) >=
MRB_INT_MAX
-
(
mrb_int
)
len
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string sizes too big"
);
}
total
=
STR_LEN
(
s
)
+
len
;
if
(
capa
<=
total
) {
while
(
total
>
capa
) {
if
(
capa
+
1
>=
MRB_INT_MAX
/
2
) {
capa
=
(
total
+
4095
) /
4096
;
break
;
}
capa
=
(
capa
+
1
)
*
2
;
}
RESIZE_CAPA
(
s
,
capa
);
}
if
(
off
!=
-1
) {
ptr
=
STR_PTR
(
s
)
+
off
;
}
memcpy
(
STR_PTR
(
s
)
+
STR_LEN
(
s
),
ptr
,
len
);
STR_SET_LEN
(
s
,
total
);
STR_PTR
(
s
)[
total
]
=
'\0'
;
/* sentinel */
}
mrb_value
mrb_str_buf_cat
(
mrb_state
*
mrb
,
mrb_value
str
,
const
char
*
ptr
,
size_t
len
)
{
if
(
len
==
0
)
return
str
;
str_buf_cat
(
mrb
,
mrb_str_ptr
(
str
),
ptr
,
len
);
return
str
;
}
mrb_value
mrb_str_new
(
mrb_state
*
mrb
,
const
char
*
p
,
size_t
len
)
{
struct
RString
*
s
;
if
((
mrb_int
)
len
<
0
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"negative string size (or size too big)"
);
}
s
=
str_new
(
mrb
,
p
,
len
);
return
mrb_obj_value
(
s
);
}
/*
* call-seq: (Caution! NULL string)
* String.new(str="") => new_str
*
* Returns a new string object containing a copy of <i>str</i>.
*/
mrb_value
mrb_str_new_cstr
(
mrb_state
*
mrb
,
const
char
*
p
)
{
struct
RString
*
s
;
size_t
len
;
if
(
p
) {
len
=
strlen
(
p
);
if
((
mrb_int
)
len
<
0
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"argument too big"
);
}
}
else
{
len
=
0
;
}
s
=
str_new
(
mrb
,
p
,
len
);
return
mrb_obj_value
(
s
);
}
mrb_value
mrb_str_new_static
(
mrb_state
*
mrb
,
const
char
*
p
,
size_t
len
)
{
struct
RString
*
s
;
if
((
mrb_int
)
len
<
0
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"negative string size (or size too big)"
);
}
s
=
mrb_obj_alloc_string
(
mrb
);
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
aux
.
capa
=
0
;
/* nofree */
s
->
as
.
heap
.
ptr
=
(
char
*
)
p
;
s
->
flags
=
MRB_STR_NOFREE
;
return
mrb_obj_value
(
s
);
}
void
mrb_gc_free_str
(
mrb_state
*
mrb
,
struct
RString
*
str
)
{
if
(
STR_EMBED_P
(
str
))
/* no code */
;
else
if
(
STR_SHARED_P
(
str
))
str_decref
(
mrb
,
str
->
as
.
heap
.
aux
.
shared
);
else
if
((
str
->
flags
&
MRB_STR_NOFREE
)
==
0
)
mrb_free
(
mrb
,
str
->
as
.
heap
.
ptr
);
}
char
*
mrb_str_to_cstr
(
mrb_state
*
mrb
,
mrb_value
str0
)
{
struct
RString
*
s
;
if
(!
mrb_string_p
(
str0
)) {
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"expected String"
);
}
s
=
str_new
(
mrb
,
RSTRING_PTR
(
str0
),
RSTRING_LEN
(
str0
));
if
((
strlen
(
STR_PTR
(
s
)) ^
STR_LEN
(
s
))
!=
0
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string contains null byte"
);
}
return
STR_PTR
(
s
);
}
static
void
str_make_shared
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
if
(!
STR_SHARED_P
(
s
)) {
mrb_shared_string
*
shared
=
(
mrb_shared_string
*
)
mrb_malloc
(
mrb
,
sizeof
(
mrb_shared_string
));
shared
->
refcnt
=
1
;
if
(
STR_EMBED_P
(
s
)) {
const
mrb_int
len
=
STR_EMBED_LEN
(
s
);
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
len
+
1
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
tmp
[
len
]
=
'\0'
;
STR_UNSET_EMBED_FLAG
(
s
);
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
len
=
len
;
shared
->
nofree
=
FALSE;
shared
->
ptr
=
s
->
as
.
heap
.
ptr
;
}
else
if
(
s
->
flags
&
MRB_STR_NOFREE
) {
shared
->
nofree
=
TRUE;
shared
->
ptr
=
STR_PTR
(
s
);
s
->
flags
&= ~
MRB_STR_NOFREE
;
}
else
{
shared
->
nofree
=
FALSE;
if
(
s
->
as
.
heap
.
aux
.
capa
>
s
->
as
.
heap
.
len
) {
s
->
as
.
heap
.
ptr
=
shared
->
ptr
=
(
char
*
)
mrb_realloc
(
mrb
,
STR_PTR
(
s
),
s
->
as
.
heap
.
len
+
1
);
}
else
{
shared
->
ptr
=
STR_PTR
(
s
);
}
}
shared
->
len
=
s
->
as
.
heap
.
len
;
s
->
as
.
heap
.
aux
.
shared
=
shared
;
STR_SET_SHARED_FLAG
(
s
);
}
}
/*
* call-seq:
* char* str = String("abcd"), len=strlen("abcd")
*
* Returns a new string object containing a copy of <i>str</i>.
*/
const
char
*
mrb_str_body
(
mrb_value
str
,
int
*
len_p
)
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
*
len_p
=
STR_LEN
(
s
);
return
STR_PTR
(
s
);
}
/*
* call-seq: (Caution! String("abcd") change)
* String("abcdefg") = String("abcd") + String("efg")
*
* Returns a new string object containing a copy of <i>str</i>.
*/
void
mrb_str_concat
(
mrb_state
*
mrb
,
mrb_value
self
,
mrb_value
other
)
{
struct
RString
*
s1
=
mrb_str_ptr
(
self
),
*
s2
;
mrb_int
len
;
mrb_str_modify
(
mrb
,
s1
);
if
(!
mrb_string_p
(
other
)) {
other
=
mrb_str_to_str
(
mrb
,
other
);
}
s2
=
mrb_str_ptr
(
other
);
len
=
STR_LEN
(
s1
)
+
STR_LEN
(
s2
);
if
(
RSTRING_CAPA
(
self
)
<
len
) {
RESIZE_CAPA
(
s1
,
len
);
}
memcpy
(
STR_PTR
(
s1
)
+
STR_LEN
(
s1
),
STR_PTR
(
s2
),
STR_LEN
(
s2
));
STR_SET_LEN
(
s1
,
len
);
STR_PTR
(
s1
)[
len
]
=
'\0'
;
}
/*
* call-seq: (Caution! String("abcd") remain)
* String("abcdefg") = String("abcd") + String("efg")
*
* Returns a new string object containing a copy of <i>str</i>.
*/
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
;
t
=
str_new
(
mrb
,
0
,
STR_LEN
(
s
)
+
STR_LEN
(
s2
));
memcpy
(
STR_PTR
(
t
),
STR_PTR
(
s
),
STR_LEN
(
s
));
memcpy
(
STR_PTR
(
t
)
+
STR_LEN
(
s
),
STR_PTR
(
s2
),
STR_LEN
(
s2
));
return
mrb_obj_value
(
t
);
}
/* 15.2.10.5.2 */
/*
* call-seq: (Caution! String("abcd") remain) for stack_argument
* String("abcdefg") = String("abcd") + String("efg")
*
* Returns a new string object containing a copy of <i>str</i>.
*/
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
);
}
/*
* call-seq:
* len = strlen(String("abcd"))
*
* Returns a new string object containing a copy of <i>str</i>.
*/
static
mrb_value
mrb_str_bytesize
(
mrb_state
*
mrb
,
mrb_value
self
)
{
struct
RString
*
s
=
mrb_str_ptr
(
self
);
return
mrb_fixnum_value
(
STR_LEN
(
s
));
}
/* 15.2.10.5.26 */
/* 15.2.10.5.33 */
/*
* call-seq:
* len = strlen(String("abcd"))
*
* Returns a new string object containing a copy of <i>str</i>.
*/
mrb_value
mrb_str_size
(
mrb_state
*
mrb
,
mrb_value
self
)
{
struct
RString
*
s
=
mrb_str_ptr
(
self
);
return
mrb_fixnum_value
(
STR_LEN
(
s
));
}
/* 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"
,
&
times
);
if
(
times
<
0
) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"negative argument"
);
}
if
(
times
&&
MRB_INT_MAX
/
times
<
RSTRING_LEN
(
self
)) {
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"argument too big"
);
}
len
=
RSTRING_LEN
(
self
)
*
times
;
str2
=
str_new
(
mrb
,
0
,
len
);
str_with_class
(
mrb
,
str2
,
self
);
p
=
STR_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
[
STR_LEN
(
str2
)]
=
'\0'
;
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
*/
int
mrb_str_cmp
(
mrb_state
*
mrb
,
mrb_value
str1
,
mrb_value
str2
)
{
mrb_int
len
;
mrb_int
retval
;
struct
RString
*
s1
=
mrb_str_ptr
(
str1
);
struct
RString
*
s2
=
mrb_str_ptr
(
str2
);
len
=
lesser
(
STR_LEN
(
s1
),
STR_LEN
(
s2
));
retval
=
memcmp
(
STR_PTR
(
s1
),
STR_PTR
(
s2
),
len
);
if
(
retval
==
0
) {
if
(
STR_LEN
(
s1
)
==
STR_LEN
(
s2
))
return
0
;
if
(
STR_LEN
(
s1
)
>
STR_LEN
(
s2
))
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_int
result
;
mrb_get_args
(
mrb
,
"o"
,
&
str2
);
if
(!
mrb_string_p
(
str2
)) {
if
(!
mrb_respond_to
(
mrb
,
str2
,
mrb_intern_lit
(
mrb
,
"to_s"
))) {
return
mrb_nil_value
();
}
else
if
(!
mrb_respond_to
(
mrb
,
str2
,
mrb_intern_lit
(
mrb
,
"<=>"
))) {
return
mrb_nil_value
();
}
else
{
mrb_value
tmp
=
mrb_funcall
(
mrb
,
str2
,
"<=>"
,
1
,
str1
);
if
(
mrb_nil_p
(
tmp
))
return
mrb_nil_value
();
if
(!
mrb_fixnum
(
tmp
)) {
return
mrb_funcall
(
mrb
,
mrb_fixnum_value
(
0
),
"-"
,
1
,
tmp
);
}
result
=
-
mrb_fixnum
(
tmp
);
}
}
else
{
result
=
mrb_str_cmp
(
mrb
,
str1
,
str2
);
}
return
mrb_fixnum_value
(
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_bool
mrb_str_equal
(
mrb_state
*
mrb
,
mrb_value
str1
,
mrb_value
str2
)
{
if
(
mrb_obj_equal
(
mrb
,
str1
,
str2
))
return
TRUE;
if
(!
mrb_string_p
(
str2
)) {
if
(
mrb_nil_p
(
str2
))
return
FALSE;
if
(!
mrb_respond_to
(
mrb
,
str2
,
mrb_intern_lit
(
mrb
,
"to_str"
))) {
return
FALSE;
}
str2
=
mrb_funcall
(
mrb
,
str2
,
"to_str"
,
0
);
return
mrb_equal
(
mrb
,
str2
,
str1
);
}
return
str_eql
(
mrb
,
str1
,
str2
);
}
/* 15.2.10.5.4 */
/*
* call-seq:
* str == obj => true or false
*
* Equality---
* If <i>obj</i> is not a <code>String</code>, returns <code>false</code>.
* Otherwise, returns <code>false</code> or <code>true</code>
*
* caution:if <i>str</i> <code><=></code> <i>obj</i> returns zero.
*/
static
mrb_value
mrb_str_equal_m
(
mrb_state
*
mrb
,
mrb_value
str1
)
{
mrb_value
str2
;
mrb_bool
equal_p
;
mrb_get_args
(
mrb
,
"o"
,
&
str2
);
equal_p
=
mrb_str_equal
(
mrb
,
str1
,
str2
);
return
mrb_bool_value
(
equal_p
);
}
/* ---------------------------------- */
mrb_value
mrb_str_to_str
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_value
s
;
if
(!
mrb_string_p
(
str
)) {
s
=
mrb_check_convert_type
(
mrb
,
str
,
MRB_TT_STRING
,
"String"
,
"to_str"
);
if
(
mrb_nil_p
(
s
)) {
s
=
mrb_convert_type
(
mrb
,
str
,
MRB_TT_STRING
,
"String"
,
"to_s"
);
}
return
s
;
}
return
str
;
}
char
*
mrb_string_value_ptr
(
mrb_state
*
mrb
,
mrb_value
ptr
)
{
mrb_value
str
=
mrb_str_to_str
(
mrb
,
ptr
);
return
RSTRING_PTR
(
str
);
}
static
mrb_value
noregexp
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_raise
(
mrb
,
E_NOTIMP_ERROR
,
"Regexp class not implemented"
);
return
mrb_nil_value
();
}
static
void
regexp_check
(
mrb_state
*
mrb
,
mrb_value
obj
)
{
if
(!
memcmp
(
mrb_obj_classname
(
mrb
,
obj
),
REGEXP_CLASS
,
sizeof
(
REGEXP_CLASS
)
-
1
)) {
noregexp
(
mrb
,
obj
);
}
}
static
inline
mrb_int
mrb_memsearch_qs
(
const
unsigned
char
*
xs
,
mrb_int
m
,
const
unsigned
char
*
ys
,
mrb_int
n
)
{
const
unsigned
char
*
x
=
xs
,
*
xe
=
xs
+
m
;
const
unsigned
char
*
y
=
ys
;
int
i
,
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
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
=
y
,
*
ye
=
ys
+
n
;
for
(;
y
<
ye
;
++
y
) {
if
(
*
x
==
*
y
)
return
y
-
ys
;
}
return
-1
;
}
return
mrb_memsearch_qs
((
const
unsigned
char
*
)
x0
,
m
, (
const
unsigned
char
*
)
y0
,
n
);
}
static
mrb_int
mrb_str_index
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_value
sub
,
mrb_int
offset
)
{
mrb_int
pos
;
char
*
s
,
*
sptr
;
mrb_int
len
,
slen
;
len
=
RSTRING_LEN
(
str
);
slen
=
RSTRING_LEN
(
sub
);
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 */
sptr
=
RSTRING_PTR
(
sub
);
slen
=
RSTRING_LEN
(
sub
);
len
=
RSTRING_LEN
(
str
)
-
offset
;
pos
=
mrb_memsearch
(
sptr
,
slen
,
s
,
len
);
if
(
pos
<
0
)
return
pos
;
return
pos
+
offset
;
}
mrb_value
mrb_str_dup
(
mrb_state
*
mrb
,
mrb_value
str
)
{
/* should return shared string */
struct
RString
*
s
=
mrb_str_ptr
(
str
);
return
mrb_str_new
(
mrb
,
STR_PTR
(
s
),
STR_LEN
(
s
));
}
static
mrb_value
mrb_str_aref
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_value
indx
)
{
mrb_int
idx
;
regexp_check
(
mrb
,
indx
);
switch
(
mrb_type
(
indx
)) {
case
MRB_TT_FIXNUM
:
idx
=
mrb_fixnum
(
indx
);
num_index
:
str
=
mrb_str_substr
(
mrb
,
str
,
idx
,
1
);
if
(!
mrb_nil_p
(
str
)
&&
RSTRING_LEN
(
str
)
==
0
)
return
mrb_nil_value
();
return
str
;
case
MRB_TT_STRING
:
if
(
mrb_str_index
(
mrb
,
str
,
indx
,
0
)
!=
-1
)
return
mrb_str_dup
(
mrb
,
indx
);
return
mrb_nil_value
();
case
MRB_TT_RANGE
:
/* check if indx is Range */
{
mrb_int
beg
,
len
;
len
=
RSTRING_LEN
(
str
);
if
(
mrb_range_beg_len
(
mrb
,
indx
,
&
beg
,
&
len
,
len
)) {
return
mrb_str_subseq
(
mrb
,
str
,
beg
,
len
);
}
else
{
return
mrb_nil_value
();
}
}
default
:
idx
=
mrb_fixnum
(
indx
);
goto
num_index
;
}
return
mrb_nil_value
();
/* not reached */
}
/* 15.2.10.5.6 */
/* 15.2.10.5.34 */
/*
* call-seq:
* str[fixnum] => fixnum or nil
* str[fixnum, fixnum] => new_str or nil
* str[range] => new_str or nil
* str[regexp] => new_str or nil
* str[regexp, fixnum] => new_str or nil
* str[other_str] => new_str or nil
* str.slice(fixnum) => fixnum or nil
* str.slice(fixnum, fixnum) => new_str or nil
* str.slice(range) => new_str or nil
* str.slice(regexp) => new_str or nil
* str.slice(regexp, fixnum) => new_str or nil
* str.slice(other_str) => new_str or nil
*
* Element Reference---If passed a single <code>Fixnum</code>, returns the code
* of the character at that position. If passed two <code>Fixnum</code>
* objects, returns a substring starting at the offset given by the first, and
* a length given by the second. If given a range, a substring containing
* characters at offsets given by the range is returned. In all three cases, if
* an offset is negative, it is counted from the end of <i>str</i>. Returns
* <code>nil</code> if the initial offset falls outside the string, the length
* is negative, or the beginning of the range is greater than the end.
*
* If a <code>Regexp</code> is supplied, the matching portion of <i>str</i> is
* returned. If a numeric parameter follows the regular expression, that
* component of the <code>MatchData</code> is returned instead. If a
* <code>String</code> is given, that string is returned if it occurs in
* <i>str</i>. In both cases, <code>nil</code> is returned if there is no
* match.
*
* a = "hello there"
* a[1] #=> 101(1.8.7) "e"(1.9.2)
* a[1,3] #=> "ell"
* a[1..3] #=> "ell"
* a[-3,2] #=> "er"
* a[-4..-2] #=> "her"
* a[12..-1] #=> nil
* a[-2..-4] #=> ""
* a[/[aeiou](.)\1/] #=> "ell"
* a[/[aeiou](.)\1/, 0] #=> "ell"
* a[/[aeiou](.)\1/, 1] #=> "l"
* a[/[aeiou](.)\1/, 2] #=> nil
* a["lo"] #=> "lo"
* a["bye"] #=> nil
*/
static
mrb_value
mrb_str_aref_m
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_value
a1
,
a2
;
int
argc
;
argc
=
mrb_get_args
(
mrb
,
"o|o"
,
&
a1
,
&
a2
);
if
(
argc
==
2
) {
regexp_check
(
mrb
,
a1
);
return
mrb_str_substr
(
mrb
,
str
,
mrb_fixnum
(
a1
),
mrb_fixnum
(
a2
));
}
if
(
argc
!=
1
) {
mrb_raisef
(
mrb
,
E_ARGUMENT_ERROR
,
"wrong number of arguments (%S for 1)"
,
mrb_fixnum_value
(
argc
));
}
return
mrb_str_aref
(
mrb
,
str
,
a1
);
}
/* 15.2.10.5.8 */
/*
* call-seq:
* str.capitalize! => str or nil
*
* Modifies <i>str</i> by converting the first character to uppercase and the
* remainder to lowercase. Returns <code>nil</code> if no changes are made.
*
* a = "hello"
* a.capitalize! #=> "Hello"
* a #=> "Hello"
* a.capitalize! #=> nil
*/
static
mrb_value
mrb_str_capitalize_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
{
char
*
p
,
*
pend
;
int
modify
=
0
;
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
if
(
STR_LEN
(
s
)
==
0
||
!
STR_PTR
(
s
))
return
mrb_nil_value
();
p
=
STR_PTR
(
s
);
pend
=
STR_PTR
(
s
)
+
STR_LEN
(
s
);
if
(
ISLOWER
(
*
p
)) {
*
p
=
TOUPPER
(
*
p
);
modify
=
1
;
}
while
(
++
p
<
pend
) {
if
(
ISUPPER
(
*
p
)) {
*
p
=
TOLOWER
(
*
p
);
modify
=
1
;
}
}
if
(
modify
)
return
str
;
return
mrb_nil_value
();
}
/* 15.2.10.5.7 */
/*
* call-seq:
* str.capitalize => new_str
*
* Returns a copy of <i>str</i> with the first character converted to uppercase
* and the remainder to lowercase.
*
* "hello".capitalize #=> "Hello"
* "HELLO".capitalize #=> "Hello"
* "123ABC".capitalize #=> "123abc"
*/
static
mrb_value
mrb_str_capitalize
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
str
;
str
=
mrb_str_dup
(
mrb
,
self
);
mrb_str_capitalize_bang
(
mrb
,
str
);
return
str
;
}
/* 15.2.10.5.10 */
/*
* call-seq:
* str.chomp!(separator=$/) => str or nil
*
* Modifies <i>str</i> in place as described for <code>String#chomp</code>,
* returning <i>str</i>, or <code>nil</code> if no modifications were made.
*/
static
mrb_value
mrb_str_chomp_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_value
rs
;
mrb_int
newline
;
char
*
p
,
*
pp
;
mrb_int
rslen
;
mrb_int
len
;
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
len
=
STR_LEN
(
s
);
if
(
mrb_get_args
(
mrb
,
"|S"
,
&
rs
)
==
0
) {
if
(
len
==
0
)
return
mrb_nil_value
();
smart_chomp
:
if
(
STR_PTR
(
s
)[
len
-
1
]
==
'\n'
) {
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
if
(
STR_LEN
(
s
)
>
0
&&
STR_PTR
(
s
)[
STR_LEN
(
s
)
-
1
]
==
'\r'
) {
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
}
}
else
if
(
STR_PTR
(
s
)[
len
-
1
]
==
'\r'
) {
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL