forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubyString.java
More file actions
7705 lines (6672 loc) · 284 KB
/
Copy pathRubyString.java
File metadata and controls
7705 lines (6672 loc) · 284 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
/*
**** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
* Copyright (C) 2005 Tim Azzopardi <tim@tigerfive.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2006 Ola Bini <ola@ologix.com>
* Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import static org.jruby.CompatVersion.RUBY1_8;
import static org.jruby.CompatVersion.RUBY1_9;
import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.anno.FrameField.BACKREF;
import static org.jruby.javasupport.util.RuntimeHelpers.invokedynamic;
import static org.jruby.runtime.invokedynamic.MethodNames.OP_CMP;
import static org.jruby.runtime.invokedynamic.MethodNames.OP_EQUAL;
import static org.jruby.runtime.Visibility.PRIVATE;
import static org.jruby.util.StringSupport.CR_7BIT;
import static org.jruby.util.StringSupport.CR_BROKEN;
import static org.jruby.util.StringSupport.CR_MASK;
import static org.jruby.util.StringSupport.CR_UNKNOWN;
import static org.jruby.util.StringSupport.CR_VALID;
import static org.jruby.util.StringSupport.codeLength;
import static org.jruby.util.StringSupport.codePoint;
import static org.jruby.util.StringSupport.codeRangeScan;
import static org.jruby.util.StringSupport.searchNonAscii;
import static org.jruby.util.StringSupport.strLengthWithCodeRange;
import static org.jruby.util.StringSupport.toLower;
import static org.jruby.util.StringSupport.toUpper;
import static org.jruby.util.StringSupport.unpackArg;
import static org.jruby.util.StringSupport.unpackResult;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Locale;
import org.jcodings.Encoding;
import org.jcodings.EncodingDB;
import org.jcodings.ascii.AsciiTables;
import org.jcodings.constants.CharacterType;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jcodings.util.CaseInsensitiveBytesHash;
import org.jcodings.util.IntHash;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.Regex;
import org.joni.Region;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.parser.LocalStaticScope;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.encoding.EncodingCapable;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;
import org.jruby.util.CharsetTranscoder;
import org.jruby.util.ConvertBytes;
import org.jruby.util.MurmurHash;
import org.jruby.util.Numeric;
import org.jruby.util.Pack;
import org.jruby.util.PerlHash;
import org.jruby.util.RegexpOptions;
import org.jruby.util.SipHashInline;
import org.jruby.util.Sprintf;
import org.jruby.util.StringSupport;
import org.jruby.util.TypeConverter;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import org.jruby.util.string.JavaCrypt;
/**
* Implementation of Ruby String class
*
* Concurrency: no synchronization is required among readers, but
* all users must synchronize externally with writers.
*
*/
@JRubyClass(name="String", include={"Enumerable", "Comparable"})
public class RubyString extends RubyObject implements EncodingCapable {
private static final Logger LOG = LoggerFactory.getLogger("RubyString");
private static final ASCIIEncoding ASCII = ASCIIEncoding.INSTANCE;
private static final UTF8Encoding UTF8 = UTF8Encoding.INSTANCE;
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
// string doesn't share any resources
private static final int SHARE_LEVEL_NONE = 0;
// string has it's own ByteList, but it's pointing to a shared buffer (byte[])
private static final int SHARE_LEVEL_BUFFER = 1;
// string doesn't have it's own ByteList (values)
private static final int SHARE_LEVEL_BYTELIST = 2;
// FIXME: Using a cached StaticScope for to_r and to_c that call backref-sensitive methods
private static final StaticScope INTERNAL_STATIC_SCOPE = new LocalStaticScope(null);
private volatile int shareLevel = SHARE_LEVEL_NONE;
private ByteList value;
public static RubyClass createStringClass(Ruby runtime) {
RubyClass stringClass = runtime.defineClass("String", runtime.getObject(), STRING_ALLOCATOR);
runtime.setString(stringClass);
stringClass.index = ClassIndex.STRING;
stringClass.setReifiedClass(RubyString.class);
stringClass.kindOf = new RubyModule.KindOf() {
@Override
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyString;
}
};
stringClass.includeModule(runtime.getComparable());
if (!runtime.is1_9()) stringClass.includeModule(runtime.getEnumerable());
stringClass.defineAnnotatedMethods(RubyString.class);
return stringClass;
}
private static ObjectAllocator STRING_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return RubyString.newAllocatedString(runtime, klass);
}
};
public Encoding getEncoding() {
return value.getEncoding();
}
public void setEncoding(Encoding encoding) {
value.setEncoding(encoding);
}
public void associateEncoding(Encoding enc) {
if (value.getEncoding() != enc) {
if (!isCodeRangeAsciiOnly() || !enc.isAsciiCompatible()) clearCodeRange();
value.setEncoding(enc);
}
}
public final void setEncodingAndCodeRange(Encoding enc, int cr) {
value.setEncoding(enc);
setCodeRange(cr);
}
public final Encoding toEncoding(Ruby runtime) {
return runtime.getEncodingService().findEncoding(this);
}
public final int getCodeRange() {
return flags & CR_MASK;
}
public final void setCodeRange(int codeRange) {
clearCodeRange();
flags |= codeRange & CR_MASK;
}
public final void clearCodeRange() {
flags &= ~CR_MASK;
}
private void keepCodeRange() {
if (getCodeRange() == CR_BROKEN) clearCodeRange();
}
// ENC_CODERANGE_ASCIIONLY
public final boolean isCodeRangeAsciiOnly() {
return getCodeRange() == CR_7BIT;
}
// rb_enc_str_asciionly_p
public final boolean isAsciiOnly() {
return value.getEncoding().isAsciiCompatible() && scanForCodeRange() == CR_7BIT;
}
public final boolean isCodeRangeValid() {
return (flags & CR_VALID) != 0;
}
public final boolean isCodeRangeBroken() {
return (flags & CR_BROKEN) != 0;
}
static int codeRangeAnd(int cr1, int cr2) {
if (cr1 == CR_7BIT) return cr2;
if (cr1 == CR_VALID) return cr2 == CR_7BIT ? CR_VALID : cr2;
return CR_UNKNOWN;
}
private void copyCodeRangeForSubstr(RubyString from, Encoding enc) {
int fromCr = from.getCodeRange();
if (fromCr == CR_7BIT) {
setCodeRange(fromCr);
} else if (fromCr == CR_VALID) {
if (!enc.isAsciiCompatible() || searchNonAscii(value) != -1) {
setCodeRange(CR_VALID);
} else {
setCodeRange(CR_7BIT);
}
} else{
if (value.getRealSize() == 0) {
setCodeRange(!enc.isAsciiCompatible() ? CR_VALID : CR_7BIT);
}
}
}
private void copyCodeRange(RubyString from) {
value.setEncoding(from.value.getEncoding());
setCodeRange(from.getCodeRange());
}
// rb_enc_str_coderange
final int scanForCodeRange() {
int cr = getCodeRange();
if (cr == CR_UNKNOWN) {
cr = codeRangeScan(value.getEncoding(), value);
setCodeRange(cr);
}
return cr;
}
final boolean singleByteOptimizable() {
return getCodeRange() == CR_7BIT || value.getEncoding().isSingleByte();
}
final boolean singleByteOptimizable(Encoding enc) {
return getCodeRange() == CR_7BIT || enc.isSingleByte();
}
private Encoding isCompatibleWith(RubyString other) {
Encoding enc1 = value.getEncoding();
Encoding enc2 = other.value.getEncoding();
if (enc1 == enc2) return enc1;
if (other.value.getRealSize() == 0) return enc1;
if (value.getRealSize() == 0) return enc2;
if (!enc1.isAsciiCompatible() || !enc2.isAsciiCompatible()) return null;
return RubyEncoding.areCompatible(enc1, scanForCodeRange(), enc2, other.scanForCodeRange());
}
final Encoding isCompatibleWith(EncodingCapable other) {
if (other instanceof RubyString) return checkEncoding((RubyString)other);
Encoding enc1 = value.getEncoding();
Encoding enc2 = other.getEncoding();
if (enc1 == enc2) return enc1;
if (value.getRealSize() == 0) return enc2;
if (!enc1.isAsciiCompatible() || !enc2.isAsciiCompatible()) return null;
if (enc2 instanceof USASCIIEncoding) return enc1;
if (scanForCodeRange() == CR_7BIT) return enc2;
return null;
}
public final Encoding checkEncoding(RubyString other) {
Encoding enc = isCompatibleWith(other);
if (enc == null) throw getRuntime().newEncodingCompatibilityError("incompatible character encodings: " +
value.getEncoding() + " and " + other.value.getEncoding());
return enc;
}
final Encoding checkEncoding(EncodingCapable other) {
Encoding enc = isCompatibleWith(other);
if (enc == null) throw getRuntime().newEncodingCompatibilityError("incompatible character encodings: " +
value.getEncoding() + " and " + other.getEncoding());
return enc;
}
private Encoding checkDummyEncoding() {
Encoding enc = value.getEncoding();
if (enc.isDummy()) throw getRuntime().newEncodingCompatibilityError(
"incompatible encoding with this operation: " + enc);
return enc;
}
private boolean isComparableWith(RubyString other) {
ByteList otherValue = other.value;
if (value.getEncoding() == otherValue.getEncoding() ||
value.getRealSize() == 0 || otherValue.getRealSize() == 0) return true;
return isComparableViaCodeRangeWith(other);
}
private boolean isComparableViaCodeRangeWith(RubyString other) {
int cr1 = scanForCodeRange();
int cr2 = other.scanForCodeRange();
if (cr1 == CR_7BIT && (cr2 == CR_7BIT || other.value.getEncoding().isAsciiCompatible())) return true;
if (cr2 == CR_7BIT && value.getEncoding().isAsciiCompatible()) return true;
return false;
}
private int strLength(Encoding enc) {
if (singleByteOptimizable(enc)) return value.getRealSize();
return strLength(value, enc);
}
public final int strLength() {
if (singleByteOptimizable()) return value.getRealSize();
return strLength(value);
}
private int strLength(ByteList bytes) {
return strLength(bytes, bytes.getEncoding());
}
private int strLength(ByteList bytes, Encoding enc) {
if (isCodeRangeValid() && enc instanceof UTF8Encoding) return StringSupport.utf8Length(value);
long lencr = strLengthWithCodeRange(bytes, enc);
int cr = unpackArg(lencr);
if (cr != 0) setCodeRange(cr);
return unpackResult(lencr);
}
final int subLength(int pos) {
if (singleByteOptimizable() || pos < 0) return pos;
return StringSupport.strLength(value.getEncoding(), value.getUnsafeBytes(), value.getBegin(), value.getBegin() + pos);
}
/** short circuit for String key comparison
*
*/
@Override
public final boolean eql(IRubyObject other) {
Ruby runtime = getRuntime();
if (getMetaClass() != runtime.getString() || getMetaClass() != other.getMetaClass()) return super.eql(other);
return runtime.is1_9() ? eql19(runtime, other) : eql18(runtime, other);
}
private boolean eql18(Ruby runtime, IRubyObject other) {
return value.equal(((RubyString)other).value);
}
// rb_str_hash_cmp
private boolean eql19(Ruby runtime, IRubyObject other) {
RubyString otherString = (RubyString)other;
return isComparableWith(otherString) && value.equal(((RubyString)other).value);
}
public RubyString(Ruby runtime, RubyClass rubyClass) {
this(runtime, rubyClass, EMPTY_BYTE_ARRAY);
}
public RubyString(Ruby runtime, RubyClass rubyClass, CharSequence value) {
super(runtime, rubyClass);
assert value != null;
Encoding defaultEncoding = runtime.getEncodingService().getLocaleEncoding();
if (defaultEncoding == null) defaultEncoding = UTF8;
this.value = encodeBytelist(value, defaultEncoding);
}
public RubyString(Ruby runtime, RubyClass rubyClass, CharSequence value, Encoding defaultEncoding) {
super(runtime, rubyClass);
assert value != null;
this.value = encodeBytelist(value, defaultEncoding);
}
public RubyString(Ruby runtime, RubyClass rubyClass, byte[] value) {
super(runtime, rubyClass);
assert value != null;
this.value = new ByteList(value);
}
public RubyString(Ruby runtime, RubyClass rubyClass, ByteList value) {
super(runtime, rubyClass);
assert value != null;
this.value = value;
}
public RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, boolean objectSpace) {
super(runtime, rubyClass, objectSpace);
assert value != null;
this.value = value;
}
public RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, Encoding encoding, boolean objectSpace) {
this(runtime, rubyClass, value, objectSpace);
value.setEncoding(encoding);
}
protected RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, Encoding enc, int cr) {
this(runtime, rubyClass, value);
value.setEncoding(enc);
flags |= cr;
}
protected RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, Encoding enc) {
this(runtime, rubyClass, value);
value.setEncoding(enc);
}
protected RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, int cr) {
this(runtime, rubyClass, value);
flags |= cr;
}
// Deprecated String construction routines
/** Create a new String which uses the same Ruby runtime and the same
* class like this String.
*
* This method should be used to satisfy RCR #38.
* @deprecated
*/
@Deprecated
public RubyString newString(CharSequence s) {
return new RubyString(getRuntime(), getType(), s);
}
/** Create a new String which uses the same Ruby runtime and the same
* class like this String.
*
* This method should be used to satisfy RCR #38.
* @deprecated
*/
@Deprecated
public RubyString newString(ByteList s) {
return new RubyString(getRuntime(), getMetaClass(), s);
}
@Deprecated
public static RubyString newString(Ruby runtime, RubyClass clazz, CharSequence str) {
return new RubyString(runtime, clazz, str);
}
public static RubyString newStringLight(Ruby runtime, ByteList bytes) {
return new RubyString(runtime, runtime.getString(), bytes, false);
}
public static RubyString newStringLight(Ruby runtime, int size) {
return new RubyString(runtime, runtime.getString(), new ByteList(size), false);
}
public static RubyString newStringLight(Ruby runtime, int size, Encoding encoding) {
return new RubyString(runtime, runtime.getString(), new ByteList(size), encoding, false);
}
public static RubyString newString(Ruby runtime, CharSequence str) {
return new RubyString(runtime, runtime.getString(), str);
}
public static RubyString newString(Ruby runtime, String str) {
return new RubyString(runtime, runtime.getString(), str);
}
public static RubyString newUSASCIIString(Ruby runtime, String str) {
return new RubyString(runtime, runtime.getString(), str, USASCIIEncoding.INSTANCE);
}
public static RubyString newString(Ruby runtime, byte[] bytes) {
return new RubyString(runtime, runtime.getString(), bytes);
}
public static RubyString newString(Ruby runtime, byte[] bytes, int start, int length) {
byte[] copy = new byte[length];
System.arraycopy(bytes, start, copy, 0, length);
return new RubyString(runtime, runtime.getString(), new ByteList(copy, false));
}
public static RubyString newString(Ruby runtime, ByteList bytes) {
return new RubyString(runtime, runtime.getString(), bytes);
}
public static RubyString newString(Ruby runtime, ByteList bytes, Encoding encoding) {
return new RubyString(runtime, runtime.getString(), bytes, encoding);
}
public static RubyString newUnicodeString(Ruby runtime, String str) {
ByteList byteList = new ByteList(RubyEncoding.encodeUTF8(str), UTF8Encoding.INSTANCE, false);
return new RubyString(runtime, runtime.getString(), byteList);
}
public static RubyString newUnicodeString(Ruby runtime, CharSequence str) {
ByteList byteList = new ByteList(RubyEncoding.encodeUTF8(str), UTF8Encoding.INSTANCE, false);
return new RubyString(runtime, runtime.getString(), byteList);
}
/**
* Return a new Ruby String encoded as the default internal encoding given a Java String that
* has come from an external source. If there is no default internal encoding set, the Ruby
* String will be encoded using Java's default external encoding. If an internal encoding is
* set, that encoding will be used for the Ruby String.
*
* @param runtime
* @param str
* @return
*/
public static RubyString newInternalFromJavaExternal(Ruby runtime, String str) {
// Ruby internal
Encoding internal = runtime.getDefaultInternalEncoding();
Charset rubyInt = null;
if (internal != null && internal.getCharset() != null) rubyInt = internal.getCharset();
// Java external, used if no internal
Charset javaExt = Charset.defaultCharset();
Encoding javaExtEncoding = runtime.getEncodingService().getJavaDefault();
if (rubyInt == null) {
return RubyString.newString(
runtime,
new ByteList(str.getBytes(), javaExtEncoding));
} else {
return RubyString.newString(
runtime,
new ByteList(RubyEncoding.encode(str, rubyInt), internal));
}
}
// String construction routines by NOT byte[] buffer and making the target String shared
public static RubyString newStringShared(Ruby runtime, RubyString orig) {
orig.shareLevel = SHARE_LEVEL_BYTELIST;
RubyString str = new RubyString(runtime, runtime.getString(), orig.value);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}
public static RubyString newStringShared(Ruby runtime, ByteList bytes) {
return newStringShared(runtime, runtime.getString(), bytes);
}
public static RubyString newStringShared(Ruby runtime, ByteList bytes, Encoding encoding) {
return newStringShared(runtime, runtime.getString(), bytes, encoding);
}
public static RubyString newStringShared(Ruby runtime, ByteList bytes, int codeRange) {
RubyString str = new RubyString(runtime, runtime.getString(), bytes, codeRange);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}
public static RubyString newStringShared(Ruby runtime, RubyClass clazz, ByteList bytes) {
RubyString str = new RubyString(runtime, clazz, bytes);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}
public static RubyString newStringShared(Ruby runtime, RubyClass clazz, ByteList bytes, Encoding encoding) {
RubyString str = new RubyString(runtime, clazz, bytes, encoding);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}
public static RubyString newStringShared(Ruby runtime, byte[] bytes) {
return newStringShared(runtime, new ByteList(bytes, false));
}
public static RubyString newStringShared(Ruby runtime, byte[] bytes, int start, int length) {
return newStringShared(runtime, new ByteList(bytes, start, length, false));
}
public static RubyString newEmptyString(Ruby runtime) {
return newEmptyString(runtime, runtime.getString());
}
private static final ByteList EMPTY_ASCII8BIT_BYTELIST = new ByteList(new byte[0], ASCIIEncoding.INSTANCE);
private static final ByteList EMPTY_USASCII_BYTELIST = new ByteList(new byte[0], USASCIIEncoding.INSTANCE);
public static RubyString newAllocatedString(Ruby runtime, RubyClass metaClass) {
RubyString empty = new RubyString(runtime, metaClass, EMPTY_ASCII8BIT_BYTELIST);
empty.shareLevel = SHARE_LEVEL_BYTELIST;
return empty;
}
public static RubyString newEmptyString(Ruby runtime, RubyClass metaClass) {
RubyString empty = new RubyString(runtime, metaClass, runtime.is1_9() ? EMPTY_USASCII_BYTELIST : EMPTY_ASCII8BIT_BYTELIST);
empty.shareLevel = SHARE_LEVEL_BYTELIST;
return empty;
}
// String construction routines by NOT byte[] buffer and NOT making the target String shared
public static RubyString newStringNoCopy(Ruby runtime, ByteList bytes) {
return newStringNoCopy(runtime, runtime.getString(), bytes);
}
public static RubyString newStringNoCopy(Ruby runtime, RubyClass clazz, ByteList bytes) {
return new RubyString(runtime, clazz, bytes);
}
public static RubyString newStringNoCopy(Ruby runtime, byte[] bytes, int start, int length) {
return newStringNoCopy(runtime, new ByteList(bytes, start, length, false));
}
public static RubyString newStringNoCopy(Ruby runtime, byte[] bytes) {
return newStringNoCopy(runtime, new ByteList(bytes, false));
}
/** Encoding aware String construction routines for 1.9
*
*/
private static final class EmptyByteListHolder {
final ByteList bytes;
final int cr;
EmptyByteListHolder(Encoding enc) {
this.bytes = new ByteList(ByteList.NULL_ARRAY, enc);
this.cr = bytes.getEncoding().isAsciiCompatible() ? CR_7BIT : CR_VALID;
}
}
private static EmptyByteListHolder EMPTY_BYTELISTS[] = new EmptyByteListHolder[4];
static EmptyByteListHolder getEmptyByteList(Encoding enc) {
if (enc == null) enc = ASCIIEncoding.INSTANCE;
int index = enc.getIndex();
EmptyByteListHolder bytes;
if (index < EMPTY_BYTELISTS.length && (bytes = EMPTY_BYTELISTS[index]) != null) {
return bytes;
}
return prepareEmptyByteList(enc);
}
private static EmptyByteListHolder prepareEmptyByteList(Encoding enc) {
if (enc == null) enc = ASCIIEncoding.INSTANCE;
int index = enc.getIndex();
if (index >= EMPTY_BYTELISTS.length) {
EmptyByteListHolder tmp[] = new EmptyByteListHolder[index + 4];
System.arraycopy(EMPTY_BYTELISTS,0, tmp, 0, EMPTY_BYTELISTS.length);
EMPTY_BYTELISTS = tmp;
}
return EMPTY_BYTELISTS[index] = new EmptyByteListHolder(enc);
}
public static RubyString newEmptyString(Ruby runtime, RubyClass metaClass, Encoding enc) {
EmptyByteListHolder holder = getEmptyByteList(enc);
RubyString empty = new RubyString(runtime, metaClass, holder.bytes, holder.cr);
empty.shareLevel = SHARE_LEVEL_BYTELIST;
return empty;
}
public static RubyString newEmptyString(Ruby runtime, Encoding enc) {
return newEmptyString(runtime, runtime.getString(), enc);
}
public static RubyString newStringNoCopy(Ruby runtime, RubyClass clazz, ByteList bytes, Encoding enc, int cr) {
return new RubyString(runtime, clazz, bytes, enc, cr);
}
public static RubyString newStringNoCopy(Ruby runtime, ByteList bytes, Encoding enc, int cr) {
return newStringNoCopy(runtime, runtime.getString(), bytes, enc, cr);
}
public static RubyString newUsAsciiStringNoCopy(Ruby runtime, ByteList bytes) {
return newStringNoCopy(runtime, bytes, USASCIIEncoding.INSTANCE, CR_7BIT);
}
public static RubyString newUsAsciiStringShared(Ruby runtime, ByteList bytes) {
RubyString str = newStringNoCopy(runtime, bytes, USASCIIEncoding.INSTANCE, CR_7BIT);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}
public static RubyString newUsAsciiStringShared(Ruby runtime, byte[] bytes, int start, int length) {
byte[] copy = new byte[length];
System.arraycopy(bytes, start, copy, 0, length);
return newUsAsciiStringShared(runtime, new ByteList(copy, false));
}
@Override
public int getNativeTypeIndex() {
return ClassIndex.STRING;
}
@Override
public Class getJavaClass() {
return String.class;
}
@Override
public RubyString convertToString() {
return this;
}
@Override
public String toString() {
return decodeString();
}
/**
* Convert this Ruby string to a Java String. This version is encoding-aware.
*
* @return A decoded Java String, based on this Ruby string's encoding.
*/
public String decodeString() {
Ruby runtime = getRuntime();
// Note: we always choose UTF-8 for outbound strings in 1.8 mode. This is clearly undesirable
// but we do not mark any incoming Strings from JI with their real encoding so we just pick utf-8.
if (runtime.is1_9()) {
Encoding encoding = getEncoding();
if (encoding == UTF8) {
// faster UTF8 decoding
return RubyEncoding.decodeUTF8(value.getUnsafeBytes(), value.begin(), value.length());
}
Charset charset = runtime.getEncodingService().charsetForEncoding(encoding);
// charset is not defined for this encoding in jcodings db. Try letting Java resolve this.
if (charset == null) {
try {
return new String(value.getUnsafeBytes(), value.begin(), value.length(), encoding.toString());
} catch (UnsupportedEncodingException uee) {
return value.toString();
}
}
return RubyEncoding.decode(value.getUnsafeBytes(), value.begin(), value.length(), charset);
} else {
// fast UTF8 decoding
return RubyEncoding.decodeUTF8(value.getUnsafeBytes(), value.begin(), value.length());
}
}
/**
* Overridden dup for fast-path logic.
*
* @return A new RubyString sharing the original backing store.
*/
@Override
public IRubyObject dup() {
RubyClass mc = metaClass.getRealClass();
if (mc.index != ClassIndex.STRING) return super.dup();
return strDup(mc.getClassRuntime(), mc.getRealClass());
}
/** rb_str_dup
*
*/
@Deprecated
public final RubyString strDup() {
return strDup(getRuntime(), getMetaClass());
}
public final RubyString strDup(Ruby runtime) {
return strDup(runtime, getMetaClass());
}
@Deprecated
final RubyString strDup(RubyClass clazz) {
return strDup(getRuntime(), getMetaClass());
}
final RubyString strDup(Ruby runtime, RubyClass clazz) {
shareLevel = SHARE_LEVEL_BYTELIST;
RubyString dup = new RubyString(runtime, clazz, value);
dup.shareLevel = SHARE_LEVEL_BYTELIST;
dup.flags |= flags & (CR_MASK | TAINTED_F | UNTRUSTED_F);
return dup;
}
/* rb_str_subseq */
public final RubyString makeSharedString(Ruby runtime, int index, int len) {
return makeShared(runtime, runtime.getString(), index, len);
}
public RubyString makeSharedString19(Ruby runtime, int index, int len) {
return makeShared19(runtime, runtime.getString(), value, index, len);
}
public final RubyString makeShared(Ruby runtime, int index, int len) {
return makeShared(runtime, getType(), index, len);
}
public final RubyString makeShared(Ruby runtime, RubyClass meta, int index, int len) {
final RubyString shared;
if (len == 0) {
shared = newEmptyString(runtime, meta);
} else if (len == 1) {
shared = newStringShared(runtime, meta,
RubyInteger.SINGLE_CHAR_BYTELISTS[value.getUnsafeBytes()[value.getBegin() + index] & 0xff]);
} else {
if (shareLevel == SHARE_LEVEL_NONE) shareLevel = SHARE_LEVEL_BUFFER;
shared = new RubyString(runtime, meta, value.makeShared(index, len));
shared.shareLevel = SHARE_LEVEL_BUFFER;
}
shared.infectBy(this);
return shared;
}
public final RubyString makeShared19(Ruby runtime, int index, int len) {
return makeShared19(runtime, value, index, len);
}
private RubyString makeShared19(Ruby runtime, ByteList value, int index, int len) {
return makeShared19(runtime, getType(), value, index, len);
}
private RubyString makeShared19(Ruby runtime, RubyClass meta, ByteList value, int index, int len) {
final RubyString shared;
Encoding enc = value.getEncoding();
if (len == 0) {
shared = newEmptyString(runtime, meta, enc);
} else {
if (shareLevel == SHARE_LEVEL_NONE) shareLevel = SHARE_LEVEL_BUFFER;
shared = new RubyString(runtime, meta, value.makeShared(index, len));
shared.shareLevel = SHARE_LEVEL_BUFFER;
shared.copyCodeRangeForSubstr(this, enc); // no need to assign encoding, same bytelist shared
}
shared.infectBy(this);
return shared;
}
public final void setByteListShared() {
if (shareLevel != SHARE_LEVEL_BYTELIST) shareLevel = SHARE_LEVEL_BYTELIST;
}
/**
* Check that the string can be modified, raising error otherwise.
*
* If you plan to modify a string with shared backing store, this
* method is not sufficient; you will need to call modify() instead.
*/
public final void modifyCheck() {
frozenCheck();
}
private void modifyCheck(byte[] b, int len) {
if (value.getUnsafeBytes() != b || value.getRealSize() != len) throw getRuntime().newRuntimeError("string modified");
}
private void modifyCheck(byte[] b, int len, Encoding enc) {
if (value.getUnsafeBytes() != b || value.getRealSize() != len || value.getEncoding() != enc) throw getRuntime().newRuntimeError("string modified");
}
private void frozenCheck() {
frozenCheck(false);
}
private void frozenCheck(boolean runtimeError) {
if (isFrozen()) throw getRuntime().newFrozenError("string", runtimeError);
}
/** rb_str_modify
*
*/
public final void modify() {
modifyCheck();
if (shareLevel != SHARE_LEVEL_NONE) {
if (shareLevel == SHARE_LEVEL_BYTELIST) {
value = value.dup();
} else {
value.unshare();
}
shareLevel = SHARE_LEVEL_NONE;
}
value.invalidate();
}
public final void modify19() {
modify();
clearCodeRange();
}
private void modifyAndKeepCodeRange() {
modify();
keepCodeRange();
}
/** rb_str_modify (with length bytes ensured)
*
*/
public final void modify(int length) {
modifyCheck();
if (shareLevel != SHARE_LEVEL_NONE) {
if (shareLevel == SHARE_LEVEL_BYTELIST) {
value = value.dup(length);
} else {
value.unshare(length);
}
shareLevel = SHARE_LEVEL_NONE;
} else {
value.ensure(length);
}
value.invalidate();
}
public final void modify19(int length) {
modify(length);
clearCodeRange();
}
/** rb_str_resize
*/
public final void resize(int length) {
modify();
if (value.getRealSize() > length) {
value.setRealSize(length);
} else if (value.length() < length) {
value.length(length);
}
}
public final void view(ByteList bytes) {
modifyCheck();
value = bytes;
shareLevel = SHARE_LEVEL_NONE;
}
private void view(byte[]bytes) {
modifyCheck();
value = new ByteList(bytes);
shareLevel = SHARE_LEVEL_NONE;
value.invalidate();
}
private void view(int index, int len) {
modifyCheck();
if (shareLevel != SHARE_LEVEL_NONE) {
if (shareLevel == SHARE_LEVEL_BYTELIST) {
// if len == 0 then shared empty
value = value.makeShared(index, len);
shareLevel = SHARE_LEVEL_BUFFER;
} else {
value.view(index, len);
}
} else {
value.view(index, len);
// FIXME this below is temporary, but its much safer for COW (it prevents not shared Strings with begin != 0)
// this allows now e.g.: ByteList#set not to be begin aware
shareLevel = SHARE_LEVEL_BUFFER;
}
value.invalidate();
}
public static String bytesToString(byte[] bytes, int beg, int len) {
return new String(ByteList.plain(bytes, beg, len));
}
public static String byteListToString(ByteList bytes) {
return bytesToString(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
}
public static String bytesToString(byte[] bytes) {
return bytesToString(bytes, 0, bytes.length);
}
public static byte[] stringToBytes(String string) {
return ByteList.plain(string);
}
@Override
public RubyString asString() {
return this;