wavedash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5861f53c58547e735f4b8c2ecfd69e53e67487e9
4
+ data.tar.gz: c2ccc8f495977b929f221d34a9ad52e3200c53fa
5
+ SHA512:
6
+ metadata.gz: f36db7862d0f9e6d393870d785f3f3ef9ff1ad2f090b17c04761d8301db206778aabcd854d79bf564f008b25d526a28f5e2a84fd60e6e91c673c1879e7cf1fa9
7
+ data.tar.gz: 521b9230335c8370539af0e311387029de89bb8d0e0ba1745bc049fec030a381a90a0ba7648cdcdea2f931263da090f0752107613edfb6addbb1536f64fff665
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wavedash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Takatoshi Ono
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Wavedash [![Build Status](https://travis-ci.org/takatoshiono/wavedash.svg?branch=master)](https://travis-ci.org/takatoshiono/wavedash)
2
+
3
+ Normalize unencodable characters that raise `Encoding::UndefinedConversionError` exception in `String#encode`.
4
+
5
+ ### Support encoding
6
+
7
+ * eucjp-ms
8
+ * euc-jp
9
+ * cp932
10
+ * shift_jis
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'wavedash'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install wavedash
27
+
28
+ ## Usage
29
+
30
+ ### destination_encoding
31
+
32
+ First af all, configure destination encoding that your application needs.
33
+
34
+ ```ruby
35
+ require 'wavedash'
36
+
37
+ Wavedash.destination_encoding = 'eucjp-ms'
38
+ ```
39
+
40
+ ### #normalize
41
+
42
+ Normalize characters like "WAVE DASH (U+301C)"
43
+
44
+ ```ruby
45
+ str = "こんにちは\u{301C}" # => "こんにちは〜"
46
+ str.encode('eucjp-ms') # => Encoding::UndefinedConversionError: U+301C from UTF-8 to eucJP-ms
47
+
48
+ normalized = Wavedash.normalize(str) # => "こんにちは~"
49
+ normalized.encode('eucjp-ms') # => "\x{A4B3}\x{A4F3}\x{A4CB}\x{A4C1}\x{A4CF}\x{A1C1}" ("こんにちは~")
50
+ ```
51
+
52
+ ### #invalid?
53
+
54
+ Detect unencodable characters
55
+
56
+ ```ruby
57
+ str = "こんにちは\u{301C}" # => "こんにちは〜"
58
+ Wavedash.invalid?(str) # => true
59
+ ```
60
+
61
+ ### Thought of Wavedash
62
+
63
+ Character code conversion is required when interact between softwares that treet different character code. For example, it is a situation such as the following.
64
+
65
+ * A Web application written in UTF-8 using a database saved in EUC-JP
66
+ * Exchanging data files(csv,tsv,..) between different systems
67
+
68
+ In Ruby, You can convert a character code using `String#encode`, but some characters cannot. `Encoding::UndefinedConversionError` raises when a character is undefined in the destination encoding. But `String#encode` has options. You can specify `:undef => :replace` then replace the undefined characters with the replacement character.
69
+
70
+ `Wavedash` is similar to `String#encode` with `:undef => :replace`, but it does more aggressive character conversion.
71
+
72
+ Despite some characters have resembling shape, character code point is different each other. For example, when you convert characters to EUCJP-MS from UTF-8, can convert "~" (FULLWIDTH TILDE U+FF5E) but cannot convert "〜" (WAVE DASH U+301C). The opposite will occur when you convert to EUC-JP.
73
+
74
+ UNICODE | EUC-JP | EUCJP-MS |
75
+ --------|--------|---------
76
+ "〜" U+301C WAVE-DASH | 0xA1C1 | Encoding::UndefinedConversionError
77
+ "~" U+FF5E FULLWIDTH TILDE | Encoding::UndefinedConversionError | 0xA1C1
78
+
79
+ In Web applications, it depends on the client environment that the input character as "〜" is U+301C or U+FF5E. This cannot select by the application. What you can do with the application is only to determine the handling of the unencodable characters.
80
+
81
+ `Wavedash` offers the option that to convert unencodable characters to resembling characters.
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (git checkout -b my-new-feature)
93
+ 3. Commit your changes (git commit -am 'Add some feature')
94
+ 4. Push to the branch (git push origin my-new-feature)
95
+ 5. Create new Pull Request
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
100
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wavedash"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/wavedash.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "wavedash/version"
2
+
3
+ module Wavedash
4
+ @@destination_encoding = nil
5
+
6
+ CHARACTER_CODE_MAPPING = {
7
+ 'eucjp-ms' => {
8
+ "\u{301C}" => "\u{FF5E}", # 'WAVE DASH' => 'FULLWIDTH TILDE'
9
+ "\u{2212}" => "\u{FF0D}", # 'MINUS SIGN' => 'FULLWIDTH HYPHEN-MINUS'
10
+ "\u{2016}" => "\u{2225}", # 'DOUBLE VERTICAL LINE' => 'PARALLEL TO'
11
+ "\u{2014}" => "\u{2015}", # 'EM DASH' => 'HORIZONTAL BAR'
12
+ "\u{00A2}" => "\u{FFE0}", # 'CENT SIGN' => 'FULLWIDTH CENT SIGN'
13
+ "\u{00A3}" => "\u{FFE1}", # 'POUND SIGN' => 'FULLWIDTH POUND SIGN'
14
+ "\u{00AC}" => "\u{FFE2}", # 'NOT SIGN' => 'FULLWIDTH NOT SIGN'
15
+ },
16
+ 'euc-jp' => {
17
+ "\u{FF5E}" => "\u{301C}", # 'FULLWIDTH TILDE' => 'WAVE DASH'
18
+ "\u{FF0D}" => "\u{2212}", # 'FULLWIDTH HYPHEN-MINUS' => 'MINUS SIGN'
19
+ "\u{2225}" => "\u{2016}", # 'PARALLEL TO' => 'DOUBLE VERTICAL LINE'
20
+ "\u{FFE0}" => "\u{00A2}", # 'FULLWIDTH CENT SIGN' => 'CENT SIGN'
21
+ "\u{FFE1}" => "\u{00A3}", # 'FULLWIDTH POUND SIGN' => 'POUND SIGN'
22
+ "\u{FFE2}" => "\u{00AC}", # 'FULLWIDTH NOT SIGN' => 'NOT SIGN'
23
+ },
24
+ 'cp932' => {
25
+ "\u{301C}" => "\u{FF5E}", # 'WAVE DASH' => 'FULLWIDTH TILDE'
26
+ "\u{2212}" => "\u{FF0D}", # 'MINUS SIGN' => 'FULLWIDTH HYPHEN-MINUS'
27
+ "\u{2016}" => "\u{2225}", # 'DOUBLE VERTICAL LINE' => 'PARALLEL TO'
28
+ "\u{2014}" => "\u{2015}", # 'EM DASH' => 'HORIZONTAL BAR'
29
+ "\u{00A2}" => "\u{FFE0}", # 'CENT SIGN' => 'FULLWIDTH CENT SIGN'
30
+ "\u{00A3}" => "\u{FFE1}", # 'POUND SIGN' => 'FULLWIDTH POUND SIGN'
31
+ "\u{00AC}" => "\u{FFE2}", # 'NOT SIGN' => 'FULLWIDTH NOT SIGN'
32
+ },
33
+ 'shift_jis' => {
34
+ "\u{FF5E}" => "\u{301C}", # 'FULLWIDTH TILDE' => 'WAVE DASH'
35
+ "\u{FF0D}" => "\u{2212}", # 'FULLWIDTH HYPHEN-MINUS' => 'MINUS SIGN'
36
+ "\u{2225}" => "\u{2016}", # 'PARALLEL TO' => 'DOUBLE VERTICAL LINE'
37
+ "\u{FFE0}" => "\u{00A2}", # 'FULLWIDTH CENT SIGN' => 'CENT SIGN'
38
+ "\u{FFE1}" => "\u{00A3}", # 'FULLWIDTH POUND SIGN' => 'POUND SIGN'
39
+ "\u{FFE2}" => "\u{00AC}", # 'FULLWIDTH NOT SIGN' => 'NOT SIGN'
40
+ },
41
+ }
42
+
43
+ def self.destination_encoding=(encoding)
44
+ @@destination_encoding = encoding
45
+ end
46
+
47
+ def self.normalize(str)
48
+ mapping = CHARACTER_CODE_MAPPING[@@destination_encoding]
49
+ return str unless mapping
50
+ str.tr(mapping.keys.join, mapping.values.join)
51
+ end
52
+
53
+ def self.invalid?(str)
54
+ return false unless str.is_a?(String)
55
+ mapping = CHARACTER_CODE_MAPPING[@@destination_encoding]
56
+ return false unless mapping
57
+ str.each_char.any? { |c| mapping[c] }
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Wavedash
2
+ VERSION = "0.1.0"
3
+ end
data/wavedash.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'wavedash/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "wavedash"
7
+ spec.version = Wavedash::VERSION
8
+ spec.authors = ["Takatoshi Ono"]
9
+ spec.email = ["takatoshi.ono@gmail.com"]
10
+
11
+ spec.summary = %q{Normalize unencodable characters}
12
+ spec.description = %q{Normalize unencodable characters}
13
+ spec.homepage = "https://github.com/takatoshiono/wavedash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wavedash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takatoshi Ono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Normalize unencodable characters
56
+ email:
57
+ - takatoshi.ono@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/wavedash.rb
73
+ - lib/wavedash/version.rb
74
+ - wavedash.gemspec
75
+ homepage: https://github.com/takatoshiono/wavedash
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Normalize unencodable characters
99
+ test_files: []