string_utf8 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -0
- data/lib/string/utf8.rb +42 -0
- data/lib/string_utf8_version.rb +3 -0
- data/string_utf8.rdoc +3 -0
- metadata +56 -0
data/README.rdoc
ADDED
data/lib/string/utf8.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# This monkey patch add ability of turning stirng's
|
4
|
+
# encoding into utf8
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# require 'string/utf8'
|
9
|
+
# p "\xD6\xD0\xCE\xC4".utf8!
|
10
|
+
# => "中文"
|
11
|
+
#
|
12
|
+
class String
|
13
|
+
|
14
|
+
ENCODINGS = %w(ascii-8bit utf-8 ucs-bom shift-jis gb18030 gbk gb2312 cp936)
|
15
|
+
|
16
|
+
# Try to turn a string's encoding to utf8
|
17
|
+
#
|
18
|
+
# Example:
|
19
|
+
# >> "中文"
|
20
|
+
# # => "中文"
|
21
|
+
#
|
22
|
+
# >> "中文".encode('gb2312')
|
23
|
+
# # => "\x{D6D0}\x{CEC4}"
|
24
|
+
#
|
25
|
+
# >> "中文".encode('gb2312').utf8!
|
26
|
+
# # => "中文"
|
27
|
+
#
|
28
|
+
def utf8!
|
29
|
+
ENCODINGS.any? do |c|
|
30
|
+
begin
|
31
|
+
self.encode!('utf-8', c).force_encoding('utf-8')
|
32
|
+
if self.valid_encoding?
|
33
|
+
$enc = c
|
34
|
+
return self
|
35
|
+
end
|
36
|
+
rescue
|
37
|
+
end
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/string_utf8.rdoc
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_utf8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- qhwa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-05 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: qhwa@163.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.rdoc
|
20
|
+
- string_utf8.rdoc
|
21
|
+
files:
|
22
|
+
- lib/string_utf8_version.rb
|
23
|
+
- lib/string/utf8.rb
|
24
|
+
- README.rdoc
|
25
|
+
- string_utf8.rdoc
|
26
|
+
homepage: http://q.pnq.cc
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options:
|
30
|
+
- --title
|
31
|
+
- string_utf8
|
32
|
+
- --main
|
33
|
+
- README.rdoc
|
34
|
+
- -ri
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.17
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Convert strings encoding to utf8
|
56
|
+
test_files: []
|