Method: Opal::Nodes::StringNode#to_utf16

Defined in:
lib/opal/nodes/literal.rb

#to_utf16(code_point) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/opal/nodes/literal.rb', line 89

def to_utf16(code_point)
  ten_bits = 0b1111111111
  u = ->(code_unit) { '\\u' + code_unit.to_s(16).upcase }

  return u.call(code_point) if code_point <= 0xFFFF

  code_point -= 0x10000

  # Shift right to get to most significant 10 bits
  lead_surrogate = 0xD800 + (code_point >> 10)

  # Mask to get least significant 10 bits
  tail_surrogate = 0xDC00 + (code_point & ten_bits)

  u.call(lead_surrogate) + u.call(tail_surrogate)
end