PageRenderTime 56ms CodeModel.GetById 43ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rexml/encodings/UTF-16.rb

http://github.com/agross/netopenspace
Ruby | 35 lines | 33 code | 2 blank | 0 comment | 2 complexity | cf582a98f8e6778bb000f023ded02086 MD5 | raw file
 1module REXML
 2  module Encoding
 3    def encode_utf16 content
 4      array_utf8 = content.unpack("U*")
 5      array_enc = []
 6      array_utf8.each do |num|
 7        if ((num>>16) > 0)
 8          array_enc << 0
 9          array_enc << ??
10        else
11          array_enc << (num >> 8)
12          array_enc << (num & 0xFF)
13        end
14      end
15      array_enc.pack('C*')
16    end
17
18    def decode_utf16(str)
19      str = str[2..-1] if /^\376\377/n =~ str
20      array_enc=str.unpack('C*')
21      array_utf8 = []
22      0.step(array_enc.size-1, 2){|i| 
23        array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
24      }
25      array_utf8.pack('U*')
26    end
27
28    register(UTF_16) do |obj|
29      class << obj
30        alias decode decode_utf16
31        alias encode encode_utf16
32      end
33    end
34  end
35end