lib/io_streams/encode/reader.rb in iostreams-1.1.0 vs lib/io_streams/encode/reader.rb in iostreams-1.1.1
- old
+ new
@@ -5,13 +5,13 @@
NOT_PRINTABLE = Regexp.compile(/[^[:print:]|\r|\n]/).freeze
# Builtin strip options to apply after encoding the read data.
CLEANSE_RULES = {
# Strips all non printable characters
- printable: ->(data, _) { data.gsub!(NOT_PRINTABLE, '') || data },
+ printable: ->(data, _) { data.gsub!(NOT_PRINTABLE, "") || data },
# Replaces non printable characters with the value specified in the `replace` option.
- replace_non_printable: ->(data, replace) { data.gsub!(NOT_PRINTABLE, replace || '') || data }
+ replace_non_printable: ->(data, replace) { data.gsub!(NOT_PRINTABLE, replace || "") || data }
}.freeze
# Read a line at a time from a file or stream
def self.stream(input_stream, original_file_name: nil, **args)
yield new(input_stream, **args)
@@ -40,24 +40,20 @@
# Cleanse data read from the input stream.
# nil: No cleansing
# :printable Cleanse all non-printable characters except \r and \n
# Proc/lambda Proc to call after every read to cleanse the data
# Default: nil
- def initialize(input_stream, encoding: 'UTF-8', cleaner: nil, replace: nil)
+ def initialize(input_stream, encoding: "UTF-8", cleaner: nil, replace: nil)
super(input_stream)
@cleaner = self.class.extract_cleaner(cleaner)
@encoding = encoding.nil? || encoding.is_a?(Encoding) ? encoding : Encoding.find(encoding)
@encoding_options = replace.nil? ? {} : {invalid: :replace, undef: :replace, replace: replace}
@replace = replace
# More efficient read buffering only supported when the input stream `#read` method supports it.
- if replace.nil? && !@input_stream.method(:read).arity.between?(0, 1)
- @read_cache_buffer = ''.encode(@encoding)
- else
- @read_cache_buffer = nil
- end
+ @read_cache_buffer = ("".encode(@encoding) if replace.nil? && !@input_stream.method(:read).arity.between?(0, 1))
end
# Returns [String] data returned from the input stream.
# Returns [nil] if end of file and no further data was read.
def read(size = nil)
@@ -89,9 +85,10 @@
case cleaner
when Symbol
proc = CLEANSE_RULES[cleaner]
raise(ArgumentError, "Invalid cleansing rule #{cleaner.inspect}") unless proc
+
proc
when Proc
cleaner
end
end