Skip to content

Commit 9e91796

Browse files
committed
Fix syntax warnings and allocation specs
1 parent 925c777 commit 9e91796

File tree

11 files changed

+458
-243
lines changed

11 files changed

+458
-243
lines changed

lib/stupidedi/reader/pointer.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def +(other)
283283
end
284284

285285
if (@storage.equal?(o_storage) and @offset + @length == o_offset) \
286-
or subseq_eq?(@storage, @offset + @length, o_storage, o_offset, o_length)
286+
or subseq_eq?(@storage, @offset + @length, o_storage, o_offset, o_length)
287287
self.class.new(@storage.freeze, @offset, @length + o_length)
288288
else
289289
if other.is_a?(self.class)
@@ -338,6 +338,9 @@ def inspect
338338
# This operation typically allocates memory and copies part of @storage,
339339
# so this is avoided as much as possible.
340340
#
341+
# Unless the optional parameter `always_allocate` is `true`, then the
342+
# return value may be `#frozen?` in some cases.
343+
#
341344
# @return [S]
342345
def reify(always_allocate = false)
343346
if @storage.frozen? \
@@ -378,13 +381,13 @@ class << Pointer
378381
#########################################################################
379382

380383
# Constructs a new Pointer depending on what type of object is given.
381-
#
384+
#
382385
# NOTE: Pointer operations can potentially destrucively modify the given
383386
# object, but if it is `#frozen?`, a copy will be made before the update.
384387
# If you are accessing or modifying the object outside of the Pointer API,
385388
# unexpected results might occur. To avoid this, either provide a copy
386389
# with `#dup` or freeze the object first with `#freeze`.
387-
#
390+
#
388391
# @return [Pointer]
389392
def build(object)
390393
case object

lib/stupidedi/reader/substring.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def count(other)
177177
# substring pointer.
178178
#
179179
# @return [self]
180+
Z = "abc"
180181
def <<(other)
181182
case other
182183
when self.class
@@ -185,7 +186,7 @@ def <<(other)
185186
@length += other.length
186187
elsif not @storage.frozen?
187188
# Surely no one will notice if we destructively update @storage
188-
@storage[@offset + @length, @storage.length - @offset - @length] = other
189+
@storage[@offset + @length, @storage.length - @offset - @length] = other.reify
189190
@length += other.length
190191
else
191192
# Other pointers are sharing our storage. We need to make our own
@@ -398,7 +399,7 @@ def _match(pattern, offset, anchorless=false)
398399

399400
offset = @length if offset > length
400401
offset = @offset + offset
401-
m = pattern.match(@storage, offset)
402+
m = @storage.match(pattern, offset)
402403

403404
if m and m.begin(0) <= @offset + @length
404405
if m.end(0) <= @offset + @length

lib/stupidedi/reader/tokenizer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def _update_state(segment_tok, config)
528528
gscode = version.try(:slice, 0, 6)
529529

530530
# GS01: Functional Identifier Code
531-
fgcode = segment_tok.element_toks.at(0).try(:value)
531+
# fgcode = segment_tok.element_toks.at(0).try(:value)
532532

533533
if config.functional_group.defined_at?(gscode)
534534
envelope_def = config.functional_group.at(gscode)

lib/stupidedi/ruby/string.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def match?(pattern, pos=nil)
9090
if pos.nil?
9191
!!(self =~ pattern)
9292
else
93+
# NOTE: Regexp#match allocates a MatchData, String#match does not
9394
!!match(pattern, pos)
9495
end
9596
end

spec/lib/stupidedi/reader/native_ext_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
expect(extend_language).to be_graphic(encoding: e)
172172
end
173173

174-
if n = e[/iso-8859-(\d+)/, 1].try{|n| Integer(n) }
174+
if n = e[/iso-8859-(\d+)/, 1].try{|m| Integer(m) }
175175
it "identifies all graphic characters" do
176176
bytes = iso_8859_table.reject{|_, no| no.include?(n) }.keys
177177
string = bytes.sort.map(&:chr).join.force_encoding(e)

spec/lib/stupidedi/reader/pointer_spec.rb

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
# frozen_string_literal: true
1+
# frozen_string_literal: false
2+
23
describe Stupidedi::Reader::Pointer do
34
using Stupidedi::Refinements
45

5-
def pointer(value)
6-
Stupidedi::Reader::Pointer.build(value)
6+
def pointer(string, frozen: nil)
7+
frozen = [true, false].sample if frozen.nil?
8+
string.freeze if frozen and not string.frozen?
9+
string = string.dup if not frozen and string.frozen?
10+
Stupidedi::Reader::Pointer.build(string)
711
end
812

13+
914
def pointer_(*args)
1015
Stupidedi::Reader::Pointer.new(args)
1116
end
@@ -23,12 +28,12 @@ def suffix(pointer)
2328

2429
describe ".build" do
2530
context "when value is a String" do
26-
specify { expect(pointer("abc")).to be_a(Stupidedi::Reader::Pointer) }
31+
specify { expect(pointer("111")).to be_a(Stupidedi::Reader::Pointer) }
2732

2833
allocation do
2934
ignore = nil
30-
storage = [1,1,1]
31-
expect{ ignore = pointer(storage) }.to allocate(Stupidedi::Reader::Pointer => 1)
35+
storage = "111"
36+
expect{ ignore = pointer(storage, frozen: true) }.to allocate(Stupidedi::Reader::Substring => 1)
3237
end
3338
end
3439

@@ -37,7 +42,7 @@ def suffix(pointer)
3742

3843
allocation do
3944
storage = [1,2]
40-
expect{ pointer(storage) }.to allocate(Stupidedi::Reader::Pointer => 1)
45+
expect{ pointer(storage, frozen: true) }.to allocate(Stupidedi::Reader::Pointer => 1)
4146
end
4247
end
4348

@@ -76,24 +81,39 @@ def suffix(pointer)
7681

7782
describe "#reify" do
7883
context "when storage is frozen" do
79-
let(:abcdef) { pointer("abcdef".freeze) }
84+
let(:p) { pointer("abcdef", frozen: true) }
8085

8186
context "and storage spans entire storage" do
82-
allocation { p = abcdef; expect{ p.send(:reify) }.to allocate(String: 0) }
83-
allocation { p = abcdef; expect{ p.send(:reify, false) }.to allocate(String: 0) }
87+
allocation { p; expect{ p.send(:reify) }.to allocate(String: 0) }
88+
allocation { p; expect{ p.send(:reify, false) }.to allocate(String: 0) }
8489

8590
context "but always_allocate is true" do
86-
allocation { p = abcdef; expect{ p.send(:reify, true) }.to allocate(String: 1) }
91+
allocation { p; expect{ p.send(:reify, true) }.to allocate(String: 1) }
8792
end
8893
end
8994

9095
context "and storage does not span entire storage" do
91-
allocate { p = abcdef.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
92-
allocate { p = abcdef.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
96+
allocate { p = p.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
97+
allocate { p = p.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
9398
end
9499
end
95100

96-
todo "when storage is not frozen" do
101+
context "when storage is not frozen" do
102+
let(:p) { pointer("abcdef", frozen: false) }
103+
104+
context "and storage spans entire storage" do
105+
allocation { p; expect{ p.send(:reify) }.to allocate(String: 1) }
106+
allocation { p; expect{ p.send(:reify, false) }.to allocate(String: 1) }
107+
108+
context "but always_allocate is true" do
109+
allocation { p; expect{ p.send(:reify, true) }.to allocate(String: 1) }
110+
end
111+
end
112+
113+
context "and storage does not span entire storage" do
114+
allocate { p = p.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
115+
allocate { p = p.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
116+
end
97117
end
98118
end
99119

@@ -159,11 +179,10 @@ def suffix(pointer)
159179
end
160180

161181
describe "+" do
162-
let(:a) { pointer("abcdefghi".dup) }
163-
164182
context "when argument is a non-pointer value" do
165183
context "when pointer suffix starts with argument" do
166184
specify do
185+
a = pointer("abcdefghi")
167186
b = a.drop(3).take(3)
168187
c = "gh"
169188

@@ -178,15 +197,17 @@ def suffix(pointer)
178197
end
179198

180199
allocation do
200+
a = pointer("abcdefghi")
181201
b = a.drop(3).take(3)
182202
c = "gh"
183203
expect(suffix(b)).to start_with(c)
184-
expect{ b + c }.to allocate(String: 0, a.class => 1)
204+
expect{ b + c }.to allocate(b.class => 1)
185205
end
186206
end
187207

188208
context "when argument is pointer suffix plus more" do
189209
specify do
210+
a = pointer("abcdefghi")
190211
b = a.drop(3).take(3)
191212
c = "ghijkl"
192213

@@ -203,6 +224,7 @@ def suffix(pointer)
203224
end
204225

205226
allocation do
227+
a = pointer("abcdefghi")
206228
b = a.drop(3).take(3)
207229
c = "ghijkl"
208230
expect(c).to start_with(suffix(b))
@@ -213,6 +235,7 @@ def suffix(pointer)
213235

214236
context "when argument is not pointer suffix" do
215237
specify do
238+
a = pointer("abcdefghi")
216239
b = a.take(6)
217240
c = "xxx"
218241

@@ -228,6 +251,7 @@ def suffix(pointer)
228251
end
229252

230253
allocation do
254+
a = pointer("abcdefghi")
231255
b = a.take(6)
232256
c = "xxx"
233257
expect(a.storage).to be_frozen
@@ -239,6 +263,7 @@ def suffix(pointer)
239263
context "when argument is a string pointer" do
240264
context "when pointer suffix starts with argument" do
241265
specify do
266+
a = pointer("abcdefghi")
242267
b = a.drop(3).take(3)
243268
c = pointer("gh")
244269

@@ -254,15 +279,17 @@ def suffix(pointer)
254279
end
255280

256281
allocation do
282+
a = pointer("abcdefghi")
257283
b = a.drop(3).take(3)
258284
c = pointer("gh")
259285
expect(suffix(b)).to start_with(c)
260-
expect{ b + c }.to allocate(a.class => 1)
286+
expect{ b + c }.to allocate(b.class => 1)
261287
end
262288
end
263289

264290
context "when argument is pointer suffix plus more" do
265291
specify do
292+
a = pointer("abcdefghi")
266293
b = a.drop(3).take(3)
267294
c = pointer("ghijkl")
268295

@@ -279,6 +306,7 @@ def suffix(pointer)
279306
end
280307

281308
allocation do
309+
a = pointer("abcdefghi")
282310
b = a.drop(3).take(3)
283311
c = pointer("ghijkl")
284312
expect(c).to start_with(suffix(b))
@@ -289,6 +317,7 @@ def suffix(pointer)
289317

290318
context "when argument is not pointer suffix" do
291319
specify do
320+
a = pointer("abcdefghi")
292321
b = a.take(6)
293322
c = pointer("xxx")
294323

@@ -304,51 +333,35 @@ def suffix(pointer)
304333
end
305334

306335
allocation do
336+
a = pointer("abcdefghi")
307337
b = a.take(6)
308-
c = pointer("xxx")
338+
c = pointer("xxx", frozen: true)
309339
expect(suffix(b)).to_not start_with(c)
310-
expect{ b + c }.to allocate(String: 1)
340+
expect{ b + c }.to allocate(String: 1 + (c.storage.frozen? && 0 || 1))
341+
end
342+
343+
allocation do
344+
a = pointer("abcdefghi")
345+
b = a.take(6)
346+
c = pointer("xxx", frozen: false)
347+
expect(suffix(b)).to_not start_with(c)
348+
expect{ b + c }.to allocate(String: 1 + (c.storage.frozen? && 0 || 1))
311349
end
312350
end
313351
end
314352
end
315353

316-
describe "#head" do
317-
end
318-
319-
describe "#last" do
320-
end
321-
322-
describe "#defined_at?" do
323-
end
324-
325-
describe "#at" do
326-
end
327-
328-
describe "#tail" do
329-
end
330-
331-
describe "#[]" do
332-
end
333-
334-
describe "#drop" do
335-
end
336-
337-
describe "#drop!" do
338-
end
339-
340-
describe "take" do
341-
end
342-
343-
context "#take!" do
344-
end
345-
346-
describe "#drop_take" do
347-
end
348-
349-
describe "#split_at" do
350-
end
351-
352-
describe ".build" do
353-
end
354+
todo "#head"
355+
todo "#last"
356+
todo "#defined_at?"
357+
todo "#at"
358+
todo "#tail"
359+
todo "#[]"
360+
todo "#drop"
361+
todo "#drop!"
362+
todo "take"
363+
todo "#take!"
364+
todo "#drop_take"
365+
todo "#split_at"
366+
todo ".build"
354367
end

0 commit comments

Comments
 (0)