filter_io 0.2.7 → 0.2.11

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 88a035f96c444d4c3c91aa87b0bb7628c8d9092b
4
- data.tar.gz: 26497c89876324625e20fd824cfb83d04288b4e5
2
+ SHA256:
3
+ metadata.gz: 2aa379c9231e3f092273dbc5cf5306b9dcb58b749aa4c3c4ada4081995b1dbf4
4
+ data.tar.gz: 370b83c3187be5d2a863e7b94e75a717da16ee6c25360edd91eae61ba0e713b8
5
5
  SHA512:
6
- metadata.gz: f3f82c288657ac02c89afb0a32a3f66ea473dc83d901c6f463cd548297de2b2a7293b80575f72a1bbc05567b858411f9cec103ff0b3a838b0abb68ec0a23c98a
7
- data.tar.gz: a270fe4c534c2193a35a31561157979774938036437ece8e26d5333ff658aff9085ab99b4bc803cec4ae32de228baccde59d9d7c7bdcdc8685e6afdf19c88e48
6
+ metadata.gz: 7805dc1d20a7325bb87161836b67e8cf949ed021411678f5280d0ebd85ef6e535c27cf4b32cc935b35690c99b529d79ad0413397ca0f50129a6f0a6a653016c3
7
+ data.tar.gz: e8d5aac3c61ca1b819e6b6b029af39a432735ab1734ec8d0a2ef3c0ee777812d185284a5ea40bd8275ee7ec4b34e5c11ce8857e60842a633f8aeb7c2b1f8f7db
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.5
data/README.markdown CHANGED
@@ -93,7 +93,7 @@ When either `readline`, `gets` or `read(nil)` is called, `filter_io` will proces
93
93
 
94
94
  Ruby 1.9 has character encoding support can convert between UTF-8, ISO-8859-1, ASCII-8BIT, etc. This is triggered in `IO` by using `:external_encoding` and `:internal_encoding` when opening the stream.
95
95
  `filter_io` will use the underlying stream's encoding settings when reading and filtering data. The processing block will be passed data in the internal encoding.
96
- As per the core `IO` object, if `read` is called with a length (in bytes), the data will be returned in the external encoding.
96
+ As per the core `IO` object, if `read` is called with a length (in bytes), the data will be returned in ASCII-8BIT.
97
97
  In summary, everything should Just Work™
98
98
 
99
99
  ### Notes on Patches/Pull Requests
data/filter_io.gemspec CHANGED
@@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.add_development_dependency 'bundler', '~> 1.3'
20
+ spec.add_development_dependency 'bundler', '~> 2.0'
21
21
  spec.add_development_dependency 'rake'
22
22
  spec.add_development_dependency 'simplecov'
23
- spec.add_development_dependency 'rspec', '~> 2.13'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
24
  spec.add_development_dependency 'pry'
25
25
  end
@@ -1,3 +1,3 @@
1
1
  class FilterIO
2
- VERSION = '0.2.7'
2
+ VERSION = '0.2.11'
3
3
  end
data/lib/filter_io.rb CHANGED
@@ -54,12 +54,11 @@ class FilterIO
54
54
  end
55
55
 
56
56
  def default_encoding
57
- unless @default_encoding
57
+ @default_encoding ||= begin
58
58
  c = @io.getc
59
59
  @io.ungetc c
60
- @default_encoding = c.encoding
60
+ c.encoding
61
61
  end
62
- @default_encoding
63
62
  end
64
63
 
65
64
  def internal_encoding
@@ -103,6 +102,7 @@ class FilterIO
103
102
  return '' if length == 0
104
103
 
105
104
  # fill the buffer up to the fill level (or whole input if length is nil)
105
+ @did_first_read ||= false
106
106
  while (!source_eof? || !@did_first_read) && (length.nil? || length > @buffer.bytesize)
107
107
  @did_first_read = true
108
108
  buffer_data @options[:block_size] || length
@@ -188,12 +188,12 @@ class FilterIO
188
188
  raise ArgumentError
189
189
  end
190
190
 
191
+ return "" if limit == 0
191
192
  return nil if eof?
192
- return read if sep_string.nil?
193
193
 
194
194
  paragraph_mode = sep_string == ''
195
195
  sep_string = "\n\n" if paragraph_mode
196
- sep_string = sep_string.to_s unless sep_string.is_a? String
196
+ sep_string = sep_string&.to_s unless sep_string.is_a? String
197
197
 
198
198
  if paragraph_mode
199
199
  # consume any leading newlines
@@ -278,7 +278,7 @@ class FilterIO
278
278
  def with_byte_buffer
279
279
  begin
280
280
  org_encoding = @buffer.encoding
281
- @buffer.force_encoding 'ASCII-8BIT'
281
+ @buffer.force_encoding Encoding::BINARY
282
282
  yield
283
283
  ensure
284
284
  @buffer.force_encoding org_encoding
@@ -298,7 +298,7 @@ class FilterIO
298
298
 
299
299
  def find_bytes(str)
300
300
  with_byte_buffer do
301
- @buffer.index(str)
301
+ str && @buffer.index(str)
302
302
  end
303
303
  end
304
304
 
@@ -325,7 +325,7 @@ class FilterIO
325
325
  # and add some more data to the buffer
326
326
  raise NeedMoreData
327
327
  end
328
- rescue NeedMoreData => e
328
+ rescue NeedMoreData
329
329
  raise EOFError, 'end of file reached' if @io.eof?
330
330
  new_data = @io.read(block_size).force_encoding(external_encoding)
331
331
  data << new_data
@@ -336,7 +336,7 @@ class FilterIO
336
336
  data = [data] unless data.is_a? Array
337
337
  raise 'Block must have 1 or 2 values' unless data.size <= 2
338
338
  if @buffer.encoding != data[0].encoding
339
- if [@buffer, data[0]].any? { |x| x.encoding.to_s == 'ASCII-8BIT' }
339
+ if [@buffer, data[0]].any? { |x| x.encoding == Encoding::BINARY }
340
340
  data[0] = data[0].dup.force_encoding @buffer.encoding
341
341
  end
342
342
  end
@@ -367,7 +367,7 @@ class FilterIO
367
367
  rescue Errno::ESPIPE
368
368
  @source_pos
369
369
  end
370
- args << BlockState.new(src_pos == data.length, source_eof?)
370
+ args << BlockState.new(src_pos == data.bytesize, source_eof?)
371
371
  end
372
372
  data = @block.call(*args)
373
373
  raise IOError, 'Block returned nil' if data.nil?
@@ -28,6 +28,8 @@ describe FilterIO do
28
28
  errors << [e.class, e.message]
29
29
  end
30
30
  positions << io.pos
31
+
32
+ break if results == [""] * 10 && errors == [nil] * 10 && positions == [0] * 10
31
33
  raise 'Too many iterations' if results.size > 100
32
34
  end
33
35
 
@@ -204,7 +206,7 @@ describe FilterIO do
204
206
 
205
207
  it 'converts ISO-8859-1 to UTF-8 via a block' do
206
208
  [1, 2, nil].each do |block_size|
207
- expected = "über\nrésumé"
209
+ # expected: "über\nrésumé"
208
210
  with_iso8859_1_test_file 'UTF-8' do |io_raw|
209
211
  io = FilterIO.new(io_raw, :block_size => block_size) do |data, state|
210
212
  if state.bof?
@@ -225,7 +227,7 @@ describe FilterIO do
225
227
 
226
228
  it 'converts ISO-8859-1 to raw via a block' do
227
229
  [1, 2, nil].each do |block_size|
228
- expected = "über\nrésumé".encode('ISO-8859-1')
230
+ # expected: "über\nrésumé".encode('ISO-8859-1')
229
231
  with_iso8859_1_test_file 'ISO-8859-1' do |io_raw|
230
232
  io = FilterIO.new(io_raw, :block_size => block_size) do |data, state|
231
233
  if state.bof?
@@ -348,6 +350,18 @@ describe FilterIO do
348
350
  expect(io.read).to eq expected
349
351
  end
350
352
 
353
+ it 'passes true for BOF to the block when stream starts with a UTF-8 BOM' do
354
+ input = "\xEF\xBB\xBFTest String"
355
+ expected = ">>>*\xEF\xBB\xBFT**est **Stri**ng*<<<"
356
+ io = FilterIO.new(StringIO.new(input), :block_size => 4) do |data, state|
357
+ data = "*#{data}*"
358
+ data = ">>>#{data}" if state.bof?
359
+ data = "#{data}<<<" if state.eof?
360
+ data
361
+ end
362
+ expect(io.read).to eq expected
363
+ end
364
+
351
365
  it 'passes false for BOF to the block if stream previously read' do
352
366
  input = StringIO.new 'Test String'
353
367
  expect(input.read(4)).to eq 'Test'
@@ -451,7 +465,7 @@ describe FilterIO do
451
465
 
452
466
  it 'can be rewound with block' do
453
467
  input = 'abcdefghij'
454
- expected = input[1..-1]
468
+ # expected: input[1..-1]
455
469
  io = FilterIO.new(StringIO.new(input), :block_size => 4) do |data, state|
456
470
  data = data[1..-1] if state.bof?
457
471
  data
@@ -562,7 +576,7 @@ describe FilterIO do
562
576
  end
563
577
  end
564
578
 
565
- it 'supports `get` with a limit' do
579
+ it 'supports `gets` with a limit' do
566
580
  [
567
581
  "",
568
582
  "x",
@@ -570,11 +584,24 @@ describe FilterIO do
570
584
  "abc\rdef\rghi\r",
571
585
  "über",
572
586
  ].each do |input|
573
- [1, 2, 3, 4, 10].each do |limit|
587
+ [0, 1, 2, 3, 4, 10].each do |limit|
574
588
  matches_reference_io_behaviour(input) { |io| io.gets(limit) }
575
589
  end
576
590
  end
577
- # TODO: test zero limit
591
+ end
592
+
593
+ it 'supports `gets` with nil separator and a limit' do
594
+ [
595
+ "",
596
+ "x",
597
+ "foo\nbar\rbaz\n",
598
+ "abc\rdef\rghi\r",
599
+ "über",
600
+ ].each do |input|
601
+ [0, 1, 2, 3, 4, 10].each do |limit|
602
+ matches_reference_io_behaviour(input) { |io| io.gets(nil, limit) }
603
+ end
604
+ end
578
605
  end
579
606
 
580
607
  it 'supports `gets` with a separator and a limit' do
@@ -586,15 +613,14 @@ describe FilterIO do
586
613
  "über",
587
614
  ].each do |input|
588
615
  ["\r", "x"].each do |sep_string|
589
- [1, 2, 3, 4, 10].each do |limit|
616
+ [0, 1, 2, 3, 4, 10].each do |limit|
590
617
  matches_reference_io_behaviour(input) { |io| io.gets(sep_string, limit) }
591
618
  end
592
619
  end
593
620
  end
594
- # TODO: test zero limit
595
621
  end
596
622
 
597
- it 'errors when `get` is passed more than two args' do
623
+ it 'errors when `gets` is passed more than two args' do
598
624
  expect {
599
625
  FilterIO.new(StringIO.new).gets(1,2,3)
600
626
  }.to raise_error ArgumentError
@@ -821,11 +847,6 @@ describe FilterIO do
821
847
  expect {
822
848
  filtered_io.gets
823
849
  }.to raise_error IOError
824
-
825
- # closing again should raise an error
826
- expect {
827
- filtered_io.close
828
- }.to raise_error IOError
829
850
  end
830
851
  end
831
852
 
@@ -955,5 +976,4 @@ describe FilterIO do
955
976
 
956
977
  expect(dest.string).to eq 'FOO'
957
978
  end
958
-
959
979
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filter_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Weathered
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2022-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.13'
61
+ version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '2.13'
68
+ version: '3.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description:
84
84
  email:
85
85
  - jason@jasoncodes.com
86
86
  executables: []
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
+ - ".tool-versions"
91
92
  - Gemfile
92
93
  - LICENSE
93
94
  - README.markdown
@@ -101,7 +102,7 @@ homepage: http://github.com/jasoncodes/filter_io
101
102
  licenses:
102
103
  - MIT
103
104
  metadata: {}
104
- post_install_message:
105
+ post_install_message:
105
106
  rdoc_options: []
106
107
  require_paths:
107
108
  - lib
@@ -116,9 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
117
  - !ruby/object:Gem::Version
117
118
  version: '0'
118
119
  requirements: []
119
- rubyforge_project:
120
- rubygems_version: 2.2.2
121
- signing_key:
120
+ rubygems_version: 3.2.32
121
+ signing_key:
122
122
  specification_version: 4
123
123
  summary: Filter IO streams with a block. Ruby's FilterInputStream.
124
124
  test_files: