core :: Char :: escape_to_utf16
self
escaped to UTF-16i.e. Represents self
.code_point
using UTF-16 codets escaped
with a \u
assert 'A'.escape_to_utf16 == "\\u0041"
assert 'รจ'.escape_to_utf16 == "\\u00e8"
assert 'ใ'.escape_to_utf16 == "\\u3042"
assert '๐'.escape_to_utf16 == "\\ud800\\udfd3"
# Returns `self` escaped to UTF-16
#
# i.e. Represents `self`.`code_point` using UTF-16 codets escaped
# with a `\u`
#
# ~~~
# assert 'A'.escape_to_utf16 == "\\u0041"
# assert 'รจ'.escape_to_utf16 == "\\u00e8"
# assert 'ใ'.escape_to_utf16 == "\\u3042"
# assert '๐'.escape_to_utf16 == "\\ud800\\udfd3"
# ~~~
fun escape_to_utf16: String do
var cp = code_point
var buf: Buffer
if cp < 0xD800 or (cp >= 0xE000 and cp <= 0xFFFF) then
buf = new Buffer.with_cap(6)
buf.append("\\u0000")
var hx = cp.to_hex
var outid = 5
for i in hx.chars.reverse_iterator do
buf[outid] = i
outid -= 1
end
else
buf = new Buffer.with_cap(12)
buf.append("\\u0000\\u0000")
var lo = (((cp - 0x10000) & 0x3FF) + 0xDC00).to_hex
var hi = ((((cp - 0x10000) & 0xFFC00) >> 10) + 0xD800).to_hex
var out = 2
for i in hi do
buf[out] = i
out += 1
end
out = 8
for i in lo do
buf[out] = i
out += 1
end
end
return buf.to_s
end
lib/core/text/abstract_text.nit:2201,2--2241,4