unicode-display_width 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/display_width"
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ module Unicode; end
3
+
4
+ module Unicode::DisplayWidth
5
+ VERSION = '0.1.0'
6
+
7
+ DATA_DIR = File.join(File.dirname(__FILE__), '../../data/')
8
+ FIELDS = [:codepoint, :width, :name]
9
+ INDEX_FILE = DATA_DIR + 'EastAsianWidth.index'
10
+ DATA_FILE = DATA_DIR + 'EastAsianWidth.txt'
11
+
12
+ def self.data
13
+ @@data ||= File.open DATA_FILE
14
+ end
15
+
16
+ def self.offsets
17
+ @@offsets ||= Marshal.load File.respond_to?(:binread) ? File.binread(INDEX_FILE) : File.read(INDEX_FILE)
18
+ end
19
+
20
+ class Codepoint < Struct.new(*FIELDS)
21
+ def initialize(*args)
22
+ super
23
+ self.codepoint = self.codepoint.to_i(16) if self.codepoint && self.codepoint !~ /\.\./
24
+ # TODO cleaner # FIXME ranges
25
+ end
26
+
27
+ def self.from_line(line)
28
+ line =~ /(.*);(.*) # (.*)$/
29
+ raise 'BUG' unless line
30
+ new $1,$2,$3
31
+ end
32
+ end
33
+
34
+ def self.line(n)
35
+ data.rewind
36
+ offset = offsets[n] or raise ArgumentError
37
+ data.seek offset
38
+ data.readline.chomp
39
+ end
40
+
41
+ def self.codepoint(n)
42
+ Codepoint.from_line line(n)
43
+ end
44
+
45
+ def self.valid_index?
46
+ !!offsets rescue false
47
+ end
48
+
49
+ def self.build_index
50
+ data.rewind
51
+ offsets = {}
52
+ dir = File.dirname INDEX_FILE
53
+ Dir.mkdir(dir) unless Dir.exists?(dir)
54
+ data.lines.map do |line|
55
+ offsets[Codepoint.from_line(line).codepoint] = data.pos - line.size
56
+ end
57
+ File.open(INDEX_FILE, 'wb') { |f| Marshal.dump(offsets, f) }
58
+ end
59
+ end
60
+
61
+ class String
62
+ def display_width(ambiguous=1)
63
+ #codepoints.inject(0){ |a,c|
64
+ unpack('U*').inject(0){ |a,c|
65
+ width = case Unicode::DisplayWidth.codepoint(c).width
66
+ when *%w[F W]
67
+ 2
68
+ when *%w[N Na H]
69
+ 1
70
+ when *%w[A] # TODO
71
+ ambiguous
72
+ else
73
+ 1
74
+ end
75
+ a + width
76
+ }
77
+ end
78
+ alias display_size display_width
79
+ alias display_length display_width
80
+ end
81
+
82
+ # J-_-L
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicode-display_width
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jan Lelis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-03 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem adds String#display_size to get the display size of a string using EastAsianWidth.txt.
23
+ email: mail@janlelis.de
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - LICENSE.txt
31
+ files:
32
+ - lib/unicode/display_size.rb
33
+ - lib/unicode/display_width.rb
34
+ - LICENSE.txt
35
+ - README.rdoc
36
+ - data/EastAsianWidth.txt
37
+ - data/EastAsianWidth.index
38
+ - Rakefile
39
+ - .gemspec
40
+ has_rdoc: true
41
+ homepage: http://github.com/janlelis/unicode-display_size
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 23
64
+ segments:
65
+ - 1
66
+ - 3
67
+ - 6
68
+ version: 1.3.6
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Support for east_asian_width String sizes.
76
+ test_files: []
77
+