ruby_peter_v 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/Guardfile +1 -1
- data/HISTORY.txt +4 -0
- data/README.md +31 -0
- data/lib/ruby_peter_v/truncate_utf8.rb +7 -0
- data/lib/ruby_peter_v/version.rb +1 -1
- data/lib/ruby_peter_v.rb +1 -0
- data/spec/lib/ruby_peter_v/truncate_utf8_spec.rb +55 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7190173c2c5c5a0bfadfcb55c312266cad3ac5d5
|
4
|
+
data.tar.gz: 2f8acf9357dfcd5806d1780f87dbc8457904aa8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37531a42eef7fd73f99004ef47fccccae802a4219e8eb58ae30d26baffa9d4353c515102ae5a8741649decbcf61dae002036d6193f1a803545273a33b4ab3404
|
7
|
+
data.tar.gz: 43fe5335e3e48d2ce37a73e06b39cf36614c2dabade2b6e9b6dfb52ab185aefe075687e2fa531c3e5ec47c7a942ec622611c55c70ffac38cf2879b1ee0f2c7b0
|
data/.rspec
ADDED
data/Guardfile
CHANGED
data/HISTORY.txt
CHANGED
data/README.md
CHANGED
@@ -120,3 +120,34 @@ d
|
|
120
120
|
2.0.0-p195 :032 > b1 == b2
|
121
121
|
=> false
|
122
122
|
```
|
123
|
+
|
124
|
+
### truncate_utf8(max_byte_size) on String
|
125
|
+
|
126
|
+
Cutting off on bytes inside a utf-8 encoded string will create invalid strings.
|
127
|
+
This implementation limits the number of _bytes_ (not the number of characters).
|
128
|
+
It is based on the response of "Angelo" on
|
129
|
+
http://joernhees.de/blog/2010/12/14/how-to-restrict-the-length-of-a-unicode-string/
|
130
|
+
|
131
|
+
```
|
132
|
+
/Users/peter_v/p/ruby_peter_v $ irb
|
133
|
+
2.0.0-p247 :001 > require 'ruby_peter_v'
|
134
|
+
=> true
|
135
|
+
2.0.0-p247 :002 > s = 'ABCDüéàè'
|
136
|
+
=> "ABCDüéàè"
|
137
|
+
2.0.0-p247 :003 > s.size
|
138
|
+
=> 8
|
139
|
+
2.0.0-p247 :004 > s.bytesize
|
140
|
+
=> 12
|
141
|
+
2.0.0-p247 :005 > s.truncate_utf8(4)
|
142
|
+
=> "ABCD"
|
143
|
+
2.0.0-p247 :006 > s.truncate_utf8(5)
|
144
|
+
=> "ABCD"
|
145
|
+
2.0.0-p247 :007 > s.truncate_utf8(5).bytesize
|
146
|
+
=> 4
|
147
|
+
2.0.0-p247 :008 > s.truncate_utf8(6)
|
148
|
+
=> "ABCDü"
|
149
|
+
2.0.0-p247 :009 > s.truncate_utf8(7)
|
150
|
+
=> "ABCDü"
|
151
|
+
2.0.0-p247 :010 > s.truncate_utf8(7).valid_encoding?
|
152
|
+
=> true
|
153
|
+
```
|
data/lib/ruby_peter_v/version.rb
CHANGED
data/lib/ruby_peter_v.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding=utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'truncate_utf8' do
|
5
|
+
|
6
|
+
context 'ASCII only' do
|
7
|
+
|
8
|
+
let (:a120) { 'A' * 120 }
|
9
|
+
|
10
|
+
it 'leaves untouched if max_size is larger' do
|
11
|
+
a120.truncate_utf8(140).should == a120
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'leaves untouched if max_size is equal' do
|
15
|
+
a120.truncate_utf8(120).should == a120
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'cuts-off if max_size is smaller' do
|
19
|
+
a120.truncate_utf8(100).should == 'A'*100
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'UTF-8' do
|
24
|
+
|
25
|
+
let (:a120) { 'A' * 100 + 'üÜêéàèûôëï' }
|
26
|
+
|
27
|
+
it 'leaves untouched if max_size is larger' do
|
28
|
+
a120.truncate_utf8(140).should == a120
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'leaves untouched if max_size is equal' do
|
32
|
+
a120.truncate_utf8(120).should == a120
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'cuts-off if max_size is smaller' do
|
36
|
+
a120.truncate_utf8(100).should == 'A' * 100
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'cut-off falls in the UTF-8 region' do
|
40
|
+
it 'on utf-8 boundary' do
|
41
|
+
a120.truncate_utf8(104).should == 'A' * 100 + 'üÜ'
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'halfway in a utf-8 character' do
|
45
|
+
it 'keeps all up to one less' do
|
46
|
+
a120.truncate_utf8(103).should == 'A' * 100 + 'ü'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'is valid encoding' do
|
50
|
+
a120.truncate_utf8(103).should be_valid_encoding
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_peter_v
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Vandenabeele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
+
- .rspec
|
77
78
|
- .rvmrc
|
78
79
|
- .travis.yml
|
79
80
|
- Gemfile
|
@@ -87,12 +88,14 @@ files:
|
|
87
88
|
- lib/ruby_peter_v/each_recursively.rb
|
88
89
|
- lib/ruby_peter_v/set_once.rb
|
89
90
|
- lib/ruby_peter_v/single.rb
|
91
|
+
- lib/ruby_peter_v/truncate_utf8.rb
|
90
92
|
- lib/ruby_peter_v/version.rb
|
91
93
|
- ruby_peter_v.gemspec
|
92
94
|
- spec/lib/ruby_peter_v/define_equality_spec.rb
|
93
95
|
- spec/lib/ruby_peter_v/each_recursively_spec.rb
|
94
96
|
- spec/lib/ruby_peter_v/set_once_spec.rb
|
95
97
|
- spec/lib/ruby_peter_v/single_spec.rb
|
98
|
+
- spec/lib/ruby_peter_v/truncate_utf8_spec.rb
|
96
99
|
- spec/spec_helper.rb
|
97
100
|
homepage: https://github.com/petervandenabeele/ruby_peter_v
|
98
101
|
licenses: []
|
@@ -122,4 +125,5 @@ test_files:
|
|
122
125
|
- spec/lib/ruby_peter_v/each_recursively_spec.rb
|
123
126
|
- spec/lib/ruby_peter_v/set_once_spec.rb
|
124
127
|
- spec/lib/ruby_peter_v/single_spec.rb
|
128
|
+
- spec/lib/ruby_peter_v/truncate_utf8_spec.rb
|
125
129
|
- spec/spec_helper.rb
|