/
string.rb
2670 lines (2116 loc) · 59.6 KB
/
string.rb
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
class String
include Comparable
attr_reader :num_bytes
attr_reader_specific :num_bytes, :bytesize
attr_writer :encoding
attr_writer :ascii_only
attr_writer :valid_encoding
attr_accessor :data
alias_method :__data__, :data
alias_method :__data__=, :data=
def self.__allocate__
Rubinius.primitive :string_allocate
raise PrimitiveFailure, "String.allocate primitive failed"
end
def self.allocate
str = __allocate__
str.__data__ = Rubinius::ByteArray.allocate_sized(1)
str.num_bytes = 0
str
end
def self.from_codepoint(code, enc)
Rubinius.primitive :string_from_codepoint
raise PrimitiveFailure, "String.from_codepoint primitive failed"
end
def self.pattern(size, str)
Rubinius.primitive :string_pattern
raise PrimitiveFailure, "String.pattern primitive failed"
end
def ascii_only?
Rubinius.primitive :string_ascii_only_p
raise PrimitiveFailure, "String#ascii_only? primitive failed"
end
def encoding
Rubinius.primitive :string_encoding
raise PrimitiveFailure, "String#encoding primitive failed"
end
def ord
Rubinius.primitive :string_codepoint
raise ArgumentError, 'empty string' if empty?
raise ArgumentError, "invalid byte sequence in #{encoding}"
end
def chr_at(byte)
Rubinius.primitive :string_chr_at
raise ArgumentError, "String#chr_at primitive failed"
end
def to_f
Rubinius::Type::coerce_to_float self, false, false
end
alias_method :convert_float, :to_f
def __crypt__(other_str)
Rubinius.primitive :string_crypt
raise PrimitiveFailure, "String#crypt primitive failed"
end
def append(str)
Rubinius.primitive :string_append
raise TypeError, "String#append primitive only accepts Strings"
end
def byte_append(str)
Rubinius.primitive :string_byte_append
raise TypeError, "String#byte_append primitive only accepts Strings"
end
def byteslice(index_or_range, length=undefined)
Rubinius.primitive :string_byte_substring
if index_or_range.kind_of? Range
index = Rubinius::Type.coerce_to index_or_range.begin, Fixnum, :to_int
index += @num_bytes if index < 0
return if index < 0 or index > @num_bytes
finish = Rubinius::Type.coerce_to index_or_range.end, Fixnum, :to_int
finish += @num_bytes if finish < 0
finish += 1 unless index_or_range.exclude_end?
length = finish - index
return byteslice 0, 0 if length < 0
else
index = Rubinius::Type.coerce_to index_or_range, Fixnum, :to_int
index += @num_bytes if index < 0
if undefined.equal?(length)
return if index == @num_bytes
length = 1
else
length = Rubinius::Type.coerce_to length, Fixnum, :to_int
return if length < 0
end
return if index < 0 or index > @num_bytes
end
byteslice index, length
end
def dup
other = Rubinius.invoke_primitive :string_dup, self
Rubinius::Type.object_initialize_dup self, other
other
end
def clone
other = Rubinius.invoke_primitive :string_dup, self
Rubinius.invoke_primitive :object_copy_singleton_class, other, self
Rubinius::Type.object_initialize_clone self, other
other.freeze if frozen?
other
end
def substring(start, count)
Rubinius.primitive :string_substring
raise PrimitiveFailure, "String#substring primitive failed"
end
def find_string(pattern, start)
Rubinius.primitive :string_index
raise PrimitiveFailure, "String#find_string primitive failed"
end
def find_string_reverse(pattern, start)
Rubinius.primitive :string_rindex
raise PrimitiveFailure, "String#find_string_reverse primitive failed"
end
def ==(other)
Rubinius.primitive :string_equal
raise PrimitiveFailure, "String#== primitive failed"
end
def secure_compare(other)
Rubinius.primitive :string_secure_compare
if other.kind_of?(String)
raise PrimitiveFailure, "String#secure_compare primitive failed"
else
secure_compare StringValue(other)
end
end
def find_character(offset)
Rubinius.primitive :string_find_character
raise PrimitiveFailure, "String#find_character primitive failed"
end
def size
Rubinius.primitive :string_size
raise PrimitiveFailure, "String#size primitive failed"
end
alias_method :length, :size
# This is a work-in-progress. String is entirely coded around the idea of
# bytes, but we have to convert to the idea of characters, even for 1.8
# mode, where UTF-8, EUC, and SJIS will be represented properly as encoded
# strings rather than some ad hoc internal state. However, the byte
# characteristic of String is still important. More work remains.
def num_bytes=(bytes)
@num_chars = nil
@num_bytes = bytes
end
# In time, the JIT should be able to handle this as a ruby method.
def transform(tbl)
Rubinius.primitive :string_transform
raise PrimitiveFailure, "String#transform primitive failed"
end
def reverse!
Rubinius.primitive :string_reverse
raise PrimitiveFailure, "String#reverse primitive failed"
end
def valid_encoding?
Rubinius.primitive :string_valid_encoding_p
raise PrimitiveFailure, "String#valid_encoding? primitive failed"
end
##
# Creates a new string from copying _count_ bytes from the
# _start_ of _bytes_.
def self.from_bytearray(bytes, start, count)
Rubinius.primitive :string_from_bytearray
raise PrimitiveFailure, "String.from_bytearray primitive failed"
end
def self.try_convert(obj)
Rubinius::Type.try_convert obj, String, :to_str
end
class << self
def clone
raise TypeError, "Unable to clone/dup String class"
end
alias_method :dup, :clone
end
def initialize(arg = undefined, encoding: nil)
replace arg unless undefined.equal?(arg)
self.force_encoding(encoding) if encoding
self
end
private :initialize
def %(args)
*args = args
ret = Rubinius::Sprinter.get(self).call(*args)
Rubinius::Type.infect ret, self
end
def *(num)
num = Rubinius::Type.coerce_to(num, Integer, :to_int) unless num.kind_of? Integer
if num.kind_of? Bignum
raise RangeError, "bignum too big to convert into `long' (#{num})"
end
if num < 0
raise ArgumentError, "unable to multiple negative times (#{num})"
end
str = self.class.pattern num * @num_bytes, self
return str
end
def +(other)
other = StringValue(other)
Rubinius::Type.compatible_encoding self, other
String.new(self) << other
end
def ==(other)
Rubinius.primitive :string_equal
# Use #=== rather than #kind_of? because other might redefine kind_of?
unless String === other
if other.respond_to?(:to_str)
return other == self
end
return false
end
return false unless @num_bytes == other.bytesize
return false unless Encoding.compatible?(self, other)
return @data.compare_bytes(other.__data__, @num_bytes, other.bytesize) == 0
end
def =~(pattern)
case pattern
when Regexp
match_data = pattern.search_region(self, 0, @num_bytes, true)
Regexp.last_match = match_data
return match_data.begin(0) if match_data
when String
raise TypeError, "type mismatch: String given"
else
pattern =~ self
end
end
def [](index, other = undefined)
Rubinius.primitive :string_aref
unless undefined.equal?(other)
if index.kind_of?(Fixnum) && other.kind_of?(Fixnum)
return substring(index, other)
elsif index.kind_of? Regexp
match, str = subpattern(index, other)
Regexp.last_match = match
return str
else
length = Rubinius::Type.coerce_to(other, Fixnum, :to_int)
start = Rubinius::Type.coerce_to(index, Fixnum, :to_int)
return substring(start, length)
end
end
case index
when Regexp
match_data = index.search_region(self, 0, @num_bytes, true)
Regexp.last_match = match_data
if match_data
result = match_data.to_s
Rubinius::Type.infect result, index
return result
end
when String
return include?(index) ? index.dup : nil
when Range
start = Rubinius::Type.coerce_to index.first, Fixnum, :to_int
length = Rubinius::Type.coerce_to index.last, Fixnum, :to_int
start += size if start < 0
length += size if length < 0
length += 1 unless index.exclude_end?
return "" if start == size
return nil if start < 0 || start > size
length = size if length > size
length = length - start
length = 0 if length < 0
return substring(start, length)
else
index = Rubinius::Type.coerce_to index, Fixnum, :to_int
return self[index]
end
end
alias_method :slice, :[]
def capitalize
return dup if @num_bytes == 0
str = transform(Rubinius::CType::Lowered)
str.modify!
# Now do the actual capitalization
ba = str.__data__
ba[0] = Rubinius::CType.toupper(ba[0])
return str
end
def capitalize!
Rubinius.check_frozen
cap = capitalize()
return nil if cap == self
replace(cap)
return self
end
def casecmp(to)
to = StringValue(to)
order = @num_bytes - to.num_bytes
size = order < 0 ? @num_bytes : to.num_bytes
ctype = Rubinius::CType
i = 0
while i < size
a = @data[i]
b = to.__data__[i]
i += 1
a = ctype.toupper!(a) if ctype.islower(a)
b = ctype.toupper!(b) if ctype.islower(b)
r = a - b
next if r == 0
return r < 0 ? -1 : 1
end
return 0 if order == 0
return order < 0 ? -1 : 1
end
def chomp(separator=$/)
str = dup
str.chomp!(separator) || str
end
def chop
str = dup
str.chop! || str
end
def count(*strings)
raise ArgumentError, "wrong number of Arguments" if strings.empty?
return 0 if @num_bytes == 0
table = count_table(*strings).__data__
count = bytes = 0
while bytes < @num_bytes
count += 1 if table[@data[bytes]] == 1
bytes += find_character(bytes).num_bytes
end
count
end
def crypt(other_str)
other_str = StringValue(other_str)
if other_str.size < 2
raise ArgumentError, "salt must be at least 2 characters"
end
hash = __crypt__(other_str)
Rubinius::Type.infect hash, self
Rubinius::Type.infect hash, other_str
end
def delete(*strings)
str = dup
str.delete!(*strings) || str
end
def delete!(*strings)
raise ArgumentError, "wrong number of arguments" if strings.empty?
table = count_table(*strings).__data__
self.modify!
i = 0
j = -1
while i < @num_bytes
c = @data[i]
unless table[c] == 1
@data[j+=1] = c
end
i += 1
end
if (j += 1) < @num_bytes
self.num_bytes = j
self
else
nil
end
end
def downcase
return dup if @num_bytes == 0
transform(Rubinius::CType::Lowered)
end
def downcase!
Rubinius.check_frozen
return if @num_bytes == 0
str = transform(Rubinius::CType::Lowered)
return nil if str == self
replace(str)
return self
end
def each_char
return to_enum(:each_char) { size } unless block_given?
bytes = 0
while bytes < @num_bytes
char = find_character(bytes)
yield char
bytes += char.num_bytes
end
self
end
def each_byte
return to_enum(:each_byte) { bytesize } unless block_given?
i = 0
while i < @num_bytes do
yield @data.get_byte(i)
i += 1
end
self
end
def empty?
@num_bytes == 0
end
def end_with?(*suffixes)
suffixes.each do |suffix|
suffix = Rubinius::Type.check_convert_type suffix, String, :to_str
next unless suffix
return true if self[-suffix.length, suffix.length] == suffix
end
false
end
def eql?(other)
Rubinius.primitive :string_equal
return false unless other.kind_of?(String) && other.bytesize == @num_bytes
return false unless Encoding.compatible?(self, other)
return @data.compare_bytes(other.__data__, @num_bytes, other.bytesize) == 0
end
# This method is specifically part of 1.9 but we enable it in 1.8 also
# because we need it internally.
def getbyte(index)
index = Rubinius::Type.coerce_to index, Fixnum, :to_int
index += bytesize if index < 0
return if index < 0 or index >= bytesize
@data[index]
end
def include?(needle)
!!find_string(StringValue(needle), 0)
end
ControlCharacters = [10, 9, 7, 11, 12, 13, 27, 8]
ControlPrintValue = ["\\n", "\\t", "\\a", "\\v", "\\f", "\\r", "\\e", "\\b"]
def lstrip
str = dup
str.lstrip! || str
end
def oct
to_inum(-8, false)
end
# Treats leading characters from <i>self</i> as a string of hexadecimal digits
# (with an optional sign and an optional <code>0x</code>) and returns the
# corresponding number. Zero is returned on error.
#
# "0x0a".hex #=> 10
# "-1234".hex #=> -4660
# "0".hex #=> 0
# "wombat".hex #=> 0
def hex
to_inum(16, false)
end
def reverse
dup.reverse!
end
def partition(pattern=nil)
return super() if pattern == nil && block_given?
if pattern.kind_of? Regexp
if m = pattern.match(self)
Regexp.last_match = m
return [m.pre_match, m.to_s, m.post_match]
end
else
pattern = StringValue(pattern)
if i = index(pattern)
post_start = i + pattern.length
post_len = size - post_start
return [substring(0, i),
pattern.dup,
substring(post_start, post_len)]
end
end
# Nothing worked out, this is the default.
return [self, "", ""]
end
def rpartition(pattern)
if pattern.kind_of? Regexp
if m = pattern.search_region(self, 0, size, false)
Regexp.last_match = m
[m.pre_match, m[0], m.post_match]
end
else
pattern = StringValue(pattern)
if i = rindex(pattern)
post_start = i + pattern.length
post_len = size - post_start
return [substring(0, i),
pattern.dup,
substring(post_start, post_len)]
end
# Nothing worked out, this is the default.
return ["", "", self]
end
end
def rstrip
str = dup
str.rstrip! || str
end
def scan(pattern)
taint = tainted? || pattern.tainted?
pattern = Rubinius::Type.coerce_to_regexp(pattern, true)
index = 0
last_match = nil
if block_given?
ret = self
else
ret = []
end
while match = pattern.match_from(self, index)
fin = match.full.at(1)
if match.collapsing?
if char = find_character(fin)
index = fin + char.bytesize
else
index = fin + 1
end
else
index = fin
end
last_match = match
val = (match.length == 1 ? match[0] : match.captures)
val.taint if taint
if block_given?
Regexp.last_match = match
yield(val)
else
ret << val
end
end
Regexp.last_match = last_match
return ret
end
# This method is specifically part of 1.9 but we enable it in 1.8 also
# because we need it internally.
def setbyte(index, byte)
self.modify!
index = Rubinius::Type.coerce_to index, Fixnum, :to_int
byte = Rubinius::Type.coerce_to byte, Fixnum, :to_int
index += bytesize if index < 0
if index < 0 or index >= bytesize
raise IndexError, "byte index #{index} is outside bounds of String"
end
@ascii_only = @valid_encoding = nil
@data[index] = byte
end
def split(pattern=nil, limit=undefined)
Rubinius::Splitter.split(self, pattern, limit)
end
def squeeze(*strings)
str = dup
str.squeeze!(*strings) || str
end
def squeeze!(*strings)
return if @num_bytes == 0
table = count_table(*strings).__data__
self.modify!
i = 1
j = 0
last = @data[0]
while i < @num_bytes
c = @data[i]
unless c == last and table[c] == 1
@data[j+=1] = last = c
end
i += 1
end
if (j += 1) < @num_bytes
self.num_bytes = j
self
else
nil
end
end
def start_with?(*prefixes)
prefixes.each do |prefix|
prefix = Rubinius::Type.check_convert_type prefix, String, :to_str
next unless prefix
return true if self[0, prefix.length] == prefix
end
false
end
def strip
str = dup
str.strip! || str
end
def strip!
left = lstrip!
right = rstrip!
left.nil? && right.nil? ? nil : self
end
def succ
dup.succ!
end
def sum(bits=16)
bits = Rubinius::Type.coerce_to bits, Fixnum, :to_int
i = -1
sum = 0
sum += @data[i] while (i += 1) < @num_bytes
if bits > 0
sum & ((1 << bits) - 1)
else
sum
end
end
def swapcase
str = dup
str.swapcase! || str
end
def swapcase!
self.modify!
return if @num_bytes == 0
modified = false
ctype = Rubinius::CType
i = 0
while i < @num_bytes
c = @data[i]
if ctype.islower(c)
@data[i] = ctype.toupper!(c)
modified = true
elsif ctype.isupper(c)
@data[i] = ctype.tolower!(c)
modified = true
end
i += 1
end
modified ? self : nil
end
alias_method :intern, :to_sym
def to_i(base=10)
base = Rubinius::Type.coerce_to base, Integer, :to_int
if base < 0 || base == 1 || base > 36
raise ArgumentError, "illegal radix #{base}"
end
to_inum(base, false)
end
def to_s
instance_of?(String) ? self : "".replace(self)
end
alias_method :to_str, :to_s
def tr(source, replacement)
str = dup
str.tr!(source, replacement) || str
end
def tr!(source, replacement)
tr_trans(source, replacement, false)
end
def tr_s(source, replacement)
str = dup
str.tr_s!(source, replacement) || str
end
def tr_s!(source, replacement)
tr_trans(source, replacement, true)
end
def unpack(directives)
Rubinius.primitive :string_unpack
unless directives.kind_of? String
return unpack(StringValue(directives))
end
raise ArgumentError, "invalid directives string: #{directives}"
end
def upcase
str = dup
str.upcase! || str
end
def upcase!
return if @num_bytes == 0
self.modify!
modified = false
ctype = Rubinius::CType
i = 0
while i < @num_bytes
c = @data[i]
if ctype.islower(c)
@data[i] = ctype.toupper!(c)
modified = true
end
i += 1
end
modified ? self : nil
end
def to_sub_replacement(result, match)
index = 0
while index < @num_bytes
current = index
while current < @num_bytes && @data[current] != 92 # ?\\
current += 1
end
result.append(byteslice(index, current - index))
break if current == @num_bytes
# found backslash escape, looking next
if current == @num_bytes - 1
result.append("\\") # backslash at end of string
break
end
index = current + 1
cap = @data[index]
additional = case cap
when 38 # ?&
match[0]
when 96 # ?`
match.pre_match
when 39 # ?'
match.post_match
when 43 # ?+
match.captures.compact[-1].to_s
when 48..57 # ?0..?9
match[cap - 48].to_s
when 92 # ?\\ escaped backslash
'\\'
when 107 # \k named capture
if @data[index + 1] == 60
name = ""
i = index + 2
while i < @data.size && @data[i] != 62
name << @data[i]
i += 1
end
if i >= @data.size
'\\'.append(cap.chr)
index += 1
next
end
index = i
name.force_encoding result.encoding
match[name]
else
'\\'.append(cap.chr)
end
else # unknown escape
'\\'.append(cap.chr)
end
result.append(additional)
index += 1
end
end
def to_inum(base, check)
Rubinius.primitive :string_to_inum
raise ArgumentError, "invalid value for Integer"
end
def apply_and!(other)
Rubinius.primitive :string_apply_and
raise PrimitiveFailure, "String#apply_and! primitive failed"
end
def compare_substring(other, start, size)
Rubinius.primitive :string_compare_substring
if start > @num_bytes || start + @num_bytes < 0
raise IndexError, "index #{start} out of string"
end
raise PrimitiveFailure, "String#compare_substring primitive failed"
end
def count_table(*strings)
table = String.pattern 256, 1
i = 0
size = strings.size
while i < size
str = StringValue(strings[i]).dup
if str.bytesize > 1 && str.getbyte(0) == 94 # ?^
pos = 0
neg = 1
str.slice!(0)
else
pos = 1
neg = 0
end
set = String.pattern 256, neg
set_data = set.__data__
str.tr_expand! nil, true
str_data = str.__data__
j = -1
chars = str.bytesize
set_data[str_data[j]] = pos while (j += 1) < chars
table.apply_and! set
i += 1
end
table
end
def tr_expand!(limit, invalid_as_empty)
Rubinius.primitive :string_tr_expand
raise PrimitiveFailure, "String#tr_expand primitive failed"
end
# Unshares shared strings.
def modify!
Rubinius.check_frozen
if @shared
@data = @data.dup
@shared = nil
end
@ascii_only = @valid_encoding = nil
@hash_value = nil # reset the hash value
end
def subpattern(pattern, capture)
match = pattern.match(self)
return nil unless match
if index = Rubinius::Type.check_convert_type(capture, Fixnum, :to_int)
return nil if index >= match.size || -index >= match.size
capture = index
end
str = match[capture]
Rubinius::Type.infect str, pattern
[match, str]
end
private :subpattern
def prefix?(other)
size = other.size
return false if size > @num_bytes
other.compare_substring(self, 0, size) == 0
end
def suffix?(other)
size = other.size
return false if size > @num_bytes
other.compare_substring(self, -size, size) == 0
end
def shorten!(size)
self.modify!
return if @num_bytes == 0
self.num_bytes -= size
end
def shared!