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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9762b0492175ba460a71bc5602d2d42b5f1c2b5e
4
- data.tar.gz: 3c7ae31f9dc676b795a2caa239bf1a5357e17c96
3
+ metadata.gz: 7190173c2c5c5a0bfadfcb55c312266cad3ac5d5
4
+ data.tar.gz: 2f8acf9357dfcd5806d1780f87dbc8457904aa8d
5
5
  SHA512:
6
- metadata.gz: 562966d338daaf75304ace84f272bbefd9aa4454f61abaca07b69fb01e0efff1cfb0679e3f7c1e9a9a489da06547dc31b6d49da7b3a49d1721829c0d099fcb45
7
- data.tar.gz: de6280af695b4badacd306374aa681b5c4865790330adebea3c77199addfb2a30871288597efd014e89353aebb024b9502e820314c8545da5a8961b698a24805
6
+ metadata.gz: 37531a42eef7fd73f99004ef47fccccae802a4219e8eb58ae30d26baffa9d4353c515102ae5a8741649decbcf61dae002036d6193f1a803545273a33b4ab3404
7
+ data.tar.gz: 43fe5335e3e48d2ce37a73e06b39cf36614c2dabade2b6e9b6dfb52ab185aefe075687e2fa531c3e5ec47c7a942ec622611c55c70ffac38cf2879b1ee0f2c7b0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard :rspec do
1
+ guard :rspec, :all_after_pass => true, :all_on_start => true do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
data/HISTORY.txt CHANGED
@@ -39,5 +39,9 @@
39
39
 
40
40
  * Object#each_recursively always returns self
41
41
 
42
+ 0.0.10 (2013-07-08)
43
+
44
+ * truncate_utf8(max_byte_size)
45
+
42
46
  TODO
43
47
  * Object#assert_keys_in
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
+ ```
@@ -0,0 +1,7 @@
1
+ class String
2
+
3
+ def truncate_utf8(max_byte_size)
4
+ self.bytes.to_a[0...max_byte_size].pack('c*').force_encoding('UTF-8').encode("UTF-16BE", :invalid => :replace, :replace =>"").encode("UTF-8")
5
+ end
6
+
7
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyPeterV
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
data/lib/ruby_peter_v.rb CHANGED
@@ -3,3 +3,4 @@ require 'ruby_peter_v/single'
3
3
  require 'ruby_peter_v/set_once'
4
4
  require 'ruby_peter_v/define_equality'
5
5
  require 'ruby_peter_v/each_recursively'
6
+ require 'ruby_peter_v/truncate_utf8'
@@ -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.9
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-06-16 00:00:00.000000000 Z
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