Class: EncodedString
- Inherits:
-
Object
- Object
- EncodedString
- Defined in:
- lib/encoded_string.rb,
lib/encoded_string/version.rb
Constant Summary collapse
- UTF_8 =
Reduce allocations by storing constants.
"UTF-8"- US_ASCII =
"US-ASCII"- 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_aOn 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") "?"- ENCODE_UNCONVERTABLE_BYTES =
{ :invalid => :replace, :undef => :replace, :replace => REPLACE }
- ENCODE_NO_CONVERTER =
{ :invalid => :replace, :replace => REPLACE }
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#source_encoding ⇒ Object
readonly
Returns the value of attribute source_encoding.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(string) ⇒ Object
-
#initialize(string, encoding = nil) ⇒ EncodedString
constructor
A new instance of EncodedString.
- #split(regex_or_string) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(string, encoding = nil) ⇒ EncodedString
Returns a new instance of EncodedString.
31 32 33 34 35 |
# File 'lib/encoded_string.rb', line 31 def initialize(string, encoding=nil) @encoding = encoding @source_encoding = detect_source_encoding(string) @string = matching_encoding(string) end |
Instance Attribute Details
#source_encoding ⇒ Object (readonly)
Returns the value of attribute source_encoding.
36 37 38 |
# File 'lib/encoded_string.rb', line 36 def source_encoding @source_encoding end |
Class Method Details
.pick_encoding(_source_a, _source_b) ⇒ Object
132 133 134 |
# File 'lib/encoded_string.rb', line 132 def self.pick_encoding(source_a, source_b) Encoding.compatible?(source_a, source_b) || Encoding.default_external end |
Instance Method Details
#<<(string) ⇒ Object
43 44 45 |
# File 'lib/encoded_string.rb', line 43 def <<(string) @string << matching_encoding(string) end |
#split(regex_or_string) ⇒ Object
47 48 49 |
# File 'lib/encoded_string.rb', line 47 def split(regex_or_string) @string.split(matching_encoding(regex_or_string)) end |
#to_s ⇒ Object Also known as: to_str
51 52 53 |
# File 'lib/encoded_string.rb', line 51 def to_s @string end |