Method: GraphQL::Language::Lexer.replace_escaped_characters_in_place

Defined in:
lib/graphql/language/lexer.rb

.replace_escaped_characters_in_place(raw_string) ⇒ Object

Replace any escaped unicode or whitespace with the actual characters To avoid allocating more strings, this modifies the string passed into it



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/graphql/language/lexer.rb', line 335

def self.replace_escaped_characters_in_place(raw_string)
  raw_string.gsub!(ESCAPED) do |matched_str|
    if (point_str_1 = $1 || $2)
      codepoint_1 = point_str_1.to_i(16)
      if (codepoint_2 = $3)
        codepoint_2 = codepoint_2.to_i(16)
        if (codepoint_1 >= 0xD800 && codepoint_1 <= 0xDBFF) && # leading surrogate
            (codepoint_2 >= 0xDC00 && codepoint_2 <= 0xDFFF) # trailing surrogate
          # A surrogate pair
          combined = ((codepoint_1 - 0xD800) * 0x400) + (codepoint_2 - 0xDC00) + 0x10000
          [combined].pack('U'.freeze)
        else
          # Two separate code points
          [codepoint_1].pack('U'.freeze) + [codepoint_2].pack('U'.freeze)
        end
      else
        [codepoint_1].pack('U'.freeze)
      end
    else
      ESCAPES_REPLACE[matched_str]
    end
  end
  nil
end