Core\Charset
Names the character encoding of some text, so a conversion knows which encoding to use.
Text inside a Novis program is always UTF-8. Text from outside may use another encoding, for example a file from an old spreadsheet program. Core\Charset has one case for each encoding that web browsers support, such as Utf8, Latin1, Windows1252, ShiftJis and Gbk. You give the case to Core\Encoding::decodeText to read such bytes as text. You give it to Core\Encoding::encodeText to write your text as such bytes.
Good to know: a conversion is exact, or it throws an error. No Core\Charset value replaces the characters that do not fit. When the encoding has no way to write a character, the conversion throws an error. The message names that character.
Examples
Reading text that is not utf 8
<?nvs// A file arrives from an old system. Its bytes are not UTF-8, so reading them// as text needs the name of the encoding they were written in.
// The old system writes its files in Latin-1. This line makes such a file.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'export.txt');Core\IO::write($path, Core\Encoding::encodeText('Café', Core\Charset::Latin1));
// `Core\IO::readBytes` reads the file exactly as it is on disk.bytes $arrived = Core\IO::readBytes($path);
// Read the bytes as Latin-1. You get ordinary text back, and this prints `Café`.echo Core\Encoding::decodeText($arrived, Core\Charset::Latin1), "\n";
// The same bytes are not UTF-8. So reading the file as UTF-8 text throws an error.try { echo Core\IO::read($path), "\n";} catch (Throwable $wrong) { echo $wrong->message, "\n";}CaféCore\IO::read: the bytes read are not valid UTF-8 from byte 3, so they are not a `string`. Use `Core\IO::readBytes` to read the file as bytes, or `Core\IO::readText` to decode it.Writing text an old system can read
<?nvs// This program converts text to an older encoding, for a system that cannot read UTF-8.
string $name = 'Café Größenwahn';
// `encodeText` converts the text to Windows-1252 bytes. Each accented letter is// one byte in Windows-1252. In UTF-8 it is two bytes.bytes $written = Core\Encoding::encodeText($name, Core\Charset::Windows1252);echo Core\Encoding::toHex($written), "\n";
// A character that Windows-1252 cannot write throws an error. The message// names the character and its position in the text.try { echo Core\Encoding::toHex(Core\Encoding::encodeText('Crab 🦀', Core\Charset::Windows1252)), "\n";} catch (Throwable $missing) { echo $missing->message, "\n";}436166e9204772f6df656e7761686eCore\Encoding::encodeText(): `Core\Charset::Windows1252` has no spelling for '🦀' (U+1F980) at offset 5 of "Crab 🦀" — it is exact or it throws, and there is no transliterating or ignoring modeWhich encoding do these bytes fit
<?nvs// `isValidText` checks if some bytes are valid text in an encoding. You can// check an uploaded file this way before you read it as text.
bytes $uploaded = Core\Encoding::fromHex('436166e9');
echo 'utf-8: ', Core\Encoding::isValidText($uploaded, Core\Charset::Utf8) ? 'yes' : 'no', "\n";echo 'windows-1252: ', Core\Encoding::isValidText($uploaded, Core\Charset::Windows1252) ? 'yes' : 'no', "\n";echo 'koi8-r: ', Core\Encoding::isValidText($uploaded, Core\Charset::Koi8R) ? 'yes' : 'no', "\n";
// `yes` does not prove the encoding. Many older encodings accept every byte,// so the same bytes give different text in each one, as these two lines show.echo Core\Encoding::decodeText($uploaded, Core\Charset::Windows1252), "\n";echo Core\Encoding::decodeText($uploaded, Core\Charset::Koi8R), "\n";utf-8: nowindows-1252: yeskoi8-r: yesCaféCafИ