4.27. kink/charset/CHARSET

Provides charset type, which represents character encoding.

4.27.1. CHARSET.for(Name ...[$config])

CHARSET.for finds a charset with the specified Name.

The fun searches the repository of charsets provided by the runtime.

Preconditions:

• Name must be a str

• $config, if specified, must be a fun which takes a conf val as the only arg.

A conf val has the following methods:

• C.on_present($present_cont): specifies the present cont of CHARSET.for. If not called, VAL.identity is used as the default present cont.

• C.on_absent($absent_cont): specifies the absent cont of CHARSET.for. If not called, a fun which raises an exception is used as the default absent cont.

Result:

• If the specified charset is found, CHARSET.for tail-calls the present cont with the charset.

• If the specified charset is not found, CHARSET.for tail-calls the absent cont with no args.

4.27.2. type charset

A charset is an encoding system of a string by a byte sequence (bin).

4.27.2.1. Charset.name

Charset.name returns the name of the charset.

There is no guarantee that the name specfied to CHARSET.for is equal to the result of the method.

For example:

:CHARSET.require_from('kink/charset/')

:Utf16 <- CHARSET.for('utf16')
stdout.print_line(Utf16.name.repr) # => "UTF-16"

4.27.2.2. Charset.str_to_bin(Str)

“str_to_bin” converts the Str to a bin.

Precondition:

• Str must be a str.

Example:

:CHARSET.require_from('kink/charset/')

:Bin <- CHARSET.utf8.str_to_bin('Foo')
stdout.print_line(Bin.repr) # => BIN.of(0x46 0x6f 0x6f)

If a portion of the str cannot be converted by the charset, it is replaced by the replacement sequence default to the charset.

4.27.2.3. Charset.bin_to_str(Bin)

“bin_to_str” converts the Bin to a str.

Precondition:

• Bin must be a bin.

Example:

:CHARSET.require_from('kink/charset/')
:BIN.require_from('kink/')

:Str <- CHARSET.utf8.bin_to_str(BIN.of(0x46 0x6f 0x6f))
stdout.print_line(Str.repr) # => "Foo"

If a portion of the bin cannot be converted by the charset, it is replaced by the replacement sequence default to the charset.

4.27.2.4. X == Y, X != Y

Two charsets are equal when the names are equal.

4.27.2.5. Charset.repr

Charset.repr returns a str which represents the charset, such as "(charset UTF-16)".

4.27.3. CHARSET.is?(Val)

CHARSET.is? returns whether Val is a charset.

4.27.4. CHARSET.utf8

CHARSET.utf8 returns UTF-8 charset.

4.27.5. CHARSET.ascii

CHARSET.ascii returns ASCII charset.

4.27.6. CHARSET.default

CHARSET.default returns the charset default to the runtime.