Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor-io/ruby/string.rb

Instance Method Summary collapse

Instance Method Details

#binary?Bool

Returns ‘true` if `self` is binary, `false` if regular text.

Returns:

  • (Bool)

    ‘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_utf8String

Returns Copy of ‘self`, forced to UTF-8.

Returns:



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

#repackString

Returns ‘self` with 8-bit unsigned characters.

Returns:

  • (String)

    ‘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