+ # Returns `self` with all characters escaped with their UTF-16 representation
+ #
+ # assert "Aรจใ๐".escape_to_utf16 == "\\u0041\\u00e8\\u3042\\ud800\\udfd3"
+ fun escape_to_utf16: String do
+ var buf = new Buffer
+ for i in chars do buf.append i.escape_to_utf16
+ return buf.to_s
+ end
+
+ # Returns the Unicode char escaped by `self`
+ #
+ # assert "\\u0041".from_utf16_escape == 'A'
+ # assert "\\ud800\\udfd3".from_utf16_escape == '๐'
+ # assert "\\u00e8".from_utf16_escape == 'รจ'
+ # assert "\\u3042".from_utf16_escape == 'ใ'
+ fun from_utf16_escape: Char do
+ var ln = length
+ if ln != 6 and ln != 12 then return 0xFFFD.code_point
+ var cphi = substring(2, 4).to_hex
+ if cphi < 0xD800 then return cphi.code_point
+ if cphi > 0xDFFF then return cphi.code_point
+ if cphi > 0xDBFF then return 0xFFFD.code_point
+ var cp = 0
+ cp += (cphi - 0xD800) << 10
+ var cplo = substring(8, 4).to_hex
+ if cplo < 0xDC00 then return 0xFFFD.code_point
+ if cplo > 0xDFFF then return 0xFFFD.code_point
+ cp += cplo - 0xDC00
+ cp += 0x10000
+ return cp.code_point
+ end
+