PageRenderTime 266ms CodeModel.GetById 239ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rexml/encodings/CP-1252.rb

http://github.com/agross/netopenspace
Ruby | 103 lines | 91 code | 2 blank | 10 comment | 1 complexity | c83865cc0a0e1af52519e5a791d8149a MD5 | raw file
  1#
  2# This class was contributed by Mikko Tiihonen mikko DOT tiihonen AT hut DOT fi
  3#
  4module REXML
  5  module Encoding
  6  	register( "CP-1252" ) do |o|
  7  		class << o
  8  			alias encode encode_cp1252
  9			alias decode decode_cp1252
 10  		end
 11  	end
 12
 13    # Convert from UTF-8
 14    def encode_cp1252(content)
 15      array_utf8 = content.unpack('U*')
 16      array_enc = []
 17      array_utf8.each do |num|
 18        case num
 19          # shortcut first bunch basic characters
 20        when 0..0xFF; array_enc << num
 21          # characters added compared to iso-8859-1
 22        when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac
 23        when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a
 24        when 0x0192; array_enc << 0x83 # 0xc6 0x92
 25        when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e
 26        when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6
 27        when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0
 28        when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1
 29        when 0x02C6; array_enc << 0x88 # 0xcb 0x86
 30        when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0
 31        when 0x0160; array_enc << 0x8A # 0xc5 0xa0
 32        when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9
 33        when 0x0152; array_enc << 0x8C # 0xc5 0x92
 34        when 0x017D; array_enc << 0x8E # 0xc5 0xbd
 35        when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98
 36        when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99
 37        when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c
 38        when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d
 39        when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2
 40        when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93
 41        when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94
 42        when 0x02DC; array_enc << 0x98 # 0xcb 0x9c
 43        when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2
 44        when 0x0161; array_enc << 0x9A # 0xc5 0xa1
 45        when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba
 46        when 0x0152; array_enc << 0x9C # 0xc5 0x93
 47        when 0x017E; array_enc << 0x9E # 0xc5 0xbe
 48        when 0x0178; array_enc << 0x9F # 0xc5 0xb8
 49        else
 50          # all remaining basic characters can be used directly
 51          if num <= 0xFF
 52            array_enc << num
 53          else
 54            # Numeric entity (&#nnnn;); shard by  Stefan Scholl
 55            array_enc.concat "&\##{num};".unpack('C*')
 56          end
 57        end
 58      end
 59      array_enc.pack('C*')
 60    end
 61    
 62    # Convert to UTF-8
 63    def decode_cp1252(str)
 64      array_latin9 = str.unpack('C*')
 65      array_enc = []
 66      array_latin9.each do |num|
 67        case num
 68          # characters that added compared to iso-8859-1
 69        when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac
 70        when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a
 71        when 0x83; array_enc << 0x0192 # 0xc6 0x92
 72        when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e
 73        when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6
 74        when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0
 75        when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1
 76        when 0x88; array_enc << 0x02C6 # 0xcb 0x86
 77        when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0
 78        when 0x8A; array_enc << 0x0160 # 0xc5 0xa0
 79        when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9
 80        when 0x8C; array_enc << 0x0152 # 0xc5 0x92
 81        when 0x8E; array_enc << 0x017D # 0xc5 0xbd
 82        when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98
 83        when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99
 84        when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c
 85        when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d
 86        when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2
 87        when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93
 88        when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94
 89        when 0x98; array_enc << 0x02DC # 0xcb 0x9c
 90        when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2
 91        when 0x9A; array_enc << 0x0161 # 0xc5 0xa1
 92        when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba
 93        when 0x9C; array_enc << 0x0152 # 0xc5 0x93
 94        when 0x9E; array_enc << 0x017E # 0xc5 0xbe
 95        when 0x9F; array_enc << 0x0178 # 0xc5 0xb8
 96        else
 97          array_enc << num
 98        end
 99      end
100      array_enc.pack('U*')
101    end
102  end
103end