encoding-codepage 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +9 -1
  2. data/lib/encoding-codepage.rb +30 -2
  3. metadata +41 -24
data/README.md CHANGED
@@ -37,7 +37,7 @@ For seeing whether code-pages exist:
37
37
  Encoding.codepage?(37)
38
38
  # => nil
39
39
 
40
- Also makes all supported Code Pages available with a `CP` prefix:
40
+ It also makes all supported Code Pages available as object with a `CP` prefix:
41
41
 
42
42
  Encoding::CP28591
43
43
  # => #<Encoding:CP28591>
@@ -45,6 +45,14 @@ Also makes all supported Code Pages available with a `CP` prefix:
45
45
  Encoding.find("CP28591")
46
46
  # => #<Encoding:CP28591>
47
47
 
48
+ And adds a method to `Encoding` objects for discovering their Code Page Identifier:
49
+
50
+ Encoding::UTF_8.codepage_id
51
+ # => 65001
52
+
53
+ Encoding::ISO_8859_1.codepage_id
54
+ # => 28591
55
+
48
56
  Encodings
49
57
  =========
50
58
 
@@ -54,8 +54,10 @@ class Encoding
54
54
  number, original, comment = line.split("\t", 3)
55
55
  number = Integer(number, 10)
56
56
 
57
- if !codepage?(number) && exist?(original.upcase)
58
- Encoding.find(original.upcase).replicate "CP#{number}"
57
+ if encoding = exist?(original.upcase)
58
+ encoding.replicate "CP#{number}" unless codepage?(number)
59
+
60
+ CodePage.reverse_lookup[encoding] = codepage(number)
59
61
  end
60
62
  }
61
63
  end
@@ -63,8 +65,34 @@ class Encoding
63
65
  def codepage_file
64
66
  File.join(File.dirname(__FILE__), "codepages.tsv")
65
67
  end
68
+
69
+ # A Hash from <non-codepage-encoding> to <codepage-encoding> to facilitate running
70
+ # codepage_id in the case where Ruby has both the CodePage encoding and its alias, but
71
+ # thinks they are different Encodings.
72
+ #
73
+ def self.reverse_lookup
74
+ @reverse_lookup ||= {}
75
+ end
76
+ end
77
+
78
+ module CodePageMethods
79
+ # Find the code-page id that corresponds to this encoding.
80
+ #
81
+ # @return Integer The Code Page Identifier
82
+ # @raise ArgumentError There is no Code Page Identifier for that Encoding.
83
+ #
84
+ def codepage_id
85
+ if names.detect{ |x| x =~ /\ACP([0-9]+)\z/ }
86
+ Integer($1, 10)
87
+ elsif codepage_encoding = CodePage.reverse_lookup[self]
88
+ codepage_encoding.codepage_id
89
+ else
90
+ raise ArgumentError, "No Code Page Idenfier found for: #{self}"
91
+ end
92
+ end
66
93
  end
67
94
 
68
95
  extend CodePage
96
+ include CodePageMethods
69
97
  load_codepages!
70
98
  end
metadata CHANGED
@@ -1,53 +1,70 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: encoding-codepage
3
- version: !ruby/object:Gem::Version
4
- version: '0.1'
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Conrad Irwin
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-03-30 00:00:00.000000000Z
16
+
17
+ date: 2012-04-02 00:00:00 -07:00
18
+ default_executable:
13
19
  dependencies: []
14
- description: Provides aliases for Encodings that have Code Page Identifiers to make
15
- it easier to interface with Microsoft APIs that use Code Page Identifiers to describe
16
- content
20
+
21
+ description: Provides aliases for Encodings that have Code Page Identifiers to make it easier to interface with Microsoft APIs that use Code Page Identifiers to describe content
17
22
  email: conrad.irwin@gmail.com
18
23
  executables: []
24
+
19
25
  extensions: []
26
+
20
27
  extra_rdoc_files: []
21
- files:
28
+
29
+ files:
22
30
  - lib/encoding-codepage.rb
23
31
  - lib/codepages.tsv
24
32
  - README.md
25
33
  - LICENSE.MIT
26
34
  - LICENSE.MLPL
35
+ has_rdoc: true
27
36
  homepage: https://github.com/ConradIrwin/encoding-codepage
28
- licenses:
37
+ licenses:
29
38
  - MIT, Microsoft Limited Public License
30
39
  post_install_message:
31
40
  rdoc_options: []
32
- require_paths:
41
+
42
+ require_paths:
33
43
  - lib
34
- required_ruby_version: !ruby/object:Gem::Requirement
44
+ required_ruby_version: !ruby/object:Gem::Requirement
35
45
  none: false
36
- requirements:
37
- - - ! '>='
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
54
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
46
62
  requirements: []
63
+
47
64
  rubyforge_project:
48
- rubygems_version: 1.8.10
65
+ rubygems_version: 1.3.7
49
66
  signing_key:
50
67
  specification_version: 3
51
68
  summary: Allow looking up Encodings by their Code Page Identifier
52
69
  test_files: []
53
- has_rdoc:
70
+