Class: RSpec::Support::EncodedString

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/support/encoded_string.rb

Constant Summary collapse

UTF_8 =

Reduce allocations by storing constants.

"UTF-8"
US_ASCII =
"US-ASCII"
REPLACE =

Ruby’s default replacement string is:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, encoding = nil) ⇒ EncodedString

Returns a new instance of EncodedString.



16
17
18
19
20
# File 'lib/rspec/support/encoded_string.rb', line 16

def initialize(string, encoding=nil)
  @encoding = encoding
  @source_encoding = detect_source_encoding(string)
  @string = matching_encoding(string)
end

Instance Attribute Details

#source_encodingObject (readonly)

Returns the value of attribute source_encoding.



21
22
23
# File 'lib/rspec/support/encoded_string.rb', line 21

def source_encoding
  @source_encoding
end

Class Method Details

.pick_encoding(_source_a, _source_b) ⇒ Object



143
144
145
# File 'lib/rspec/support/encoded_string.rb', line 143

def self.pick_encoding(source_a, source_b)
  Encoding.compatible?(source_a, source_b) || Encoding.default_external
end

Instance Method Details

#<<(string) ⇒ Object



28
29
30
# File 'lib/rspec/support/encoded_string.rb', line 28

def <<(string)
  @string << matching_encoding(string)
end

#split(regex_or_string) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rspec/support/encoded_string.rb', line 33

def split(regex_or_string)
  @string.split(matching_encoding(regex_or_string))
rescue ArgumentError
  # JRuby raises an ArgumentError when splitting a source string that
  # contains invalid bytes.
  remove_invalid_bytes(@string).split regex_or_string
end

#to_sObject Also known as: to_str



46
47
48
# File 'lib/rspec/support/encoded_string.rb', line 46

def to_s
  @string
end