Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/raptor-io/ruby/string.rb
Instance Method Summary collapse
-
#binary? ⇒ Bool
‘true` if `self` is binary, `false` if regular text.
-
#force_utf8 ⇒ String
Copy of ‘self`, forced to UTF-8.
-
#force_utf8! ⇒ Object
Forces ‘self` to UTF-8 and replaces invalid characters.
-
#repack ⇒ String
‘self` with 8-bit unsigned characters.
Instance Method Details
#binary? ⇒ Bool
Returns ‘true` if `self` is binary, `false` if regular text.
21 22 23 24 25 |
# File 'lib/raptor-io/ruby/string.rb', line 21 def binary? encoding == Encoding::ASCII_8BIT || index( "\x00" ) || count( "\x00-\x7F", "^ -~\t\r\n").fdiv( length ) > 0.3 end |
#force_utf8 ⇒ String
Returns Copy of ‘self`, forced to UTF-8.
15 16 17 |
# File 'lib/raptor-io/ruby/string.rb', line 15 def force_utf8 dup.force_utf8! end |
#force_utf8! ⇒ Object
Forces ‘self` to UTF-8 and replaces invalid characters.
9 10 11 12 |
# File 'lib/raptor-io/ruby/string.rb', line 9 def force_utf8! force_encoding( 'utf-8' ) encode!( 'utf-16be', invalid: :replace, undef: :replace ).encode( 'utf-8' ) end |
#repack ⇒ String
Returns ‘self` with 8-bit unsigned characters.
4 5 6 |
# File 'lib/raptor-io/ruby/string.rb', line 4 def repack unpack( 'C*' ).pack( 'C*' ) end |