@@ -107,6 +107,7 @@ def debug_token_value(token_name)
107
107
}
108
108
UTF_8 = /\\ u(?:([\d Aa-f]{4})|\{ ([\d a-f]{4,})\} )(?:\\ u([\d Aa-f]{4}))?/i
109
109
VALID_STRING = /\A (?:[^\\ ]|#{ ESCAPES } |#{ UTF_8 } )*\z /o
110
+ ESCAPED = /(?:#{ ESCAPES } |#{ UTF_8 } )/o
110
111
111
112
def string_value
112
113
str = token_value
@@ -299,24 +300,27 @@ module ByteFor
299
300
# Replace any escaped unicode or whitespace with the _actual_ characters
300
301
# To avoid allocating more strings, this modifies the string passed into it
301
302
def self . replace_escaped_characters_in_place ( raw_string )
302
- raw_string . gsub! ( ESCAPES , ESCAPES_REPLACE )
303
- raw_string . gsub! ( UTF_8 ) do |_matched_str |
304
- codepoint_1 = ( $1 || $2) . to_i ( 16 )
305
- codepoint_2 = $3
303
+ raw_string . gsub! ( ESCAPED ) do |matched_str |
304
+ if ( res = ESCAPES_REPLACE [ matched_str ] )
305
+ res
306
+ else
307
+ codepoint_1 = ( $1 || $2) . to_i ( 16 )
308
+ codepoint_2 = $3
306
309
307
- if codepoint_2
308
- codepoint_2 = codepoint_2 . to_i ( 16 )
309
- if ( codepoint_1 >= 0xD800 && codepoint_1 <= 0xDBFF ) && # leading surrogate
310
- ( codepoint_2 >= 0xDC00 && codepoint_2 <= 0xDFFF ) # trailing surrogate
311
- # A surrogate pair
312
- combined = ( ( codepoint_1 - 0xD800 ) * 0x400 ) + ( codepoint_2 - 0xDC00 ) + 0x10000
313
- [ combined ] . pack ( 'U' . freeze )
310
+ if codepoint_2
311
+ codepoint_2 = codepoint_2 . to_i ( 16 )
312
+ if ( codepoint_1 >= 0xD800 && codepoint_1 <= 0xDBFF ) && # leading surrogate
313
+ ( codepoint_2 >= 0xDC00 && codepoint_2 <= 0xDFFF ) # trailing surrogate
314
+ # A surrogate pair
315
+ combined = ( ( codepoint_1 - 0xD800 ) * 0x400 ) + ( codepoint_2 - 0xDC00 ) + 0x10000
316
+ [ combined ] . pack ( 'U' . freeze )
317
+ else
318
+ # Two separate code points
319
+ [ codepoint_1 ] . pack ( 'U' . freeze ) + [ codepoint_2 ] . pack ( 'U' . freeze )
320
+ end
314
321
else
315
- # Two separate code points
316
- [ codepoint_1 ] . pack ( 'U' . freeze ) + [ codepoint_2 ] . pack ( 'U' . freeze )
322
+ [ codepoint_1 ] . pack ( 'U' . freeze )
317
323
end
318
- else
319
- [ codepoint_1 ] . pack ( 'U' . freeze )
320
324
end
321
325
end
322
326
nil
0 commit comments