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
-
.from_native(value, ctx) ⇒ String
Given an original string, assume it is in UTF-8.
-
.to_native(value, ctx) ⇒ String
Given a value, encodes it to UTF-8 no matter what.
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.
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.
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 |