string19 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -18,16 +18,12 @@ if String19::IS_18
18
18
  @chars = wrapped.scan(/./m)
19
19
  end
20
20
 
21
- def chars
22
- @chars.map{|c| String19(c) }.enum_for(:each)
23
- end
24
-
25
21
  def bytesize
26
22
  to_s.size
27
23
  end
28
24
 
29
- def bytes
30
- to_s.bytes
25
+ def chars
26
+ @chars.map{|c| String19(c) }.enum_for(:each)
31
27
  end
32
28
 
33
29
  def encoding
@@ -62,6 +58,26 @@ if String19::IS_18
62
58
  String19(s[0...found]).size
63
59
  end
64
60
 
61
+ def insert(position, item)
62
+ if position.abs > size
63
+ raise IndexError, "index #{position} out of string"
64
+ end
65
+
66
+ @chars.insert(position, *String19(item).chars.to_a)
67
+ self
68
+ end
69
+
70
+ def gsub!(*args, &block)
71
+ return unless result = to_s.send(:gsub!, *args, &block)
72
+ @chars = result.scan(/./m)
73
+ self
74
+ end
75
+
76
+ def gsub(*args, &block)
77
+ copy = dup
78
+ copy.gsub!(*args, &block) || copy
79
+ end
80
+
65
81
  def self.wrap(*args)
66
82
  args.each do |method|
67
83
  eval("def #{method}(*args, &block); String19(@chars.#{method}(*args, &block)); end")
@@ -74,8 +90,15 @@ if String19::IS_18
74
90
  end
75
91
  end
76
92
 
93
+ def self.delegate_to_s(*args)
94
+ args.each do |method|
95
+ eval("def #{method}(*args, &block); to_s.#{method}(*args, &block); end")
96
+ end
97
+ end
98
+
77
99
  wrap :dup, :slice, :slice!, :[], :inspect
78
100
  delegate :size
101
+ delegate_to_s :match, :bytes, :to_i
79
102
  end
80
103
  end
81
104
  else
@@ -1,5 +1,5 @@
1
1
  module String19
2
2
  class BlankSlate
3
- instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_|should|respond_to\?)/ }
3
+ instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_|should|respond_to\?|object_id)/ }
4
4
  end
5
5
  end
@@ -119,6 +119,10 @@ describe String19 do
119
119
  end
120
120
  end
121
121
 
122
+ it "has bytesize" do
123
+ String19('áßð').bytesize.should == 6
124
+ end
125
+
122
126
  describe :respond_to? do
123
127
  it "is true for mapped methods" do
124
128
  String19('x').respond_to?(:valid_encoding?).should == true
@@ -129,9 +133,82 @@ describe String19 do
129
133
  end
130
134
  end
131
135
 
136
+ describe :insert do
137
+ it "can insert at normal positions" do
138
+ result = String19("áßð").insert(1,'áßð')
139
+ result.should == 'ááßðßð'
140
+ result.size.should == 6
141
+ end
142
+
143
+ it "can insert to negative positions" do
144
+ result = String19("áßð").insert(-1,'áßð')
145
+ result.should == "áßðáßð"
146
+ result.size.should == 6
147
+ end
148
+
149
+ it "cannot insert outsize of string" do
150
+ lambda{
151
+ String19("áßð").insert(4,'áßð')
152
+ }.should raise_error
153
+ end
154
+ end
155
+
156
+ describe :match do
157
+ it "matches" do
158
+ String19('áßð').match(/ß(ð)/).to_a.should == ['ßð','ð']
159
+ end
160
+
161
+ it "does not match" do
162
+ String19('áßð').match(/xß(ð)/).should == nil
163
+ end
164
+
165
+ it "returns String19" do
166
+ pending
167
+ String19('áßð').match(/ß(ð)/)[0].size.should == 2
168
+ end
169
+ end
170
+
171
+ describe :gsub do
172
+ it "gsubs!" do
173
+ original = String19('áßð')
174
+ result = original.gsub!('ß','x')
175
+ result.should == 'áxð'
176
+ result.size.should == 3
177
+ original.should == 'áxð'
178
+ result.object_id.should == original.object_id
179
+ end
180
+
181
+ it "gsubs" do
182
+ original = String19('áßð')
183
+ result = original.gsub('ß','x')
184
+ result.should == 'áxð'
185
+ result.size.should == 3
186
+ original.should == 'áßð'
187
+ result.object_id.should_not == original.object_id
188
+ end
189
+
190
+ it "is nil when gsub! does not match" do
191
+ String19("x").gsub!('xxx','d').should == nil
192
+ end
193
+
194
+ it "is copy when gsub does not match" do
195
+ original = String19('áßð')
196
+ result = original.gsub('ßßßß','x')
197
+ result.should == 'áßð'
198
+ result.size.should == 3
199
+ original.should == 'áßð'
200
+ result.object_id.should_not == original.object_id
201
+ end
202
+ end
203
+
132
204
  describe 'replacement' do
133
205
  it "behaves like a string" do
134
206
  "#{String19('xxx')}".should == "xxx"
135
207
  end
136
208
  end
209
+
210
+ it 'to_i' do
211
+ String19('1').to_i.should == 1
212
+ String19('').to_i.should == 0
213
+ end
137
214
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{string19}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string19
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser