method
u8tou16
v1_8_7_330 -
Show latest stable
-
0 notes -
Class: Net::IMAP
- 1_8_6_287 (0)
- 1_8_7_72 (0)
- 1_8_7_330 (0)
- 1_9_1_378
- 1_9_2_180
- 1_9_3_125
- 1_9_3_392
- 2_1_10
- 2_2_9
- 2_4_6
- 2_5_5
- 2_6_3
- What's this?
u8tou16(s)
private
Hide source
# File lib/net/imap.rb, line 1307 def self.u8tou16(s) len = s.length buf = "" i = 0 while i < len c = s[i] if (c & 0x80) == 0 buf.concat(0x00) buf.concat(c) i += 1 elsif (c & 0xe0) == 0xc0 && len >= 2 && (s[i + 1] & 0xc0) == 0x80 if c == 0xc0 || c == 0xc1 raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c) end u = ((c & 0x1f) << 6) | (s[i + 1] & 0x3f) buf.concat(u >> 8) buf.concat(u & 0x00ff) i += 2 elsif (c & 0xf0) == 0xe0 && i + 2 < len && (s[i + 1] & 0xc0) == 0x80 && (s[i + 2] & 0xc0) == 0x80 if c == 0xe0 && s[i + 1] < 0xa0 raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c) end u = ((c & 0x0f) << 12) | ((s[i + 1] & 0x3f) << 6) | (s[i + 2] & 0x3f) # surrogate chars if u >= 0xd800 && u <= 0xdfff raise DataFormatError, format("none-UTF-16 char detected (%04x)", u) end buf.concat(u >> 8) buf.concat(u & 0x00ff) i += 3 elsif (c & 0xf8) == 0xf0 && i + 3 < len && (s[i + 1] & 0xc0) == 0x80 && (s[i + 2] & 0xc0) == 0x80 && (s[i + 3] & 0xc0) == 0x80 if c == 0xf0 && s[i + 1] < 0x90 raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c) end u = ((c & 0x07) << 18) | ((s[i + 1] & 0x3f) << 12) | ((s[i + 2] & 0x3f) << 6) | (s[i + 3] & 0x3f) if u < 0x10000 buf.concat(u >> 8) buf.concat(u & 0x00ff) elsif u < 0x110000 high = ((u - 0x10000) >> 10) | 0xd800 low = (u & 0x03ff) | 0xdc00 buf.concat(high >> 8) buf.concat(high & 0x00ff) buf.concat(low >> 8) buf.concat(low & 0x00ff) else raise DataFormatError, format("none-UTF-16 char detected (%04x)", u) end i += 4 else raise DataFormatError, format("illegal UTF-8 sequence (%02x)", c) end end return buf end