n.c.
[chise/ruby.git] / chise / iconv.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "iconv"
4
5 class Iconv
6   def self.iconv_to_utf8(from, str)
7     iconv = Iconv.new(from, "UTF-8")
8     out = ""
9     begin
10       out << iconv.iconv(str)
11     rescue Iconv::IllegalSequence => e
12       out << e.success
13       ch, str = e.failed.split(//u, 2)
14       out << if respond_to?(:unknown_unicode_handler)
15                u = ch.unpack("U").first
16                unknown_unicode_handler(u)
17              else
18                "?"
19              end
20       retry
21     end
22     out
23   end
24
25   def self.unknown_unicode_handler (u)
26     sprintf("&#x%04x;", u)
27   end
28
29   def self.iconv_to_from(to, from, str)
30     iconv = Iconv.new(to, from)
31     out = ""
32     begin
33       out << iconv.iconv(str)
34     rescue Iconv::IllegalSequence => e
35       out << e.success
36       ch, str = e.failed.split(//u, 2)
37       out << "?"
38       retry
39     rescue Iconv::InvalidCharacter => e
40       out << e.success
41       ch, str = e.failed.split(//u, 2)
42       out << "?"
43       retry
44     end
45     out
46   end
47 end
48
49 class String
50   def euctou8()  Iconv.iconv_to_from("UTF-8", "EUC-JP", self)           end
51   def u8toeuc()  Iconv.iconv_to_from("EUC-JP", "UTF-8", self)           end
52   def sjistou8() Iconv.iconv_to_from("UTF-8", "Shift_JIS", self)        end
53   def u8tosjis() Iconv.iconv_to_from("Shift_JIS", "UTF-8", self)        end
54   def jistou8()  Iconv.iconv_to_from("UTF-8", "ISO-2022-JP", self)      end
55
56   def u8tojis()
57     i = Iconv.new("ISO-2022-JP", "UTF-8")
58     i.iconv(self)+i.close
59   end
60
61   def u8tou16
62     Iconv.iconv_to_from("UTF-16", "UTF-8", self).sub(/\A\376\377/, "")
63   end
64
65   def u8tou32
66     Iconv.iconv_to_from("UTF-32", "UTF-8", self).sub(/\A\0\0\376\377/, "")
67   end
68
69   def u32tou8
70     Iconv.iconv_to_from("UTF-8", "UTF-32", self)
71   end
72
73   def u32tou16
74     Iconv.iconv_to_from("UTF-16", "UTF-32", self).sub(/\A\376\377/, "")
75   end
76
77   def u16toeuc()        Iconv.iconv_to_from("EUC-JP", "UTF-16", self)   end
78   def u16tosjis()       Iconv.iconv_to_from("Shift_JIS", "UTF-16", self) end
79
80 #  def u32to_i
81 #    return 0 if length == 0
82 #    s = self
83 #    return (s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3])
84 #  end
85
86 #  def u8to_i
87 #    u32 = self.u8tou32
88 #    u32.u32to_i
89 #  end
90 end
91
92 module CHISE
93 #  def i_tou32(n) # convert a integer to UTF-32 String
94 #    raise unless n.is_a?(Integer)
95 #    sprintf("%c%c%c%c", (n >> 24)&0xff, (n >> 16)&0xff, (n >> 8)&0xff, n&0xff)
96 #  end
97
98 #  def i_tou8(n) # convert a integer to UTF-8 String
99 #    u32 = CHISE.i_tou32(n)
100 #    u32.u32tou8
101 #  end
102 #  module_function :i_tou32, :i_tou8
103 end
104
105 class NuUconv
106   def self.u8tou4(s)    s.u8tou32;      end
107   def self.u4tou8(s)    s.u32tou8;      end
108   def self.u4tou16(s)   s.u32tou16;     end
109   def self.u16toeuc(s)  s.u16toeuc;     end
110   def self.u16tosjis(s) s.u16tosjis;    end
111 end