method

u8tou16

Importance_0
Ruby latest stable (v2_5_5) - 0 notes - Class: Net::IMAP

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v1_8_7_330) is shown here.

u8tou16(s) private

No documentation

This method has no description. You can help the Ruby community by adding new notes.

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
Register or log in to add new notes.