polish_chars 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +8 -0
- data/bm/benchmark_polish_chars.rb +19 -0
- data/bm/polskie_stringi.rb +28 -0
- data/lib/polish_chars.rb +1 -0
- data/lib/polish_chars/core_ext/string.rb +91 -0
- data/polish_chars.gemspec +21 -0
- data/test/test_polish_chars.rb +37 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 snukky
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# PolishChars
|
2
|
+
|
3
|
+
Gem extends the Ruby `String` class methods, such as `#downcase` and `#upcase` by
|
4
|
+
handling Polish diacritics. It also adds `#no_pl` method.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'polish_chars'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install polish_chars
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
require "polish\_chars"
|
23
|
+
|
24
|
+
text = "Zażółć Gęślą Jaźń"
|
25
|
+
text.downcase # => "zażółć gęślą jaźń"
|
26
|
+
text.upcase # => "ZAŻÓŁĆ GĘŚLĄ JAŹŃ"
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), "../lib")
|
4
|
+
|
5
|
+
require_relative "polskie_stringi"
|
6
|
+
require "polish_chars"
|
7
|
+
|
8
|
+
require "benchmark"
|
9
|
+
|
10
|
+
n = 100_000
|
11
|
+
|
12
|
+
all_chars = ('a'..'z').to_a + ('A'..'Z').to_a + String::PL_TO_ASCII.keys
|
13
|
+
sample_str = (0...n).map{ all_chars[rand(all_chars.size)] }.join
|
14
|
+
|
15
|
+
Benchmark.bm(16) do |bm|
|
16
|
+
bm.report("standard lib:") { sample_str.downcase_ascii }
|
17
|
+
bm.report("polskie_stringi:") { sample_str.downcase_ps }
|
18
|
+
bm.report("polish_chars:") { sample_str.downcase }
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# code from gem 'polskie_stringi'
|
3
|
+
|
4
|
+
class ::String
|
5
|
+
|
6
|
+
def downcase_ps
|
7
|
+
pl_replace(String.polish_chars('big') + ('A'..'Z').to_a, String.polish_chars('small') + ('a'..'z').to_a)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.polish_chars(size = 'all')
|
11
|
+
small = %w[ą ę ś ć ź ż ó ł ń]
|
12
|
+
big = %w[Ą Ę Ś Ć Ź Ż Ó Ł Ń]
|
13
|
+
case size
|
14
|
+
when 'small'
|
15
|
+
small
|
16
|
+
when 'big'
|
17
|
+
big
|
18
|
+
else
|
19
|
+
small + big
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def pl_replace(search, replace)
|
26
|
+
self.chars.collect{|c| (id = search.index(c)) ? replace[id] : c}.join('')
|
27
|
+
end
|
28
|
+
end
|
data/lib/polish_chars.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "polish_chars/core_ext/string"
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class ::String
|
4
|
+
|
5
|
+
BIG_TO_SMALL_PL = {
|
6
|
+
'Ą' => 'ą',
|
7
|
+
'Ć' => 'ć',
|
8
|
+
'Ę' => 'ę',
|
9
|
+
'Ł' => 'ł',
|
10
|
+
'Ń' => 'ń',
|
11
|
+
'Ó' => 'ó',
|
12
|
+
'Ś' => 'ś',
|
13
|
+
'Ź' => 'ź',
|
14
|
+
'Ż' => 'ż'
|
15
|
+
}
|
16
|
+
|
17
|
+
SMALL_TO_BIG_PL = BIG_TO_SMALL_PL.invert
|
18
|
+
|
19
|
+
BIG_PL_REGEXP = Regexp.new("([#{BIG_TO_SMALL_PL.keys.join}])")
|
20
|
+
SMALL_PL_REGEXP = Regexp.new("([#{SMALL_TO_BIG_PL.keys.join}])")
|
21
|
+
|
22
|
+
PL_TO_ASCII = {
|
23
|
+
'ą' => 'a',
|
24
|
+
'Ą' => 'A',
|
25
|
+
'ć' => 'c',
|
26
|
+
'Ć' => 'C',
|
27
|
+
'ę' => 'e',
|
28
|
+
'Ę' => 'E',
|
29
|
+
'ł' => 'l',
|
30
|
+
'Ł' => 'L',
|
31
|
+
'ń' => 'n',
|
32
|
+
'Ń' => 'N',
|
33
|
+
'ó' => 'o',
|
34
|
+
'Ó' => 'O',
|
35
|
+
'ś' => 's',
|
36
|
+
'Ś' => 'S',
|
37
|
+
'ź' => 'z',
|
38
|
+
'Ż' => 'Z',
|
39
|
+
'ż' => 'z',
|
40
|
+
'Ź' => 'Z'
|
41
|
+
}
|
42
|
+
|
43
|
+
PL_REGEXP = Regexp.new("([#{PL_TO_ASCII.keys.join}])")
|
44
|
+
|
45
|
+
alias :downcase_ascii :downcase
|
46
|
+
|
47
|
+
def downcase
|
48
|
+
self.downcase_ascii.gsub(BIG_PL_REGEXP, BIG_TO_SMALL_PL)
|
49
|
+
end
|
50
|
+
|
51
|
+
alias :downcase_ascii! :downcase!
|
52
|
+
|
53
|
+
def downcase!
|
54
|
+
replace downcase
|
55
|
+
end
|
56
|
+
|
57
|
+
alias :upcase_ascii :upcase
|
58
|
+
|
59
|
+
def upcase
|
60
|
+
self.upcase_ascii.gsub(SMALL_PL_REGEXP, SMALL_TO_BIG_PL)
|
61
|
+
end
|
62
|
+
|
63
|
+
alias :upcase_ascii! :upcase!
|
64
|
+
|
65
|
+
def upcase!
|
66
|
+
replace upcase
|
67
|
+
end
|
68
|
+
|
69
|
+
alias :capitalize_ascii :capitalize
|
70
|
+
|
71
|
+
def capitalize
|
72
|
+
str = self.downcase
|
73
|
+
str[0] = str[0].upcase if str.size > 0
|
74
|
+
str
|
75
|
+
end
|
76
|
+
|
77
|
+
alias :capitalize_ascii! :capitalize!
|
78
|
+
|
79
|
+
def capitalize!
|
80
|
+
replace capitalize
|
81
|
+
end
|
82
|
+
|
83
|
+
def no_pl
|
84
|
+
self.gsub(PL_REGEXP, PL_TO_ASCII)
|
85
|
+
end
|
86
|
+
|
87
|
+
def no_pl!
|
88
|
+
replace no_pl
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "polish_chars"
|
7
|
+
gem.version = "0.0.2"
|
8
|
+
gem.authors = ["snukky"]
|
9
|
+
gem.email = ["snk987@gmail.com"]
|
10
|
+
gem.description = %q{Gem extends the Ruby String class methods, such
|
11
|
+
as #downcase and #upcase by handling Polish diacritics.
|
12
|
+
It also adds #no_pl method.}
|
13
|
+
gem.summary = %q{Extension of Ruby String class by handling Polish
|
14
|
+
diacritics.}
|
15
|
+
gem.homepage = "https://github.com/snukky/polish_chars"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), "../lib")
|
4
|
+
|
5
|
+
require "polish_chars"
|
6
|
+
require "test/unit"
|
7
|
+
|
8
|
+
class TestPolishChars < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_downcase
|
11
|
+
assert_equal "zażółć gęślą jaźń", "ZAŻÓŁĆ GĘŚLĄ JAŹŃ".downcase
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_upcase
|
15
|
+
assert_equal "ZAŻÓŁĆ GĘŚLĄ JAŹŃ", "zażółć gęślą jaźń".upcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_capitalize
|
19
|
+
assert_equal "Zażółć gęślą jaźń", "ZAŻÓŁĆ Gęślą jaźń".capitalize
|
20
|
+
assert_equal "Ąbć", "ĄBĆ".capitalize
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_no_pl
|
24
|
+
assert_equal "ZAZOLC gesla jazn", "ZAŻÓŁĆ gęślą jaźń".no_pl
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_bang_downcase
|
28
|
+
str = "ZAŻÓŁĆ GĘŚLĄ JAŹŃ"
|
29
|
+
|
30
|
+
str.downcase
|
31
|
+
assert_equal "ZAŻÓŁĆ GĘŚLĄ JAŹŃ", str
|
32
|
+
|
33
|
+
str.downcase!
|
34
|
+
assert_equal "zażółć gęślą jaźń", str
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polish_chars
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- snukky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "Gem extends the Ruby String class methods, such \n as
|
15
|
+
#downcase and #upcase by handling Polish diacritics. \n It
|
16
|
+
also adds #no_pl method."
|
17
|
+
email:
|
18
|
+
- snk987@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bm/benchmark_polish_chars.rb
|
29
|
+
- bm/polskie_stringi.rb
|
30
|
+
- lib/polish_chars.rb
|
31
|
+
- lib/polish_chars/core_ext/string.rb
|
32
|
+
- polish_chars.gemspec
|
33
|
+
- test/test_polish_chars.rb
|
34
|
+
homepage: https://github.com/snukky/polish_chars
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Extension of Ruby String class by handling Polish diacritics.
|
58
|
+
test_files:
|
59
|
+
- test/test_polish_chars.rb
|