Module: Spotify::UTF8String

Extended by:
FFI::DataConverter
Defined in:
lib/spotify/data_converters/utf8_string.rb

Overview

A UTF-8 FFI type, making sure all strings are UTF8 in and out.

Given an ingoing (ruby to C) string, it will make sure the string is in UTF-8 encoding. An outgoing (C to Ruby) will be assumed to actually be in UTF-8, and force-encoded as such.

Class Method Summary collapse

Class Method Details

.from_native(value, ctx) ⇒ String

Note:

NO error checking is made, the string is just forced to UTF-8

Given an original string, assume it is in UTF-8.

Parameters:

  • value (String)

    can be in any encoding

  • ctx

Returns:

  • (String)

    value, but with UTF-8 encoding



30
31
32
# File 'lib/spotify/data_converters/utf8_string.rb', line 30

def from_native(value, ctx)
  value && value.force_encoding(Encoding::UTF_8)
end

.to_native(value, ctx) ⇒ String

Note:

if the value is already in UTF-8, ruby does nothing

Note:

if the given value is falsy, default behaviour is used

Given a value, encodes it to UTF-8 no matter what.

Parameters:

  • value (String, nil)
  • ctx

Returns:

  • (String)

    value, but in UTF-8 if it wasn’t already



20
21
22
# File 'lib/spotify/data_converters/utf8_string.rb', line 20

def to_native(value, ctx)
  value && value.encode(Encoding::UTF_8)
end