@@ -5,31 +5,11 @@ class EncodedString
5
5
# Reduce allocations by storing constants.
6
6
UTF_8 = "UTF-8"
7
7
US_ASCII = "US-ASCII"
8
- #
9
- # In MRI 2.1 'invalid: :replace' changed to also replace an invalid byte sequence
10
- # see https://github.com/ruby/ruby/blob/v2_1_0/NEWS#L176
11
- # https://www.ruby-forum.com/topic/6861247
12
- # https://twitter.com/nalsh/status/553413844685438976
13
- #
14
- # For example, given:
15
- # "\x80".force_encoding("Emacs-Mule").encode(:invalid => :replace).bytes.to_a
16
- #
17
- # On MRI 2.1 or above: 63 # '?'
18
- # else : 128 # "\x80"
19
- #
8
+
20
9
# Ruby's default replacement string is:
21
10
# U+FFFD ("\xEF\xBF\xBD"), for Unicode encoding forms, else
22
11
# ? ("\x3F")
23
12
REPLACE = "?"
24
- ENCODE_UNCONVERTABLE_BYTES = {
25
- :invalid => :replace ,
26
- :undef => :replace ,
27
- :replace => REPLACE
28
- }
29
- ENCODE_NO_CONVERTER = {
30
- :invalid => :replace ,
31
- :replace => REPLACE
32
- }
33
13
34
14
def initialize ( string , encoding = nil )
35
15
@encoding = encoding
@@ -112,34 +92,25 @@ def matching_encoding(string)
112
92
string = remove_invalid_bytes ( string )
113
93
string . encode ( @encoding )
114
94
rescue Encoding ::UndefinedConversionError , Encoding ::InvalidByteSequenceError
115
- encode_unconvertable_bytes ( string )
95
+ # Originally defined as a constant to avoid uneeded allocations, this hash must
96
+ # be defined inline (without {}) to avoid warnings on Ruby 2.7
97
+ #
98
+ # In MRI 2.1 'invalid: :replace' changed to also replace an invalid byte sequence
99
+ # see https://github.com/ruby/ruby/blob/v2_1_0/NEWS#L176
100
+ # https://www.ruby-forum.com/topic/6861247
101
+ # https://twitter.com/nalsh/status/553413844685438976
102
+ #
103
+ # For example, given:
104
+ # "\x80".force_encoding("Emacs-Mule").encode(:invalid => :replace).bytes.to_a
105
+ #
106
+ # On MRI 2.1 or above: 63 # '?'
107
+ # else : 128 # "\x80"
108
+ #
109
+ string . encode ( @encoding , :invalid => :replace , :undef => :replace , :replace => REPLACE )
116
110
rescue Encoding ::ConverterNotFoundError
117
- encode_no_converter ( string . dup . force_encoding ( @encoding ) )
118
- end
119
-
120
- # On Ruby 2.7.0 keyword arguments mixed with conventional cause a warning to
121
- # be issued requiring us to be explicit by using a ** to pass the hash as
122
- # keyword arguments. Any keyword argument supporting Ruby supports this.
123
- if RubyFeatures . kw_args_supported?
124
- # Note on non keyword supporting Ruby ** causes a syntax error hence
125
- # we must use eval. To be removed in RSpec 4.
126
- binding . eval ( <<-CODE , __FILE__ , __LINE__ )
127
- def encode_unconvertable_bytes(string)
128
- string.encode(@encoding, **ENCODE_UNCONVERTABLE_BYTES)
129
- end
130
-
131
- def encode_no_converter(string)
132
- string.encode(**ENCODE_NO_CONVERTER)
133
- end
134
- CODE
135
- else
136
- def encode_unconvertable_bytes ( string )
137
- string . encode ( @encoding , ENCODE_UNCONVERTABLE_BYTES )
138
- end
139
-
140
- def encode_no_converter ( string )
141
- string . encode ( ENCODE_NO_CONVERTER )
142
- end
111
+ # Originally defined as a constant to avoid uneeded allocations, this hash must
112
+ # be defined inline (without {}) to avoid warnings on Ruby 2.7
113
+ string . dup . force_encoding ( @encoding ) . encode ( :invalid => :replace , :replace => REPLACE )
143
114
end
144
115
145
116
# Prevents raising ArgumentError
0 commit comments