lib/expectr.rb in expectr-1.0.0 vs lib/expectr.rb in expectr-1.0.1

- old
+ new

@@ -58,11 +58,11 @@ # (default: 8192) # :constrain - Whether to constrain the internal buffer from the # sub-process to :buffer_size (default: false) def initialize(cmd, args={}) unless cmd.kind_of? String or cmd.kind_of? File - raise ArgumentError, "String or File expected" + raise ArgumentError, "String or File expected" end cmd = cmd.path if cmd.kind_of? File @buffer = ''.encode("UTF-8") @@ -89,10 +89,11 @@ rescue Errno::EIO #Application went away. @pid = 0 break end + force_utf8(buf) unless buf.valid_encoding? print_buffer(buf) @out_mutex.synchronize do @buffer << buf if @buffer.length > @buffer_size && @constrain @@ -282,6 +283,16 @@ # Returns nothing. def print_buffer(buf) print buf if @flush_buffer STDOUT.flush unless STDOUT.sync end + + # Internal: Encode a String twice to force UTF-8 encoding, dropping + # problematic characters in the process. + # + # buf - String to be encoded. + # + # Returns the encoded String. + def force_utf8(buf) + buf.force_encoding('ISO-8859-1').encode('UTF-8', 'UTF-8', replace: nil) + end end