FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/src/org/jruby/RubyRegexp.java at parallel_boot · MSNexploder/jruby · GitHub
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
jruby
/
src
/
org
/
jruby
/
RubyRegexp.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
2339 lines (2047 loc) · 91.5 KB
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
RubyRegexp.java
Copy path
File metadata and controls
2339 lines (2047 loc) · 91.5 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
/***** 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) 2004-2005 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 David Corbin <dcorbin@users.sourceforge.net>
* Copyright (C) 2006 Nick Sieger <nicksieger@gmail.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@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
.
anno
.
FrameField
.
BACKREF
;
import
static
org
.
jruby
.
anno
.
FrameField
.
LASTLINE
;
import
java
.
lang
.
ref
.
SoftReference
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
import
org
.
jcodings
.
Encoding
;
import
org
.
jcodings
.
specific
.
ASCIIEncoding
;
import
org
.
jcodings
.
specific
.
USASCIIEncoding
;
import
org
.
jcodings
.
specific
.
UTF8Encoding
;
import
org
.
joni
.
Matcher
;
import
org
.
joni
.
NameEntry
;
import
org
.
joni
.
Option
;
import
org
.
joni
.
Regex
;
import
org
.
joni
.
Region
;
import
org
.
joni
.
Syntax
;
import
org
.
joni
.
exception
.
JOniException
;
import
org
.
jruby
.
anno
.
JRubyClass
;
import
org
.
jruby
.
anno
.
JRubyMethod
;
import
org
.
jruby
.
common
.
IRubyWarnings
.
ID
;
import
org
.
jruby
.
exceptions
.
RaiseException
;
import
org
.
jruby
.
parser
.
ReOptions
;
import
org
.
jruby
.
runtime
.
Arity
;
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
.
Visibility
;
import
org
.
jruby
.
runtime
.
builtin
.
IRubyObject
;
import
org
.
jruby
.
runtime
.
encoding
.
EncodingCapable
;
import
org
.
jruby
.
runtime
.
encoding
.
MarshalEncoding
;
import
org
.
jruby
.
runtime
.
marshal
.
MarshalStream
;
import
org
.
jruby
.
runtime
.
marshal
.
UnmarshalStream
;
import
org
.
jruby
.
util
.
ByteList
;
import
org
.
jruby
.
util
.
KCode
;
import
org
.
jruby
.
util
.
Pack
;
import
org
.
jruby
.
util
.
RegexpOptions
;
import
org
.
jruby
.
util
.
Sprintf
;
import
org
.
jruby
.
util
.
StringSupport
;
import
org
.
jruby
.
util
.
TypeConverter
;
@
JRubyClass
(
name
=
"Regexp"
)
public
class
RubyRegexp
extends
RubyObject
implements
ReOptions
,
EncodingCapable
,
MarshalEncoding
{
private
Regex
pattern
;
private
ByteList
str
=
ByteList
.
EMPTY_BYTELIST
;
private
RegexpOptions
options
;
public
static
final
int
ARG_ENCODING_FIXED
=
16
;
public
static
final
int
ARG_ENCODING_NONE
=
32
;
public
void
setLiteral
() {
options
.
setLiteral
(
true
);
}
public
void
clearLiteral
() {
options
.
setLiteral
(
false
);
}
public
boolean
isLiteral
() {
return
options
.
isLiteral
();
}
public
boolean
isKCodeDefault
() {
return
options
.
isKcodeDefault
();
}
public
void
setEncodingNone
() {
options
.
setEncodingNone
(
true
);
}
public
void
clearEncodingNone
() {
options
.
setEncodingNone
(
false
);
}
public
boolean
isEncodingNone
() {
return
options
.
isEncodingNone
();
}
public
KCode
getKCode
() {
return
options
.
getKCode
();
}
public
Encoding
getEncoding
() {
return
pattern
.
getEncoding
();
}
public
void
setEncoding
(
Encoding
encoding
) {
// FIXME: Which encoding should be changed here?
// FIXME: transcode?
}
public
boolean
shouldMarshalEncoding
() {
return
getEncoding
() !=
ASCIIEncoding
.
INSTANCE
;
}
public
Encoding
getMarshalEncoding
() {
return
getEncoding
();
}
private
static
final
class
RegexpCache
{
private
volatile
SoftReference
<
Map
<
ByteList
,
Regex
>>
cache
=
new
SoftReference
<
Map
<
ByteList
,
Regex
>>(
null
);
private
Map
<
ByteList
,
Regex
>
get
() {
Map
<
ByteList
,
Regex
>
patternCache
=
cache
.
get
();
if
(
patternCache
==
null
) {
patternCache
=
new
ConcurrentHashMap
<
ByteList
,
Regex
>(
5
);
cache
=
new
SoftReference
<
Map
<
ByteList
,
Regex
>>(
patternCache
);
}
return
patternCache
;
}
}
private
static
final
RegexpCache
patternCache
=
new
RegexpCache
();
private
static
final
RegexpCache
quotedPatternCache
=
new
RegexpCache
();
private
static
final
RegexpCache
preprocessedPatternCache
=
new
RegexpCache
();
private
static
Regex
makeRegexp
(
Ruby
runtime
,
ByteList
bytes
,
RegexpOptions
options
,
Encoding
enc
) {
try
{
int
p
=
bytes
.
getBegin
();
return
new
Regex
(
bytes
.
getUnsafeBytes
(),
p
,
p
+
bytes
.
getRealSize
(),
options
.
toJoniOptions
(),
enc
,
Syntax
.
DEFAULT
,
runtime
.
getWarnings
());
}
catch
(
Exception
e
) {
if
(
runtime
.
is1_9
()) {
raiseRegexpError19
(
runtime
,
bytes
,
enc
,
options
,
e
.
getMessage
());
}
else
{
raiseRegexpError
(
runtime
,
bytes
,
enc
,
options
,
e
.
getMessage
());
}
return
null
;
// not reached
}
}
static
Regex
getRegexpFromCache
(
Ruby
runtime
,
ByteList
bytes
,
Encoding
enc
,
RegexpOptions
options
) {
Map
<
ByteList
,
Regex
>
cache
=
patternCache
.
get
();
Regex
regex
=
cache
.
get
(
bytes
);
if
(
regex
!=
null
&&
regex
.
getEncoding
() ==
enc
&&
regex
.
getOptions
() ==
options
.
toJoniOptions
())
return
regex
;
regex
=
makeRegexp
(
runtime
,
bytes
,
options
,
enc
);
cache
.
put
(
bytes
,
regex
);
return
regex
;
}
static
Regex
getQuotedRegexpFromCache
(
Ruby
runtime
,
ByteList
bytes
,
Encoding
enc
,
RegexpOptions
options
) {
Map
<
ByteList
,
Regex
>
cache
=
quotedPatternCache
.
get
();
Regex
regex
=
cache
.
get
(
bytes
);
if
(
regex
!=
null
&&
regex
.
getEncoding
() ==
enc
&&
regex
.
getOptions
() ==
options
.
toJoniOptions
())
return
regex
;
regex
=
makeRegexp
(
runtime
,
quote
(
bytes
,
enc
),
options
,
enc
);
cache
.
put
(
bytes
,
regex
);
return
regex
;
}
static
Regex
getQuotedRegexpFromCache19
(
Ruby
runtime
,
ByteList
bytes
,
RegexpOptions
options
,
boolean
asciiOnly
) {
Map
<
ByteList
,
Regex
>
cache
=
quotedPatternCache
.
get
();
Regex
regex
=
cache
.
get
(
bytes
);
Encoding
enc
=
asciiOnly
?
USASCIIEncoding
.
INSTANCE
:
bytes
.
getEncoding
();
if
(
regex
!=
null
&&
regex
.
getEncoding
() ==
enc
&&
regex
.
getOptions
() ==
options
.
toJoniOptions
())
return
regex
;
ByteList
quoted
=
quote19
(
bytes
,
asciiOnly
);
regex
=
makeRegexp
(
runtime
,
quoted
,
options
,
quoted
.
getEncoding
());
regex
.
setUserObject
(
quoted
);
cache
.
put
(
bytes
,
regex
);
return
regex
;
}
private
static
Regex
getPreprocessedRegexpFromCache
(
Ruby
runtime
,
ByteList
bytes
,
Encoding
enc
,
RegexpOptions
options
,
ErrorMode
mode
) {
Map
<
ByteList
,
Regex
>
cache
=
preprocessedPatternCache
.
get
();
Regex
regex
=
cache
.
get
(
bytes
);
if
(
regex
!=
null
&&
regex
.
getEncoding
() ==
enc
&&
regex
.
getOptions
() ==
options
.
toJoniOptions
())
return
regex
;
ByteList
preprocessed
=
preprocess
(
runtime
,
bytes
,
enc
,
new
Encoding
[]{
null
},
ErrorMode
.
RAISE
);
regex
=
makeRegexp
(
runtime
,
preprocessed
,
options
,
enc
);
regex
.
setUserObject
(
preprocessed
);
cache
.
put
(
bytes
,
regex
);
return
regex
;
}
public
static
RubyClass
createRegexpClass
(
Ruby
runtime
) {
RubyClass
regexpClass
=
runtime
.
defineClass
(
"Regexp"
,
runtime
.
getObject
(),
REGEXP_ALLOCATOR
);
runtime
.
setRegexp
(
regexpClass
);
regexpClass
.
index
=
ClassIndex
.
REGEXP
;
regexpClass
.
setReifiedClass
(
RubyRegexp
.
class
);
regexpClass
.
kindOf
=
new
RubyModule
.
KindOf
() {
@
Override
public
boolean
isKindOf
(
IRubyObject
obj
,
RubyModule
type
) {
return
obj
instanceof
RubyRegexp
;
}
};
regexpClass
.
defineConstant
(
"IGNORECASE"
,
runtime
.
newFixnum
(
RE_OPTION_IGNORECASE
));
regexpClass
.
defineConstant
(
"EXTENDED"
,
runtime
.
newFixnum
(
RE_OPTION_EXTENDED
));
regexpClass
.
defineConstant
(
"MULTILINE"
,
runtime
.
newFixnum
(
RE_OPTION_MULTILINE
));
if
(
runtime
.
is1_9
()) {
regexpClass
.
defineConstant
(
"FIXEDENCODING"
,
runtime
.
newFixnum
(
ARG_ENCODING_FIXED
));
regexpClass
.
defineConstant
(
"NOENCODING"
,
runtime
.
newFixnum
(
ARG_ENCODING_NONE
));
}
regexpClass
.
defineAnnotatedMethods
(
RubyRegexp
.
class
);
return
regexpClass
;
}
private
static
ObjectAllocator
REGEXP_ALLOCATOR
=
new
ObjectAllocator
() {
public
IRubyObject
allocate
(
Ruby
runtime
,
RubyClass
klass
) {
return
new
RubyRegexp
(
runtime
,
klass
);
}
};
@
Override
public
int
getNativeTypeIndex
() {
return
ClassIndex
.
REGEXP
;
}
/** used by allocator
*/
private
RubyRegexp
(
Ruby
runtime
,
RubyClass
klass
) {
super
(
runtime
,
klass
);
this
.
options
=
new
RegexpOptions
();
}
/** default constructor
*/
private
RubyRegexp
(
Ruby
runtime
) {
super
(
runtime
,
runtime
.
getRegexp
());
this
.
options
=
new
RegexpOptions
();
}
private
RubyRegexp
(
Ruby
runtime
,
ByteList
str
) {
this
(
runtime
);
this
.
str
=
str
;
this
.
pattern
=
getRegexpFromCache
(
runtime
,
str
,
getEncoding
(
runtime
,
str
),
RegexpOptions
.
NULL_OPTIONS
);
}
private
RubyRegexp
(
Ruby
runtime
,
ByteList
str
,
RegexpOptions
options
) {
this
(
runtime
);
if
(
runtime
.
is1_9
()) {
initializeCommon19
(
str
,
str
.
getEncoding
(),
options
);
}
else
{
this
.
options
=
options
;
this
.
str
=
str
;
this
.
pattern
=
getRegexpFromCache
(
runtime
,
str
,
getEncoding
(
runtime
,
str
),
options
);
}
}
private
Encoding
getEncoding
(
Ruby
runtime
,
ByteList
str
) {
if
(
runtime
.
is1_9
())
return
str
.
getEncoding
();
// Whatever $KCODE is we should use
if
(
options
.
isKcodeDefault
())
return
runtime
.
getKCode
().
getEncoding
();
return
options
.
getKCode
().
getEncoding
();
}
// used only by the compiler/interpreter (will set the literal flag)
public
static
RubyRegexp
newRegexp
(
Ruby
runtime
,
String
pattern
,
RegexpOptions
options
) {
return
newRegexp
(
runtime
,
ByteList
.
create
(
pattern
),
options
);
}
// used only by the compiler/interpreter (will set the literal flag)
public
static
RubyRegexp
newRegexp
(
Ruby
runtime
,
ByteList
pattern
,
RegexpOptions
options
) {
try
{
return
new
RubyRegexp
(
runtime
,
pattern
,
options
);
}
catch
(
RaiseException
re
) {
throw
runtime
.
newSyntaxError
(
re
.
getMessage
());
}
}
// used only by the compiler/interpreter (will set the literal flag)
public
static
RubyRegexp
newDRegexp
(
Ruby
runtime
,
RubyString
pattern
,
RegexpOptions
options
) {
try
{
return
new
RubyRegexp
(
runtime
,
pattern
.
getByteList
(),
options
);
}
catch
(
RaiseException
re
) {
throw
runtime
.
newRegexpError
(
re
.
getMessage
());
}
}
// used only by the compiler/interpreter (will set the literal flag)
public
static
RubyRegexp
newDRegexp
(
Ruby
runtime
,
RubyString
pattern
,
int
joniOptions
) {
try
{
RegexpOptions
options
=
RegexpOptions
.
fromJoniOptions
(
joniOptions
);
return
new
RubyRegexp
(
runtime
,
pattern
.
getByteList
(),
options
);
}
catch
(
RaiseException
re
) {
throw
runtime
.
newRegexpError
(
re
.
getMessage
());
}
}
// used only by the compiler/interpreter (will set the literal flag)
public
static
RubyRegexp
newDRegexpEmbedded
(
Ruby
runtime
,
RubyString
pattern
,
int
embeddedOptions
) {
try
{
RegexpOptions
options
=
RegexpOptions
.
fromEmbeddedOptions
(
embeddedOptions
);
// FIXME: Massive hack (fix in DRegexpNode too for interpreter)
if
(
pattern
.
getEncoding
() ==
USASCIIEncoding
.
INSTANCE
) {
pattern
.
setEncoding
(
ASCIIEncoding
.
INSTANCE
);
}
return
new
RubyRegexp
(
runtime
,
pattern
.
getByteList
(),
options
);
}
catch
(
RaiseException
re
) {
throw
runtime
.
newRegexpError
(
re
.
getMessage
());
}
}
public
static
RubyRegexp
newDRegexpEmbedded19
(
Ruby
runtime
,
IRubyObject
[]
strings
,
int
embeddedOptions
) {
try
{
RegexpOptions
options
=
RegexpOptions
.
fromEmbeddedOptions
(
embeddedOptions
);
RubyString
pattern
=
preprocessDRegexp
(
runtime
,
strings
,
options
);
return
new
RubyRegexp
(
runtime
,
pattern
.
getByteList
(),
options
);
}
catch
(
RaiseException
re
) {
throw
runtime
.
newRegexpError
(
re
.
getMessage
());
}
}
public
static
RubyRegexp
newRegexp
(
Ruby
runtime
,
ByteList
pattern
) {
return
new
RubyRegexp
(
runtime
,
pattern
);
}
static
RubyRegexp
newRegexp
(
Ruby
runtime
,
ByteList
str
,
Regex
pattern
) {
RubyRegexp
regexp
=
new
RubyRegexp
(
runtime
);
regexp
.
str
=
str
;
regexp
.
options
=
RegexpOptions
.
fromJoniOptions
(
pattern
.
getOptions
());
regexp
.
pattern
=
pattern
;
return
regexp
;
}
// internal usage (Complex/Rational)
static
RubyRegexp
newDummyRegexp
(
Ruby
runtime
,
Regex
regex
) {
RubyRegexp
regexp
=
new
RubyRegexp
(
runtime
);
regexp
.
pattern
=
regex
;
regexp
.
str
=
ByteList
.
EMPTY_BYTELIST
;
regexp
.
options
.
setFixed
(
true
);
return
regexp
;
}
/** rb_reg_options
*/
public
RegexpOptions
getOptions
() {
check
();
return
options
;
}
public
final
Regex
getPattern
() {
check
();
return
pattern
;
}
private
static
void
encodingMatchError
(
Ruby
runtime
,
Regex
pattern
,
Encoding
strEnc
) {
throw
runtime
.
newEncodingCompatibilityError
(
"incompatible encoding regexp match ("
+
pattern
.
getEncoding
() +
" regexp with "
+
strEnc
+
" string)"
);
}
private
Encoding
checkEncoding
(
RubyString
str
,
boolean
warn
) {
if
(
str
.
scanForCodeRange
() ==
StringSupport
.
CR_BROKEN
) {
throw
getRuntime
().
newArgumentError
(
"invalid byte sequence in "
+
str
.
getEncoding
());
}
check
();
Encoding
enc
=
str
.
getEncoding
();
if
(!
enc
.
isAsciiCompatible
()) {
if
(
enc
!=
pattern
.
getEncoding
())
encodingMatchError
(
getRuntime
(),
pattern
,
enc
);
}
else
if
(
options
.
isFixed
()) {
if
(
enc
!=
pattern
.
getEncoding
() &&
(!
pattern
.
getEncoding
().
isAsciiCompatible
() ||
str
.
scanForCodeRange
() !=
StringSupport
.
CR_7BIT
))
encodingMatchError
(
getRuntime
(),
pattern
,
enc
);
enc
=
pattern
.
getEncoding
();
}
if
(
warn
&&
isEncodingNone
() &&
enc
!=
ASCIIEncoding
.
INSTANCE
&&
str
.
scanForCodeRange
() !=
StringSupport
.
CR_7BIT
) {
getRuntime
().
getWarnings
().
warn
(
ID
.
REGEXP_MATCH_AGAINST_STRING
,
"regexp match /.../n against to "
+
enc
+
" string"
);
}
return
enc
;
}
public
final
Regex
preparePattern
(
RubyString
str
) {
check
();
Encoding
enc
=
checkEncoding
(
str
,
true
);
if
(
enc
==
pattern
.
getEncoding
())
return
pattern
;
return
getPreprocessedRegexpFromCache
(
getRuntime
(),
this
.
str
,
enc
,
options
,
ErrorMode
.
PREPROCESS
);
}
static
Regex
preparePattern
(
Ruby
runtime
,
Regex
pattern
,
RubyString
str
) {
if
(
str
.
scanForCodeRange
() ==
StringSupport
.
CR_BROKEN
) {
throw
runtime
.
newArgumentError
(
"invalid byte sequence in "
+
str
.
getEncoding
());
}
Encoding
enc
=
str
.
getEncoding
();
if
(!
enc
.
isAsciiCompatible
()) {
if
(
enc
!=
pattern
.
getEncoding
())
encodingMatchError
(
runtime
,
pattern
,
enc
);
}
// TODO: check for isKCodeDefault() somehow
// if (warn && isEncodingNone() && enc != ASCIIEncoding.INSTANCE && str.scanForCodeRange() != StringSupport.CR_7BIT) {
// getRuntime().getWarnings().warn(ID.REGEXP_MATCH_AGAINST_STRING, "regexp match /.../n against to " + enc + " string");
// }
if
(
enc
==
pattern
.
getEncoding
())
return
pattern
;
return
getPreprocessedRegexpFromCache
(
runtime
, (
ByteList
)
pattern
.
getUserObject
(),
enc
,
RegexpOptions
.
fromJoniOptions
(
pattern
.
getOptions
()),
ErrorMode
.
PREPROCESS
);
}
private
static
enum
ErrorMode
{
RAISE
,
PREPROCESS
,
DESC
}
private
static
int
raisePreprocessError
(
Ruby
runtime
,
ByteList
str
,
String
err
,
ErrorMode
mode
) {
switch
(
mode
) {
case
RAISE
:
raiseRegexpError19
(
runtime
,
str
,
str
.
getEncoding
(),
RegexpOptions
.
NULL_OPTIONS
,
err
);
case
PREPROCESS
:
throw
runtime
.
newArgumentError
(
"regexp preprocess failed: "
+
err
);
case
DESC
:
// silent ?
}
return
0
;
}
private
static
int
readEscapedByte
(
Ruby
runtime
,
byte
[]
to
,
int
toP
,
byte
[]
bytes
,
int
p
,
int
end
,
ByteList
str
,
ErrorMode
mode
) {
if
(
p
==
end
||
bytes
[
p
++] != (
byte
)
'\\'
)
raisePreprocessError
(
runtime
,
str
,
"too short escaped multibyte character"
,
mode
);
boolean
metaPrefix
=
false
,
ctrlPrefix
=
false
;
int
code
=
0
;
while
(
true
) {
if
(
p
==
end
)
raisePreprocessError
(
runtime
,
str
,
"too short escape sequence"
,
mode
);
switch
(
bytes
[
p
++]) {
case
'\\'
:
code
=
'\\'
;
break
;
case
'n'
:
code
=
'\n'
;
break
;
case
't'
:
code
=
'\t'
;
break
;
case
'r'
:
code
=
'\r'
;
break
;
case
'f'
:
code
=
'\f'
;
break
;
case
'v'
:
code
=
'\013'
;
break
;
case
'a'
:
code
=
'\007'
;
break
;
case
'e'
:
code
=
'\033'
;
break
;
/* \OOO */
case
'0'
:
case
'1'
:
case
'2'
:
case
'3'
:
case
'4'
:
case
'5'
:
case
'6'
:
case
'7'
:
p
--;
int
olen
=
end
<
p
+
3
?
end
-
p
:
3
;
code
=
StringSupport
.
scanOct
(
bytes
,
p
,
olen
);
p
+=
StringSupport
.
octLength
(
bytes
,
p
,
olen
);
break
;
case
'x'
:
/* \xHH */
int
hlen
=
end
<
p
+
2
?
end
-
p
:
2
;
code
=
StringSupport
.
scanHex
(
bytes
,
p
,
hlen
);
int
len
=
StringSupport
.
hexLength
(
bytes
,
p
,
hlen
);
if
(
len
<
1
)
raisePreprocessError
(
runtime
,
str
,
"invalid hex escape"
,
mode
);
p
+=
len
;
break
;
case
'M'
:
/* \M-X, \M-\C-X, \M-\cX */
if
(
metaPrefix
)
raisePreprocessError
(
runtime
,
str
,
"duplicate meta escape"
,
mode
);
metaPrefix
=
true
;
if
(
p
+
1
<
end
&&
bytes
[
p
++] == (
byte
)
'-'
&& (
bytes
[
p
] &
0x80
) ==
0
) {
if
(
bytes
[
p
] == (
byte
)
'\\'
) {
p
++;
continue
;
}
else
{
code
=
bytes
[
p
++] &
0xff
;
break
;
}
}
raisePreprocessError
(
runtime
,
str
,
"too short meta escape"
,
mode
);
case
'C'
:
/* \C-X, \C-\M-X */
if
(
p
==
end
||
bytes
[
p
++] != (
byte
)
'-'
)
raisePreprocessError
(
runtime
,
str
,
"too short control escape"
,
mode
);
case
'c'
:
/* \cX, \c\M-X */
if
(
ctrlPrefix
)
raisePreprocessError
(
runtime
,
str
,
"duplicate control escape"
,
mode
);
ctrlPrefix
=
true
;
if
(
p
<
end
&& (
bytes
[
p
] &
0x80
) ==
0
) {
if
(
bytes
[
p
] == (
byte
)
'\\'
) {
p
++;
continue
;
}
else
{
code
=
bytes
[
p
++] &
0xff
;
break
;
}
}
raisePreprocessError
(
runtime
,
str
,
"too short control escape"
,
mode
);
default
:
raisePreprocessError
(
runtime
,
str
,
"unexpected escape sequence"
,
mode
);
}
// switch
if
(
code
<
0
||
code
>
0xff
)
raisePreprocessError
(
runtime
,
str
,
"invalid escape code"
,
mode
);
if
(
ctrlPrefix
)
code
&=
0x1f
;
if
(
metaPrefix
)
code
|=
0x80
;
to
[
toP
] = (
byte
)
code
;
return
p
;
}
// while
}
// MRI: unescape_escapted_nonascii
private
static
int
unescapeEscapedNonAscii
(
Ruby
runtime
,
ByteList
to
,
byte
[]
bytes
,
int
p
,
int
end
,
Encoding
enc
,
Encoding
[]
encp
,
ByteList
str
,
ErrorMode
mode
) {
byte
[]
chBuf
=
new
byte
[
enc
.
maxLength
()];
int
chLen
=
0
;
p
=
readEscapedByte
(
runtime
,
chBuf
,
chLen
++,
bytes
,
p
,
end
,
str
,
mode
);
while
(
chLen
<
enc
.
maxLength
() &&
StringSupport
.
preciseLength
(
enc
,
chBuf
,
0
,
chLen
) < -
1
) {
// MBCLEN_NEEDMORE_P
p
=
readEscapedByte
(
runtime
,
chBuf
,
chLen
++,
bytes
,
p
,
end
,
str
,
mode
);
}
int
cl
=
StringSupport
.
preciseLength
(
enc
,
chBuf
,
0
,
chLen
);
if
(
cl
== -
1
) {
raisePreprocessError
(
runtime
,
str
,
"invalid multibyte escape"
,
mode
);
// MBCLEN_INVALID_P
}
if
(
chLen
>
1
|| (
chBuf
[
0
] &
0x80
) !=
0
) {
to
.
append
(
chBuf
,
0
,
chLen
);
if
(
encp
[
0
] ==
null
) {
encp
[
0
] =
enc
;
}
else
if
(
encp
[
0
] !=
enc
) {
raisePreprocessError
(
runtime
,
str
,
"escaped non ASCII character in UTF-8 regexp"
,
mode
);
}
}
else
{
Sprintf
.
sprintf
(
runtime
,
to
,
"
\\
x%02X"
,
chBuf
[
0
] &
0xff
);
}
return
p
;
}
private
static
void
checkUnicodeRange
(
Ruby
runtime
,
int
code
,
ByteList
str
,
ErrorMode
mode
) {
// Unicode is can be only 21 bits long, int is enough
if
((
0xd800
<=
code
&&
code
<=
0xdfff
)
/* Surrogates */
||
0x10ffff
<
code
) {
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode range"
,
mode
);
}
}
private
static
void
appendUtf8
(
Ruby
runtime
,
ByteList
to
,
int
code
,
Encoding
[]
enc
,
ByteList
str
,
ErrorMode
mode
) {
checkUnicodeRange
(
runtime
,
code
,
str
,
mode
);
if
(
code
<
0x80
) {
Sprintf
.
sprintf
(
runtime
,
to
,
"
\\
x%02X"
,
code
);
}
else
{
to
.
ensure
(
to
.
getRealSize
() +
6
);
to
.
setRealSize
(
to
.
getRealSize
() +
Pack
.
utf8Decode
(
runtime
,
to
.
getUnsafeBytes
(),
to
.
getBegin
() +
to
.
getRealSize
(),
code
));
if
(
enc
[
0
] ==
null
) {
enc
[
0
] =
UTF8Encoding
.
INSTANCE
;
}
else
if
(!(
enc
[
0
]
instanceof
UTF8Encoding
)) {
// do not load the class if not used
raisePreprocessError
(
runtime
,
str
,
"UTF-8 character in non UTF-8 regexp"
,
mode
);
}
}
}
private
static
int
unescapeUnicodeList
(
Ruby
runtime
,
ByteList
to
,
byte
[]
bytes
,
int
p
,
int
end
,
Encoding
[]
encp
,
ByteList
str
,
ErrorMode
mode
) {
while
(
p
<
end
&&
ASCIIEncoding
.
INSTANCE
.
isSpace
(
bytes
[
p
] &
0xff
))
p
++;
boolean
hasUnicode
=
false
;
while
(
true
) {
int
code
=
StringSupport
.
scanHex
(
bytes
,
p
,
end
-
p
);
int
len
=
StringSupport
.
hexLength
(
bytes
,
p
,
end
-
p
);
if
(
len
==
0
)
break
;
if
(
len
>
6
)
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode range"
,
mode
);
p
+=
len
;
appendUtf8
(
runtime
,
to
,
code
,
encp
,
str
,
mode
);
hasUnicode
=
true
;
while
(
p
<
end
&&
ASCIIEncoding
.
INSTANCE
.
isSpace
(
bytes
[
p
] &
0xff
))
p
++;
}
if
(!
hasUnicode
)
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode list"
,
mode
);
return
p
;
}
private
static
int
unescapeUnicodeBmp
(
Ruby
runtime
,
ByteList
to
,
byte
[]
bytes
,
int
p
,
int
end
,
Encoding
[]
encp
,
ByteList
str
,
ErrorMode
mode
) {
if
(
p
+
4
>
end
)
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode escape"
,
mode
);
int
code
=
StringSupport
.
scanHex
(
bytes
,
p
,
4
);
int
len
=
StringSupport
.
hexLength
(
bytes
,
p
,
4
);
if
(
len
!=
4
)
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode escape"
,
mode
);
appendUtf8
(
runtime
,
to
,
code
,
encp
,
str
,
mode
);
return
p
+
4
;
}
private
static
boolean
unescapeNonAscii
(
Ruby
runtime
,
ByteList
to
,
byte
[]
bytes
,
int
p
,
int
end
,
Encoding
enc
,
Encoding
[]
encp
,
ByteList
str
,
ErrorMode
mode
) {
boolean
hasProperty
=
false
;
while
(
p
<
end
) {
int
cl
=
StringSupport
.
preciseLength
(
enc
,
bytes
,
p
,
end
);
if
(
cl
<=
0
)
raisePreprocessError
(
runtime
,
str
,
"invalid multibyte character"
,
mode
);
if
(
cl
>
1
|| (
bytes
[
p
] &
0x80
) !=
0
) {
to
.
append
(
bytes
,
p
,
cl
);
p
+=
cl
;
if
(
encp
[
0
] ==
null
) {
encp
[
0
] =
enc
;
}
else
if
(
encp
[
0
] !=
enc
) {
raisePreprocessError
(
runtime
,
str
,
"non ASCII character in UTF-8 regexp"
,
mode
);
}
continue
;
}
int
c
;
switch
(
c
=
bytes
[
p
++] &
0xff
) {
case
'\\'
:
if
(
p
==
end
)
raisePreprocessError
(
runtime
,
str
,
"too short escape sequence"
,
mode
);
switch
(
c
=
bytes
[
p
++] &
0xff
) {
case
'1'
:
case
'2'
:
case
'3'
:
case
'4'
:
case
'5'
:
case
'6'
:
case
'7'
:
/* \O, \OO, \OOO or backref */
if
(
StringSupport
.
scanOct
(
bytes
,
p
-
1
,
end
- (
p
-
1
)) <=
0177
) {
to
.
append
(
'\\'
).
append
(
c
);
break
;
}
case
'0'
:
/* \0, \0O, \0OO */
case
'x'
:
/* \xHH */
case
'c'
:
/* \cX, \c\M-X */
case
'C'
:
/* \C-X, \C-\M-X */
case
'M'
:
/* \M-X, \M-\C-X, \M-\cX */
p
=
unescapeEscapedNonAscii
(
runtime
,
to
,
bytes
,
p
-
2
,
end
,
enc
,
encp
,
str
,
mode
);
break
;
case
'u'
:
if
(
p
==
end
)
raisePreprocessError
(
runtime
,
str
,
"too short escape sequence"
,
mode
);
if
(
bytes
[
p
] == (
byte
)
'{'
) {
/* \\u{H HH HHH HHHH HHHHH HHHHHH ...} */
p
++;
p
=
unescapeUnicodeList
(
runtime
,
to
,
bytes
,
p
,
end
,
encp
,
str
,
mode
);
if
(
p
==
end
||
bytes
[
p
++] != (
byte
)
'}'
)
raisePreprocessError
(
runtime
,
str
,
"invalid Unicode list"
,
mode
);
}
else
{
/* \\uHHHH */
p
=
unescapeUnicodeBmp
(
runtime
,
to
,
bytes
,
p
,
end
,
encp
,
str
,
mode
);
}
break
;
case
'p'
:
/* \p{Hiragana} */
if
(
encp
[
0
] ==
null
)
hasProperty
=
true
;
to
.
append
(
'\\'
).
append
(
c
);
break
;
default
:
to
.
append
(
'\\'
).
append
(
c
);
break
;
}
// inner switch
break
;
default
:
to
.
append
(
c
);
}
// switch
}
// while
return
hasProperty
;
}
private
static
ByteList
preprocess
(
Ruby
runtime
,
ByteList
str
,
Encoding
enc
,
Encoding
[]
fixedEnc
,
ErrorMode
mode
) {
ByteList
to
=
new
ByteList
(
str
.
getRealSize
());
if
(
enc
.
isAsciiCompatible
()) {
fixedEnc
[
0
] =
null
;
}
else
{
fixedEnc
[
0
] =
enc
;
to
.
setEncoding
(
enc
);
}
boolean
hasProperty
=
unescapeNonAscii
(
runtime
,
to
,
str
.
getUnsafeBytes
(),
str
.
getBegin
(),
str
.
getBegin
() +
str
.
getRealSize
(),
enc
,
fixedEnc
,
str
,
mode
);
if
(
hasProperty
&&
fixedEnc
[
0
] ==
null
)
fixedEnc
[
0
] =
enc
;
if
(
fixedEnc
[
0
] !=
null
)
to
.
setEncoding
(
fixedEnc
[
0
]);
return
to
;
}
public
static
void
preprocessCheck
(
Ruby
runtime
,
ByteList
bytes
) {
preprocess
(
runtime
,
bytes
,
bytes
.
getEncoding
(),
new
Encoding
[]{
null
},
ErrorMode
.
RAISE
);
}
// rb_reg_preprocess_dregexp
public
static
RubyString
preprocessDRegexp
(
Ruby
runtime
,
IRubyObject
[]
strings
,
RegexpOptions
options
) {
RubyString
string
=
null
;
Encoding
regexpEnc
=
null
;
for
(
int
i
=
0
;
i
<
strings
.
length
;
i
++) {
RubyString
str
=
strings
[
i
].
convertToString
();
Encoding
strEnc
=
str
.
getEncoding
();
if
(
options
.
isEncodingNone
() &&
strEnc
!=
ASCIIEncoding
.
INSTANCE
) {
if
(
str
.
scanForCodeRange
() !=
StringSupport
.
CR_7BIT
) {
throw
runtime
.
newRegexpError
(
"/.../n has a non escaped non ASCII character in non ASCII-8BIT script"
);
}
strEnc
=
ASCIIEncoding
.
INSTANCE
;
}
Encoding
[]
fixedEnc
=
new
Encoding
[
1
];
ByteList
buf
=
RubyRegexp
.
preprocess
(
runtime
,
str
.
getByteList
(),
strEnc
,
fixedEnc
,
RubyRegexp
.
ErrorMode
.
PREPROCESS
);
if
(
fixedEnc
[
0
] !=
null
) {
if
(
regexpEnc
!=
null
&&
regexpEnc
!=
fixedEnc
[
0
]) {
throw
runtime
.
newRegexpError
(
"encoding mismatch in dynamic regexp: "
+
new
String
(
regexpEnc
.
getName
()) +
" and "
+
new
String
(
fixedEnc
[
0
].
getName
()));
}
regexpEnc
=
fixedEnc
[
0
];
}
if
(
string
==
null
) {
string
= (
RubyString
)
str
.
dup
();
}
else
{
string
.
append19
(
str
);
}
}
if
(
regexpEnc
!=
null
) {
string
.
setEncoding
(
regexpEnc
);
}
return
string
;
}
private
void
check
() {
if
(
pattern
==
null
)
throw
getRuntime
().
newTypeError
(
"uninitialized Regexp"
);
}
@
JRubyMethod
(
name
= {
"new"
,
"compile"
},
rest
=
true
,
meta
=
true
)
public
static
RubyRegexp
newInstance
(
IRubyObject
recv
,
IRubyObject
[]
args
) {
RubyClass
klass
= (
RubyClass
)
recv
;
RubyRegexp
re
= (
RubyRegexp
)
klass
.
allocate
();
re
.
callInit
(
args
,
Block
.
NULL_BLOCK
);
return
re
;
}
@
JRubyMethod
(
name
=
"try_convert"
,
meta
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
try_convert
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
args
) {
return
TypeConverter
.
convertToTypeWithCheck
(
args
,
context
.
runtime
.
getRegexp
(),
"to_regexp"
);
}
/** rb_reg_s_quote
*
*/
@
JRubyMethod
(
name
= {
"quote"
,
"escape"
},
required
=
1
,
optional
=
1
,
meta
=
true
,
compat
=
CompatVersion
.
RUBY1_8
)
public
static
RubyString
quote
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
[]
args
) {
Ruby
runtime
=
context
.
runtime
;
final
KCode
code
;
if
(
args
.
length
==
1
||
args
[
1
].
isNil
()) {
code
=
runtime
.
getKCode
();
}
else
{
code
=
KCode
.
create
(
runtime
,
args
[
1
].
toString
());
}
RubyString
src
=
args
[
0
].
convertToString
();
RubyString
dst
=
RubyString
.
newStringShared
(
runtime
,
quote
(
src
.
getByteList
(),
code
.
getEncoding
()));
dst
.
infectBy
(
src
);
return
dst
;
}
@
JRubyMethod
(
name
= {
"quote"
,
"escape"
},
meta
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
quote19
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
arg
) {
Ruby
runtime
=
context
.
runtime
;
RubyString
str
=
operandCheck
(
runtime
,
arg
);
return
RubyString
.
newStringShared
(
runtime
,
quote19
(
str
.
getByteList
(),
str
.
isAsciiOnly
()));
}
/** rb_reg_quote
*
*/
private
static
ByteList
quote
(
ByteList
bs
,
Encoding
enc
) {
int
p
=
bs
.
getBegin
();
int
end
=
p
+
bs
.
getRealSize
();
byte
[]
bytes
=
bs
.
getUnsafeBytes
();
metaFound
:
do
{
for
(;
p
<
end
;
p
++) {
int
c
=
bytes
[
p
] &
0xff
;
int
cl
=
enc
.
length
(
bytes
,
p
,
end
);
if
(
cl
!=
1
) {
while
(
cl
-- >
0
&&
p
<
end
)
p
++;
p
--;
continue
;
}
switch
(
c
) {
case
'['
:
case
']'
:
case
'{'
:
case
'}'
:
case
'('
:
case
')'
:
case
'|'
:
case
'-'
:
case
'*'
:
case
'.'
:
case
'\\'
:
case
'?'
:
case
'+'
:
case
'^'
:
case
'$'
:
case
' '
:
case
'#'
:
case
'\t'
:
case
'\f'
:
case
'\n'
:
case
'\r'
:
break
metaFound
;
}
}
return
bs
;
}
while
(
false
);
ByteList
result
=
new
ByteList
(
end
*
2
);
byte
[]
obytes
=
result
.
getUnsafeBytes
();
int
op
=
p
-
bs
.
getBegin
();
System
.
arraycopy
(
bytes
,
bs
.
getBegin
(),
obytes
,
0
,
op
);
for
(;
p
<
end
;
p
++) {
int
c
=
bytes
[
p
] &
0xff
;
int
cl
=
enc
.
length
(
bytes
,
p
,
end
);
if
(
cl
!=
1
) {
while
(
cl
-- >
0
&&
p
<
end
)
obytes
[
op
++] =
bytes
[
p
++];
p
--;
continue
;
}
switch
(
c
) {
case
'['
:
case
']'
:
case
'{'
:
case
'}'
:
case
'('
:
case
')'
:
case
'|'
:
case
'-'
:
case
'*'
:
case
'.'
:
case
'\\'
:
case
'?'
:
case
'+'
:
case
'^'
:
case
'$'
:
case
'#'
:
obytes
[
op
++] =
'\\'
;
break
;
case
' '
:
obytes
[
op
++] =
'\\'
;
obytes
[
op
++] =
' '
;
continue
;
case
'\t'
:
obytes
[
op
++] =
'\\'
;
obytes
[
op
++] =
't'
;
continue
;
case
'\n'
:
obytes
[
op
++] =
'\\'
;
obytes
[
op
++] =
'n'
;
continue
;
case
'\r'
:
obytes
[
op
++] =
'\\'
;
obytes
[
op
++] =
'r'
;
continue
;
case
'\f'
:
obytes
[
op
++] =
'\\'
;
obytes
[
op
++] =
'f'
;
continue
;
}
obytes
[
op
++] = (
byte
)
c
;
}
result
.
setRealSize
(
op
);
return
result
;
}
private
static
final
int
QUOTED_V
=
11
;
static
ByteList
quote19
(
ByteList
bs
,
boolean
asciiOnly
) {
int
p
=
bs
.
getBegin
();
int
end
=
p
+
bs
.
getRealSize
();
byte
[]
bytes
=
bs
.
getUnsafeBytes
();
Encoding
enc
=
bs
.
getEncoding
();
metaFound
:
do
{
while
(
p
<
end
) {
final
int
c
;
final
int
cl
;
if
(
enc
.
isAsciiCompatible
()) {
cl
=
1
;
c
=
bytes
[
p
] &
0xff
;
}
else
{
cl
=
StringSupport
.
preciseLength
(
enc
,
bytes
,
p
,
end
);
c
=
enc
.
mbcToCode
(
bytes
,
p
,
end
);
}
if
(!
Encoding
.
isAscii
(
c
)) {
p
+=
StringSupport
.
length
(
enc
,
bytes
,
p
,
end
);
continue
;
}
switch
(
c
) {
case
'['
:
case
']'
:
case
'{'
:
case
'}'
:
case
'('
:
case
')'
:
case
'|'
:
case
'-'
:
case
'*'
:
case
'.'
:
case
'\\'
:
case
'?'
:
case
'+'
:
case
'^'
:
case
'$'
:
case
' '
:
case
'#'
:
case
'\t'
:
case
'\f'
:
case
QUOTED_V
:
case
'\n'
:
case
'\r'
:
break
metaFound
;
}
p
+=
cl
;
}
if
(
asciiOnly
) {
ByteList
tmp
=
bs
.
shallowDup
();
tmp
.
setEncoding
(
USASCIIEncoding
.
INSTANCE
);
return
tmp
;
}
return
bs
;
}
while
(
false
);
ByteList
result
=
new
ByteList
(
end
*
2
);
result
.
setEncoding
(
asciiOnly
?
USASCIIEncoding
.
INSTANCE
:
bs
.
getEncoding
());
byte
[]
obytes
=
result
.
getUnsafeBytes
();
int
op
=
p
-
bs
.
getBegin
();
System
.
arraycopy
(
bytes
,
bs
.
getBegin
(),
obytes
,
0
,
op
);
while
(
p
<
end
) {
final
int
c
;
final
int
cl
;
if
(
enc
.
isAsciiCompatible
()) {
cl
=
1
;
c
=
bytes
[
p
] &
0xff
;
}
else
{
cl
=
StringSupport
.
preciseLength
(
enc
,
bytes
,
p
,
end
);
c
=
enc
.
mbcToCode
(
bytes
,
p
,
end
);
}
if
(!
Encoding
.
isAscii
(
c
)) {
int
n
=
StringSupport
.
length
(
enc
,
bytes
,
p
,
end
);
while
(
n
-- >
0
)
obytes
[
op
++] =
bytes
[
p
++];
continue
;
}
p
+=
cl
;
switch
(
c
) {
case
'['
:
case
']'
:
case
'{'
:
case
'}'
:
case
'('
:
case
')'
:
case
'|'
:
case
'-'
:
case
'*'
:
case
'.'
:
case
'\\'
:
case
'?'
:
case
'+'
:
case
'^'
:
case
'$'
:
case
'#'
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
break
;
case
' '
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
' '
,
obytes
,
op
);
continue
;
case
'\t'
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
't'
,
obytes
,
op
);
continue
;
case
'\n'
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
'n'
,
obytes
,
op
);
continue
;
case
'\r'
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
'r'
,
obytes
,
op
);
continue
;
case
'\f'
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
'f'
,
obytes
,
op
);
continue
;
case
QUOTED_V
:
op
+=
enc
.
codeToMbc
(
'\\'
,
obytes
,
op
);
op
+=
enc
.
codeToMbc
(
'v'
,
obytes
,
op
);
continue
;
}
op
+=
enc
.
codeToMbc
(
c
,
obytes
,
op
);
}
result
.
setRealSize
(
op
);
return
result
;
}
/**
* Variable arity version for compatibility. Not bound to a Ruby method.
* @deprecated Use the versions with zero, one, or two args.
*/
public
static
IRubyObject
last_match_s
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
[]
args
) {
switch
(
args
.
length
) {
case
0
:
return
last_match_s
(
context
,
recv
);
case
1
:
return
last_match_s
(
context
,
recv
,
args
[
0
]);
default
:
Arity
.
raiseArgumentError
(
context
.
runtime
,
args
.
length
,
0
,
1
);
return
null
;
// not reached
}
}
/** rb_reg_s_last_match / match_getter
*
*/
@
JRubyMethod
(
name
=
"last_match"
,
meta
=
true
,
reads
=
BACKREF
)
public
static
IRubyObject
last_match_s
(
ThreadContext
context
,
IRubyObject
recv
) {
IRubyObject
match
=
context
.
getCurrentScope
().
getBackRef
(
context
.
runtime
);
if
(
match
instanceof
RubyMatchData
) ((
RubyMatchData
)
match
).
use
();
return
match
;
}
/** rb_reg_s_last_match
*
*/
@
JRubyMethod
(
name
=
"last_match"
,
meta
=
true
,
reads
=
BACKREF
)
public
static
IRubyObject
last_match_s
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
nth
) {
IRubyObject
match
=
context
.
getCurrentScope
().
getBackRef
(
context
.
runtime
);
if
(
match
.
isNil
())
return
match
;
return
nth_match
(((
RubyMatchData
)
match
).
backrefNumber
(
nth
),
match
);
}
/** rb_reg_s_union
*
*/
@
JRubyMethod
(
name
=
"union"
,
rest
=
true
,
meta
=
true
,
compat
=
CompatVersion
.
RUBY1_8
)
public
static
IRubyObject
union
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
[]
args
) {
Ruby
runtime
=
context
.
runtime
;
if
(
args
.
length
==
0
)
return
newRegexp
(
runtime
,
ByteList
.
create
(
"(?!)"
));
IRubyObject
[]
realArgs
=
args
;
if
(
args
.
length
==
1
) {
// The power of the union of one!
IRubyObject
v
=
TypeConverter
.
convertToTypeWithCheck
(
args
[
0
],
runtime
.
getRegexp
(),
"to_regexp"
);
if
(!
v
.
isNil
())
return
v
;
IRubyObject
a
=
TypeConverter
.
convertToTypeWithCheck
(
args
[
0
],
runtime
.
getArray
(),
"to_ary"
);
if
(
a
.
isNil
())
return
newRegexp
(
runtime
,
quote
(
context
,
recv
,
args
).
getByteList
());
RubyArray
aa
= (
RubyArray
)
a
;
int
len
=
aa
.
getLength
();
realArgs
=
new
IRubyObject
[
len
];
for
(
int
i
=
0
;
i
<
len
;
i
++) {
realArgs
[
i
] =
aa
.
entry
(
i
);
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL