Sha256: eb4a4c04a1db8feba55f7a693864ee5d3454d1e5d8d5b6db1ab97d7b8e199bea
Contents?: true
Size: 1.75 KB
Versions: 29
Compression:
Stored size: 1.75 KB
Contents
module RSpec module Support # @private class EncodedString MRI_UNICODE_UNKOWN_CHARACTER = "\xEF\xBF\xBD" def initialize(string, encoding=nil) @encoding = encoding @source_encoding = detect_source_encoding(string) @string = matching_encoding(string) end attr_reader :source_encoding delegated_methods = String.instance_methods.map(&:to_s) & %w[eql? lines == encoding empty?] delegated_methods.each do |name| define_method(name) { |*args, &block| @string.__send__(name, *args, &block) } end def <<(string) @string << matching_encoding(string) end def split(regex_or_string) @string.split(matching_encoding(regex_or_string)) end def to_s @string end alias :to_str :to_s if String.method_defined?(:encoding) private def matching_encoding(string) string.encode(@encoding) rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError normalize_missing(string.encode(@encoding, :invalid => :replace, :undef => :replace)) rescue Encoding::ConverterNotFoundError normalize_missing(string.force_encoding(@encoding).encode(:invalid => :replace)) end def normalize_missing(string) if @encoding.to_s == "UTF-8" string.gsub(MRI_UNICODE_UNKOWN_CHARACTER.force_encoding(@encoding), "?") else string end end def detect_source_encoding(string) string.encoding end else private def matching_encoding(string) string end def detect_source_encoding(_string) 'US-ASCII' end end end end end
Version data entries
29 entries across 26 versions & 8 rubygems