method
u16tou8
v1_8_7_72 -
Show latest stable
- Class:
Net::IMAP
u16tou8(s)private
No documentation available.
# File lib/net/imap.rb, line 1225
def self.u16tou8(s)
len = s.length
if len < 2
return ""
end
buf = ""
i = 0
while i < len
c = s[i] << 8 | s[i + 1]
i += 2
if c == 0xfeff
next
elsif c < 0x0080
buf.concat(c)
elsif c < 0x0800
b2 = c & 0x003f
b1 = c >> 6
buf.concat(b1 | 0xc0)
buf.concat(b2 | 0x80)
elsif c >= 0xdc00 && c < 0xe000
raise DataFormatError, "invalid surrogate detected"
elsif c >= 0xd800 && c < 0xdc00
if i + 2 > len
raise DataFormatError, "invalid surrogate detected"
end
low = s[i] << 8 | s[i + 1]
i += 2
if low < 0xdc00 || low > 0xdfff
raise DataFormatError, "invalid surrogate detected"
end
c = (((c & 0x03ff)) << 10 | (low & 0x03ff)) + 0x10000
b4 = c & 0x003f
b3 = (c >> 6) & 0x003f
b2 = (c >> 12) & 0x003f
b1 = c >> 18;
buf.concat(b1 | 0xf0)
buf.concat(b2 | 0x80)
buf.concat(b3 | 0x80)
buf.concat(b4 | 0x80)
else # 0x0800-0xffff
b3 = c & 0x003f
b2 = (c >> 6) & 0x003f
b1 = c >> 12
buf.concat(b1 | 0xe0)
buf.concat(b2 | 0x80)
buf.concat(b3 | 0x80)
end
end
return buf
end