Class: RSpec::Support::EncodedString

### Home Classes Methods

### In Files

- lib/rspec/support/encoded_string.rb

### Parent

Object

### Methods

- ::new
- ::pick_encoding
- #<<
- #detect_source_encoding
- #matching_encoding
- #remove_invalid_bytes
- #split
- #to_s
- #to_str

### Class/Module Index

Quicksearch

- RSpec
- RSpec::CallerFilter
- RSpec::Support
- RSpec::Support::BlockSignature
- RSpec::Support::Differ
- RSpec::Support::DirectoryMaker
- RSpec::Support::EncodedString
- RSpec::Support::FormattingSupport
- RSpec::Support::FuzzyMatcher
- RSpec::Support::HunkGenerator
- RSpec::Support::InSubProcess
- RSpec::Support::LooseSignatureVerifier
- RSpec::Support::LooseSignatureVerifier::SignatureWithKeywordArgumentsMatcher
- RSpec::Support::MethodSignature
- RSpec::Support::MethodSignatureVerifier
- RSpec::Support::OS
- RSpec::Support::ObjectFormatter
- RSpec::Support::RecursiveConstMethods
- RSpec::Support::Ruby
- RSpec::Support::RubyFeatures
- RSpec::Support::ShellOut
- RSpec::Support::Spec
- RSpec::Support::StdErrSplitter
- RSpec::Support::StrictSignatureVerifier
- RSpec::Support::Version
- RSpec::Support::VersionChecker
- RSpec::Support::Warnings
- RSpec::Support::WithIsolatedStdErr
- Object
- RSpecHelpers

No matching classes.

# RSpec::Support::EncodedString

@private

### Constants

ENCODE_NO_CONVERTER
ENCODE_UNCONVERTABLE_BYTES
REPLACE

  In MRI 2.1 'invalid: :replace' changed to also replace an invalid byte sequence see github.com/ruby/ruby/blob/v2_1_0/NEWS#L176 www.ruby-forum.com/topic/6861247 twitter.com/nalsh/status/553413844685438976

  For example, given:

      "\x80".force_encoding("Emacs-Mule").encode(:invalid => :replace).bytes.to_a

  On MRI 2.1 or above: 63 # '?' else : 128 # "x80"

  Ruby's default replacement string is:

      U+FFFD ("\xEF\xBF\xBD"), for Unicode encoding forms, else
      ?      ("\x3F")

US_ASCII
UTF_8

  Reduce allocations by storing constants.

### Attributes

source_encoding[R]

### Public Class Methods

new(string, encoding=nil) click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 34
    def initialize(string, encoding=nil)
      @encoding = encoding
      @source_encoding = detect_source_encoding(string)
      @string = matching_encoding(string)
    end

pick_encoding(source_a, source_b) click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 135
    def self.pick_encoding(source_a, source_b)
      Encoding.compatible?(source_a, source_b) || Encoding.default_external
    end

### Public Instance Methods

<<(string) click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 46
    def <<(string)
      @string << matching_encoding(string)
    end

detect_source_encoding(string) click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 131
    def detect_source_encoding(string)
      string.encoding
    end

matching_encoding(string) click to toggle source

Encoding Exceptions:

Raised by Encoding and String methods:

    Encoding::UndefinedConversionError:
      when a transcoding operation fails
      if the String contains characters invalid for the target encoding
      e.g. "\x80".encode('UTF-8','ASCII-8BIT')
      vs "\x80".encode('UTF-8','ASCII-8BIT', undef: :replace, replace: '<undef>')
      # => '<undef>'
    Encoding::CompatibilityError
      when Encoding.compatibile?(str1, str2) is nil
      e.g. utf_16le_emoji_string.split("\n")
      e.g. valid_unicode_string.encode(utf8_encoding) << ascii_string
    Encoding::InvalidByteSequenceError:
      when the string being transcoded contains a byte invalid for
      either the source or target encoding
      e.g. "\x80".encode('UTF-8','US-ASCII')
      vs "\x80".encode('UTF-8','US-ASCII', invalid: :replace, replace: '<byte>')
      # => '<byte>'
    ArgumentError
      when operating on a string with invalid bytes
      e.g."\x80".split("\n")
    TypeError
      when a symbol is passed as an encoding
      Encoding.find(:"UTF-8")
      when calling force_encoding on an object
      that doesn't respond to #to_str

Raised by transcoding methods:

    Encoding::ConverterNotFoundError:
      when a named encoding does not correspond with a known converter
      e.g. 'abc'.force_encoding('UTF-8').encode('foo')
      or a converter path cannot be found
      e.g. "\x80".force_encoding('ASCII-8BIT').encode('Emacs-Mule')

Raised by byte <-> char conversions

    RangeError: out of char range
      e.g. the UTF-16LE emoji: 128169.chr

    # File lib/rspec/support/encoded_string.rb, line 101
    def matching_encoding(string)
      string = remove_invalid_bytes(string)
      string.encode(@encoding)
    rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
      string.encode(@encoding, ENCODE_UNCONVERTABLE_BYTES)
    rescue Encoding::ConverterNotFoundError
      string.dup.force_encoding(@encoding).encode(ENCODE_NO_CONVERTER)
    end

remove_invalid_bytes(string) click to toggle source

github.com/ruby/ruby/blob/eeb05e8c11/doc/NEWS-2.1.0#L120-L123 github.com/ruby/ruby/blob/v2_1_0/string.c#L8242 github.com/hsbt/string-scrub github.com/rubinius/rubinius/blob/v2.5.2/kernel/common/string.rb#L1913-L1972

    # File lib/rspec/support/encoded_string.rb, line 116
    def remove_invalid_bytes(string)
      string.scrub(REPLACE)
    end

split(regex_or_string) click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 50
    def split(regex_or_string)
      @string.split(matching_encoding(regex_or_string))
    end

to_s() click to toggle source

    # File lib/rspec/support/encoded_string.rb, line 54
    def to_s
      @string
    end

Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

[Validate]

Generated with the Darkfish Rdoc Generator 2.