-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf.rb
More file actions
executable file
·79 lines (74 loc) · 2.11 KB
/
utf.rb
File metadata and controls
executable file
·79 lines (74 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby -w
# -*- coding: UTF-8 -*-
# ruby utf8 gb2312 gbk gb18030 转换库
require 'rubygems'
#p RUBY_VERSION[0,3]
if RUBY_VERSION > '1.9'
Ig = ''
#Ig = '//IGNORE'
if RUBY_VERSION >= '1.9.2'
$ec1 = Encoding::Converter.new("UTF-16lE", "UTF-8", :universal_newline => true)
$ec2 = Encoding::Converter.new("UTF-8","GB18030", :universal_newline => true)
$ec3 = Encoding::Converter.new("UTF-16lE", "GB18030", :universal_newline => true)
$ecutf8 = Encoding::Converter.new("GBK","UTF-8", :universal_newline => true)
else
require 'iconv'
end
else
require 'iconv'
Ig = '//IGNORE'
end
class String
def code_a2b(a,b)
return self if a =~ /#{b}/i
if RUBY_VERSION > '1.9' and defined? Encoding::Converter
tmp = Encoding::Converter.new(a,b, :universal_newline => true)
tmp.convert self rescue self
else
Iconv.conv("#{b}//IGNORE","#{a}//IGNORE",self)
end
end
#自动判断
def togb2312
return $ec2.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("CP20936#{Ig}","UTF-8#{Ig}",self)
end
def togbk
if RUBY_VERSION >= '1.9.2'
$ec2.convert self rescue self
else
Iconv.conv("GB18030#{Ig}","UTF-8#{Ig}",self)
end
end
def togb
if RUBY_VERSION >= '1.9.2'
$ec2.convert self rescue self
else
Iconv.conv("GB18030#{Ig}","UTF-8#{Ig}",self)
end
end
alias to_gb togb
def utf8_to_gb
return $ec2.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("GB18030#{Ig}","UTF-8#{Ig}",self)
end
def gb_to_utf8
return $ecutf8.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("UTF-8#{Ig}","GB18030#{Ig}",self)
end
def to_utf8
return $ecutf8.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("UTF-8#{Ig}","GB18030#{Ig}",self)
rescue
self
end
def uni_to_gb
return $ec3.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("GB18030#{Ig}","UTF-16#{Ig}",self)
end
def uni_to_utf8
return $ec1.convert(self) if RUBY_VERSION >= '1.9.2'
Iconv.conv("UTF-8#{Ig}","UTF-16",self)
end
end
#print '中文 '.togb