-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add changes for suppressing output when binary output is encountered
- Loading branch information
Showing
4 changed files
with
107 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Rf | ||
class BufferedIO | ||
BUFFER_SIZE = 96 * 1024 # 96KB | ||
|
||
def initialize(file_name, mode = 'r') | ||
@file = file_name == '-' ? $stdin : File.open(file_name, mode) | ||
@buffer = '' | ||
@end_of_file = false | ||
@binary = false | ||
end | ||
|
||
def binary? | ||
@binary | ||
end | ||
|
||
def gets | ||
return if @end_of_file | ||
|
||
line = '' | ||
loop do | ||
fill_buffer if @buffer.empty? | ||
break if @buffer.empty? | ||
|
||
newline_index = @buffer.index("\n") | ||
if newline_index | ||
line << @buffer.slice!(0..newline_index) | ||
break | ||
else | ||
line << @buffer.slice!(0..-1) | ||
@buffer.clear | ||
end | ||
end | ||
|
||
line.empty? ? nil : line | ||
end | ||
|
||
def read | ||
return if @end_of_file | ||
|
||
fill_buffer(nil) | ||
@buffer.empty? ? nil : @buffer.slice!(0..-1) | ||
end | ||
|
||
def close | ||
@file.close | ||
end | ||
|
||
private | ||
|
||
def fill_buffer(size = BUFFER_SIZE) | ||
read_data = @file.read(size) | ||
if read_data.nil? || read_data.empty? | ||
@end_of_file = true | ||
else | ||
@binary = true if /(?![\r\n])\p{Cntrl}/.match?(read_data) | ||
|
||
@buffer << read_data | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe 'Binary file match' do | ||
context 'when input is from stdin' do | ||
let(:input) { "hello\x00world\n" } | ||
let(:args) { '_' } | ||
let(:expect_output) { 'Binary file matches.' } | ||
|
||
it_behaves_like 'a successful exec' | ||
end | ||
|
||
context 'when input is from file' do | ||
let(:file) { 'binary_file' } | ||
let(:args) { "_ #{file}" } | ||
let(:expect_output) { 'Binary file matches.' } | ||
|
||
before do | ||
write_file(file, "hello\x00world\n") | ||
end | ||
|
||
it_behaves_like 'a successful exec' | ||
end | ||
end |