lib/io_streams/encode/reader.rb in iostreams-1.0.0.beta vs lib/io_streams/encode/reader.rb in iostreams-1.0.0.beta2

- old
+ new

@@ -5,11 +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 } }.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) @@ -44,10 +46,11 @@ 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 @@ -73,10 +76,10 @@ # EOF reached? return unless block block = block.encode(@encoding, @encoding_options) unless block.encoding == @encoding - block = @cleaner.call(block) if @cleaner + block = @cleaner.call(block, @replace) if @cleaner block end private