@@ -55,22 +55,23 @@ def self.try_convert(obj)
55
55
Truffle ::Type . try_convert obj , Regexp , :to_regexp
56
56
end
57
57
58
- def self . convert ( pattern )
59
- return pattern if Primitive . is_a? ( pattern , Regexp )
60
- if Primitive . is_a? ( pattern , Array )
61
- union ( *pattern )
62
- else
63
- Regexp . quote ( pattern . to_s )
64
- end
65
- end
58
+ def self . negotiate_union_encoding ( *patterns )
59
+ res = nil
60
+
61
+ patterns . each do |pattern |
62
+ converted = Primitive . is_a? ( pattern , Regexp ) ? pattern : Regexp . quote ( pattern )
66
63
67
- def self . compatible? ( *patterns )
68
- encodings = patterns . map { |r | convert ( r ) . encoding }
69
- last_enc = encodings . pop
70
- encodings . each do |encoding |
71
- raise ArgumentError , "incompatible encodings: #{ encoding } and #{ last_enc } " unless Primitive . encoding_compatible? ( last_enc , encoding )
72
- last_enc = encoding
64
+ enc = converted . encoding
65
+
66
+ if Primitive . nil? ( res )
67
+ res = enc
68
+ else
69
+ res = Primitive . encoding_compatible? ( enc , res )
70
+ raise ArgumentError , "incompatible encodings: #{ enc } and #{ res } " unless res
71
+ end
73
72
end
73
+
74
+ res
74
75
end
75
76
76
77
def self . last_match ( index = nil )
@@ -96,37 +97,35 @@ def self.last_match(index = nil)
96
97
def self . union ( *patterns )
97
98
case patterns . size
98
99
when 0
99
- return %r/(?!)/
100
+ %r/(?!)/
100
101
when 1
101
102
pattern = patterns . first
102
103
case pattern
103
104
when Array
104
- return union ( *pattern )
105
+ union ( *pattern )
105
106
else
106
107
converted = Truffle ::Type . rb_check_convert_type ( pattern , Regexp , :to_regexp )
107
108
if Primitive . nil? converted
108
- return Regexp . new ( Regexp . quote ( pattern ) )
109
+ Regexp . new ( Regexp . quote ( pattern ) )
109
110
else
110
- return converted
111
+ converted
111
112
end
112
113
end
113
114
else
114
- compatible? ( *patterns )
115
- enc = convert ( patterns . first ) . encoding
116
- end
115
+ patterns = patterns . map do |pat |
116
+ if Primitive . is_a? ( pat , Regexp )
117
+ pat
118
+ else
119
+ StringValue ( pat )
120
+ end
121
+ end
117
122
118
- sep = '|' . encode ( enc )
119
- str = '' . encode ( enc )
123
+ enc = negotiate_union_encoding ( *patterns )
124
+ sep = '|' . encode ( enc )
125
+ str = '' . encode ( enc )
120
126
121
- patterns = patterns . map do |pat |
122
- if Primitive . is_a? ( pat , Regexp )
123
- pat
124
- else
125
- StringValue ( pat )
126
- end
127
+ Truffle ::RegexpOperations . union ( str , sep , *patterns )
127
128
end
128
-
129
- Truffle ::RegexpOperations . union ( str , sep , *patterns )
130
129
end
131
130
Truffle ::Graal . always_split ( method ( :union ) )
132
131
0 commit comments