charwidth 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.cleancode +4 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +20 -0
- data/README.md +37 -0
- data/Rakefile +51 -0
- data/charwidth.gemspec +63 -0
- data/lib/charwidth.rb +98 -0
- data/lib/charwidth/characters.rb +80 -0
- data/lib/charwidth/string.rb +16 -0
- data/lib/charwidth/version.rb +9 -0
- data/spec/charwidth_spec.rb +44 -0
- data/spec/spec_helper.rb +12 -0
- metadata +130 -0
data/.cleancode
ADDED
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "simplecov"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
multi_json (1.0.4)
|
11
|
+
rake (0.9.2.2)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
simplecov (0.5.4)
|
21
|
+
multi_json (~> 1.0.3)
|
22
|
+
simplecov-html (~> 0.5.3)
|
23
|
+
simplecov-html (0.5.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.0.0)
|
30
|
+
jeweler (~> 1.6.4)
|
31
|
+
rspec (~> 2.3.0)
|
32
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 labocho
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# charwidth
|
2
|
+
|
3
|
+
Normalize Unicode fullwidth / halfwidth (zenkaku / hankaku) characters.
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
gem install charwidth
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
11
|
+
require "charwidth"
|
12
|
+
Charwidth.normalize("Hello, World!") # => "Hello, World!"
|
13
|
+
Charwidth.normalize!("Hello, World!") # destructive
|
14
|
+
Charwidth.normalize("「ハローワールド」") # => "「ハローワールド」"
|
15
|
+
|
16
|
+
Or extend String class.
|
17
|
+
|
18
|
+
require "charwidth/string"
|
19
|
+
"Hello, World!".normalize_charwidth # => "Hello, World!"
|
20
|
+
"Hello, World!".normalize_charwidth! # => destructive
|
21
|
+
"「ハローワールド」".normalize_charwidth # => "「ハローワールド」"
|
22
|
+
|
23
|
+
# Contributing to charwidth
|
24
|
+
|
25
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
26
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
27
|
+
* Fork the project
|
28
|
+
* Start a feature/bugfix branch
|
29
|
+
* Commit and push until you are happy with your contribution
|
30
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
31
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
32
|
+
|
33
|
+
# Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2012 labocho. See LICENSE.txt for
|
36
|
+
further details.
|
37
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
require "#{File.dirname(__FILE__)}/lib/charwidth/version"
|
16
|
+
Jeweler::Tasks.new do |gem|
|
17
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
18
|
+
gem.name = "charwidth"
|
19
|
+
gem.version = Charwidth::Version::STRING
|
20
|
+
gem.homepage = "http://github.com/labocho/charwidth"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.summary = %Q{Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters}
|
23
|
+
gem.description = %Q{Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters}
|
24
|
+
gem.email = "labocho@penguinlab.jp"
|
25
|
+
gem.authors = ["labocho"]
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rspec/core'
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
33
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
34
|
+
end
|
35
|
+
|
36
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
37
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
38
|
+
spec.rcov = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :spec
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "charwidth #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/charwidth.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "charwidth"
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["labocho"]
|
12
|
+
s.date = "2012-08-21"
|
13
|
+
s.description = "Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters"
|
14
|
+
s.email = "labocho@penguinlab.jp"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".cleancode",
|
21
|
+
".document",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"charwidth.gemspec",
|
29
|
+
"lib/charwidth.rb",
|
30
|
+
"lib/charwidth/characters.rb",
|
31
|
+
"lib/charwidth/string.rb",
|
32
|
+
"lib/charwidth/version.rb",
|
33
|
+
"spec/charwidth_spec.rb",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/labocho/charwidth"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = "1.8.23"
|
40
|
+
s.summary = "Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters"
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
49
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/lib/charwidth.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module Charwidth
|
2
|
+
autoload :String, "charwidth/string"
|
3
|
+
autoload :Characters, "charwidth/characters"
|
4
|
+
autoload :Version, "charwidth/version"
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Normalize Unicode fullwidth / halfwidth (zenkaku / hankaku) characters
|
8
|
+
# options: {
|
9
|
+
# only: [:ascii, :white_parenthesis, :cjk_punctuation, :katakana, :space],
|
10
|
+
# except: [:ascii, :white_parenthesis, :cjk_punctuation, :katakana, :space]
|
11
|
+
# }
|
12
|
+
def normalize(string, options = {})
|
13
|
+
normalize!(string.dup, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Normalize Unicode fullwidth / halfwidth (zenkaku / hankaku) characters (destructive)
|
17
|
+
def normalize!(string, options = {})
|
18
|
+
unify_voiced_katakana!(string)
|
19
|
+
normalize_charwidth!(string, options)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
TYPES = [
|
24
|
+
:ascii, :white_parenthesis, :cjk_punctuation, :katakana, :hangul,
|
25
|
+
:latin_1_punctuation_and_symbols, :mathematical_symbols, :space
|
26
|
+
].freeze
|
27
|
+
def normalize_charwidth!(src, options = {})
|
28
|
+
types = TYPES.dup
|
29
|
+
|
30
|
+
# Check options
|
31
|
+
unless (unexpected_options = options.keys - [:only, :except]).empty?
|
32
|
+
raise "Unexpected normalize option(s): #{unexpected_options}"
|
33
|
+
end
|
34
|
+
|
35
|
+
if options[:only]
|
36
|
+
unless (unexpected_types = options[:only] - TYPES).empty?
|
37
|
+
raise "Unexpected normalize type(s): #{unexpected_types.inspect}"
|
38
|
+
end
|
39
|
+
types = types & options[:only]
|
40
|
+
end
|
41
|
+
|
42
|
+
if options[:expect]
|
43
|
+
unless (unexpected_types = options[:expected] - TYPES).empty?
|
44
|
+
raise "Unexpected normalize type(s): #{t.inspect}"
|
45
|
+
end
|
46
|
+
types = types - options[:expect]
|
47
|
+
end
|
48
|
+
|
49
|
+
before, after = "", ""
|
50
|
+
types.each do |type|
|
51
|
+
case type
|
52
|
+
when :ascii
|
53
|
+
before << Characters::FULLWIDTH_ASCII_VARIANTS
|
54
|
+
after << Characters::ASCII_PUNCTUATION_AND_SYMBOLS
|
55
|
+
when :white_parenthesis
|
56
|
+
before << Characters::FULLWIDTH_BRACKETS
|
57
|
+
after << Characters::WHITE_PARENTHESIS
|
58
|
+
when :cjk_punctuation
|
59
|
+
before << Characters::HALFWIDTH_CJK_PUCTUATION
|
60
|
+
after << Characters::CJK_SYMBOLS_AND_PUNCTUATION
|
61
|
+
when :katakana
|
62
|
+
before << Characters::HALFWIDTH_KATAKANA_VARIANTS
|
63
|
+
after << Characters::KATAKANA
|
64
|
+
when :hangul
|
65
|
+
before << Characters::HALFWIDTH_HANGUL_VARIANTS
|
66
|
+
after << Characters::HANGUL
|
67
|
+
when :latin_1_punctuation_and_symbols
|
68
|
+
before << Characters::FULLWIDTH_SYMBOL_VARIANTS
|
69
|
+
after << Characters::LATIN_1_PUNCTUATION_AND_SYMBOLS
|
70
|
+
when :mathematical_symbols
|
71
|
+
before << Characters::HALFWIDTH_SYMBOL_VARIANTS
|
72
|
+
after << Characters::MATHEMATICAL_SYMBOLS
|
73
|
+
when :space
|
74
|
+
before << Characters::IDEOGRAPHIC_SPACE
|
75
|
+
after << Characters::SPACE
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
after.sub!('\\', '\\\\\\\\') # escape for tr
|
80
|
+
src.tr!(before, after) || src
|
81
|
+
end
|
82
|
+
|
83
|
+
# Unify halfwidth (semi) voiced katakana to one fullwidth voiced katakana
|
84
|
+
def unify_voiced_katakana!(src)
|
85
|
+
halfwidth =
|
86
|
+
Characters::HALFWIDTH_VOICED_KATAKANA +
|
87
|
+
Characters::HALFWIDTH_SEMI_VOICED_KATAKANA
|
88
|
+
fullwidth =
|
89
|
+
Characters::VOICED_KATAKANA +
|
90
|
+
Characters::SEMI_VOICED_KATAKANA
|
91
|
+
halfwidth.zip(fullwidth).inject(src) do |str, (h, f)|
|
92
|
+
str.gsub!(h, f) || str
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
extend ClassMethods
|
98
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Charwidth
|
2
|
+
module Characters
|
3
|
+
# http://www.unicode.org/charts/PDF/UFF00.pdf
|
4
|
+
FULLWIDTH_ASCII_VARIANTS = (0xff01..0xff5e).to_a.pack("U*").freeze
|
5
|
+
FULLWIDTH_BRACKETS = [0xff5f, 0xff60].pack("U*").freeze
|
6
|
+
HALFWIDTH_CJK_PUCTUATION = (0xff61..0xff64).to_a.pack("U*").freeze
|
7
|
+
HALFWIDTH_KATAKANA_VARIANTS = (0xff65..0xff9f).to_a.pack("U*").freeze
|
8
|
+
HALFWIDTH_HANGUL_VARIANTS = [
|
9
|
+
(0xffa0..0xffbe).to_a,
|
10
|
+
(0xffc2..0xffc7).to_a,
|
11
|
+
(0xffca..0xffcf).to_a,
|
12
|
+
(0xffd2..0xffd7).to_a,
|
13
|
+
(0xffda..0xffdc).to_a
|
14
|
+
].flatten.pack("U*").freeze
|
15
|
+
FULLWIDTH_SYMBOL_VARIANTS = (0xffe0..0xffe6).to_a.pack("U*").freeze
|
16
|
+
HALFWIDTH_SYMBOL_VARIANTS = (0xffe8..0xffee).to_a.pack("U*").freeze
|
17
|
+
IDEOGRAPHIC_SPACE = "\u3000".freeze
|
18
|
+
|
19
|
+
ASCII_PUNCTUATION_AND_SYMBOLS = (0x0021..0x007e).to_a.pack("U*").freeze
|
20
|
+
WHITE_PARENTHESIS = (0x2985..0x2986).to_a.pack("U*").freeze
|
21
|
+
CJK_SYMBOLS_AND_PUNCTUATION = [0x3002, 0x300c, 0x300d, 0x3001].pack("U*").freeze
|
22
|
+
KATAKANA = [
|
23
|
+
0x30fb, # KATAKANA MIDDLE DOT
|
24
|
+
0x30f2, # KATAKANA LETTER WO
|
25
|
+
0x30a1.step(0x30a9, 2).to_a, # KATAKANA LETTER SMALL A to O
|
26
|
+
0x30e3, 0x30e5, 0x30e7, 0x30c3, # KATAKANA LETTER SMALL YA, YU, YO, TU
|
27
|
+
0x30fc, # KATAKANA-HIRAGANA PROLONGED SOUND MARK
|
28
|
+
0x30a2.step(0x30aa, 2).to_a, # KATAKANA LETTER A to O
|
29
|
+
0x30ab.step(0x30bd, 2).to_a, # KATAKANA LETTER KA to SO
|
30
|
+
0x30bf, 0x30c1, 0x30c4, 0x30c6, 0x30c8, # KATAKANA LETTER TA to TO
|
31
|
+
(0x30ca..0x30ce).to_a, # KATAKANA LETTER NA to NO
|
32
|
+
0x30cf.step(0x30db, 3).to_a, # KATAKANA LETTER HA to HO
|
33
|
+
(0x30de..0x30e2).to_a, # KATAKANA LETTER MA to MO
|
34
|
+
0x30e4, 0x30e6, 0x30e8, # KATAKANA LETTER YA, YU, YO
|
35
|
+
(0x30e9..0x30ed).to_a, # KATAKANA LETTER RA to RO
|
36
|
+
0x30ef, 0x30f3, # KATAKANA LETTER WA, N
|
37
|
+
0x3099, # KATAKANA VOICED SOUND MARK
|
38
|
+
0x309a, # KATAKANA SEMI-VOICED SOUND MARK
|
39
|
+
].flatten.pack("U*").freeze
|
40
|
+
HANGUL = [
|
41
|
+
0x3164, # HANGUL FILLER
|
42
|
+
(0x3131..0x3163).to_a # HANGUL LETTER KIYEOK to I
|
43
|
+
].flatten.pack("U*").freeze
|
44
|
+
LATIN_1_PUNCTUATION_AND_SYMBOLS = [
|
45
|
+
0x00a2, # CENT SIGN
|
46
|
+
0x00a3, # POUND SIGN
|
47
|
+
0x00ac, # NOT SIGN
|
48
|
+
0x00af, # MACRON
|
49
|
+
0x00a6, # BROKEN BAR
|
50
|
+
0x00a5, # YEN SIGN
|
51
|
+
0x20a9 # WON SIGN
|
52
|
+
].pack("U*").freeze
|
53
|
+
MATHEMATICAL_SYMBOLS = [
|
54
|
+
0x2502, # BOX DRAWINGS LIGHT VERTICAL
|
55
|
+
(0x2190..0x2193).to_a, # LEFTWARDS, UPWARDS, RIGHTWARDS, DOWNWARDS ARROW
|
56
|
+
0x25a0, # BLACK SQUARE
|
57
|
+
0x25cb # WHITE CIRCLE
|
58
|
+
].flatten.pack("U*").freeze
|
59
|
+
SPACE = "\u0020".freeze
|
60
|
+
|
61
|
+
HALFWIDTH_KATAKANA_VOICED_SOUND_MARK = "\uff9e"
|
62
|
+
HALFWIDTH_VOICED_KATAKANA = [
|
63
|
+
(0xff76..0xff84).to_a, # HALFWIDTH KATAKANA LETTER KA to TO
|
64
|
+
(0xff8a..0xff8e).to_a, # HALFWIDTH KATAKANA LETTER HA to HO
|
65
|
+
0xff73 # HALFWIDTH KATAKANA LETTER U
|
66
|
+
].flatten.map{|k| ([k].pack("U") + HALFWIDTH_KATAKANA_VOICED_SOUND_MARK).freeze }.freeze
|
67
|
+
VOICED_KATAKANA = [
|
68
|
+
0x30ac.step(0x30be, 2).to_a, # KATAKANA LETTER GA to ZO
|
69
|
+
[0x30c0, 0x30c2, 0x30c5, 0x30c7, 0x30c9], # KATAKANA LETTER DA to DO
|
70
|
+
0x30d0.step(0x30dc, 3).to_a, # KATAKANA LETTER BA to BO
|
71
|
+
0x30f4 # KATAKANA LETTER VU
|
72
|
+
].flatten.map{|c| [c].pack("U").freeze }.freeze
|
73
|
+
|
74
|
+
HALFWIDTH_KATAKANA_SEMI_VOICED_SOUND_MARK = "\uff9f"
|
75
|
+
HALFWIDTH_SEMI_VOICED_KATAKANA = [
|
76
|
+
(0xff8a..0xff8e).to_a # HALFWIDTH KATAKANA LETTER HA to HO
|
77
|
+
].flatten.map{|k| ([k].pack("U") + HALFWIDTH_KATAKANA_SEMI_VOICED_SOUND_MARK).freeze }.freeze
|
78
|
+
SEMI_VOICED_KATAKANA = 0x30d1.step(0x30dd, 3).map{|c| [c].pack("U").freeze }.flatten.freeze # KATAKANA LETTER PA to PO
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "charwidth"
|
2
|
+
module Charwidth
|
3
|
+
module String
|
4
|
+
def normalize_charwidth(options = {})
|
5
|
+
dup.normalize_charwidth!(options)
|
6
|
+
end
|
7
|
+
|
8
|
+
def normalize_charwidth!(options = {})
|
9
|
+
Charwidth.normalize!(self, options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
::String.class_eval do
|
15
|
+
include ::Charwidth::String
|
16
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "Charwidth" do
|
5
|
+
it "should convert full-width alphabet to half-width" do
|
6
|
+
Charwidth.normalize("ABCabc").should == "ABCabc"
|
7
|
+
end
|
8
|
+
it "should convert full-width number to half-width" do
|
9
|
+
Charwidth.normalize("123").should == "123"
|
10
|
+
end
|
11
|
+
it "should convert full-width ASCII symbol before numbers to half-width" do
|
12
|
+
Charwidth.normalize("!"#").should == "!\"#"
|
13
|
+
end
|
14
|
+
it "should convert full-width ASCII symbol between numbers and upper-case to half-width" do
|
15
|
+
Charwidth.normalize(":;").should == ":;"
|
16
|
+
end
|
17
|
+
it "should convert full-width ASCII symbol between upper-case and lower-case to half-width" do
|
18
|
+
Charwidth.normalize("[\]").should == "[\\]"
|
19
|
+
end
|
20
|
+
it "should convert full-width ASCII symbol after lower-case to half-width" do
|
21
|
+
Charwidth.normalize("{|}").should == "{|}"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should convert half-width CJK punctuation to full-width" do
|
25
|
+
Charwidth.normalize("、。「」").should == "、。「」"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should convert half-width katakana to full-width" do
|
29
|
+
Charwidth.normalize("アカサタナハマヤラワヲンァャッー・").should == "アカサタナハマヤラワヲンァャッー・"
|
30
|
+
Charwidth.normalize("タチツテトナニヌネノ").should == "タチツテトナニヌネノ"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should unify half-width (semi) voiced katakana with dakuon to full-width" do
|
34
|
+
Charwidth.normalize("ガザダバパ").should == "ガザダバパ"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should convert half-width hangul to full-width" do
|
38
|
+
Charwidth.normalize("アカサタナハマヤラワヲンァャッー・").should == "アカサタナハマヤラワヲンァャッー・"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should convert IDIOGRAPHIC-SPACE to SPACE" do
|
42
|
+
Charwidth.normalize("\u3000").should == " "
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'charwidth'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: charwidth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- labocho
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.3.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.3.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.0.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.6.4
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.6.4
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: simplecov
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku)
|
79
|
+
characters
|
80
|
+
email: labocho@penguinlab.jp
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files:
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
files:
|
87
|
+
- .cleancode
|
88
|
+
- .document
|
89
|
+
- .rspec
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- charwidth.gemspec
|
96
|
+
- lib/charwidth.rb
|
97
|
+
- lib/charwidth/characters.rb
|
98
|
+
- lib/charwidth/string.rb
|
99
|
+
- lib/charwidth/version.rb
|
100
|
+
- spec/charwidth_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: http://github.com/labocho/charwidth
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
hash: -548817087615038840
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.23
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Normalize Japanese / Korean fullwidth (zenkaku) and halfwidth (hankaku) characters
|
130
|
+
test_files: []
|