6.6. kink/CHARSET¶
6.6.1. type charset¶
`charset` is a system for encoding strings into byte sequences.
6.6.1.1. Charset.name¶
`name` returns the name of the charset as a `str`.
The result might not be equal to the name specified for CHARSET.for.
Example
:CHARSET.require_from('kink/')
:Utf16 <- CHARSET.for('utf16')
stdout.print_line(Utf16.name.repr) # => "UTF-16"
6.6.1.2. Charset.str_to_bin(Str)¶
`str_to_bin` converts `Str` to a `bin`.
If you want to specify how conversion errors are handled, use kink/io/OUTPUT_PRINTER and kink/io/BIN_OUTPUT.
Precondition
`Str` must be a `str`.
`Str` must be able to encode by `Charset`.
Example
:CHARSET.require_from('kink/')
:Bin <- CHARSET.utf8.str_to_bin('Foo')
stdout.print_line(Bin.repr) # => (bin 0x46 0x6f 0x6f)
6.6.1.3. Charset.bin_to_str(Bin)¶
`bin_to_str` converts `Bin` to a `str`.
If you want to specify how conversion errors are handled, use kink/io/INPUT_SCANNER and kink/io/BIN_INPUT.
Precondition
`Bin` must be a `bin`.
`Bin` must be able to decode by `Charset` into a Unicode string.
Example
:CHARSET.require_from('kink/')
:BIN.require_from('kink/')
:Str <- CHARSET.utf8.bin_to_str(BIN.of(0x46 0x6f 0x6f))
stdout.print_line(Str.repr) # => "Foo"
6.6.1.4. X == Y¶
`==` operator, or `op_eq` method, returns whether two charsets have the same name.
Precondition
`Y` must be a `charset`.
6.6.2. CHARSET.for(Name ...[$config={}])¶
`for` tries to find a charset with the specified `Name`, from the charset repository provided by the runtime.
Config methods:
• C.on_success($success): default = VAL.identity.
• C.on_error($error): default = a fun which raises an exception.
If the specified charset is found, `for` tail-calls $success with the `charset`.
If the specified charset is not found, `for` tail-calls $error with no arg.
Preconditions
`Name` must be a `str`.
$success must be a fun which takes a `charset`.
$error must be a fun which takes no arg.
6.6.3. CHARSET.is?(Val)¶
`is?` returns whether `Val` is a `charset`.
6.6.4. CHARSET.utf8¶
`utf8` returns UTF-8 `charset`.
6.6.5. CHARSET.ascii¶
`ascii` returns ASCII `charset`.
6.6.6. CHARSET.default¶
`default` returns the `charset` default to the runtime.