immutable dstring asciiTable = iota!dchar('\x00', '\x80').array
American Standard Code for Information Interchange, 128 bit encoding
AsciiBasedString encodeFromUTF32(AsciiBasedString)(dstring s, bool safe = false)
if (is(AsciiBasedString == KOI8RString) || is(AsciiBasedString == KOI8UString))
Gets encoded Amalthea ASCII based string from dstring (UTF-32).
Parameters
| s | The UTF32-string for transcoding. |
| safe | If false, the input has to be valid to avoid mistakes, if true, inappropriate characters will be replaced with '?'. |
Example
dstring russianText = "Привет, мир!"d;
KOI8RString koi8rText = encodeFromUTF32!KOI8RString(russianText);
ubyte[] expected = [
0xf0, 0xd2, 0xc9, 0xd7, 0xc5, 0xd4, 0x2c, 0x20, 0xcd, 0xc9, 0xd2, 0x21
];
assert(cast(ubyte[])koi8rText == expected);
AsciiBasedString safeEncodeFromUTF32(AsciiBasedString)(dstring s)
Encodes dstring to KOI8RString or KOI8UString. The input does not have to be valid.
Parameters
| s | The UTF32-string for transcoding. |
Example
dstring invalidText = "你好,世界!"d;
KOI8RString koi8rText = safeEncodeFromUTF32!KOI8RString(invalidText);
assert(cast(ubyte[])koi8rText == ['?', '?', '?', '?', '?', '?']);
string decodeByEncodingName(const ubyte[] s, string encodingName)
Decodes to UTF-8 string from byte representaion by encoding name.
ubyte[] encodeByEncodingName(string s, string encodingName)
Decodes to any type string from UTF-8 representaion by encoding name.
ubyte[] encodeText(T)(const T[] seq, string encSrc, string encDst)
The function tries to encode text sequence to new encoding. The convertation is based on libiconv. The list is available with 'iconv --list'.
Homepage of libiconv: https://www.gnu.org/software/libiconv/
Parameters
| seq | Array of characters (string, dstring, KOI8RString, ubyte[], etc.). |
| encSrc | The start encoding of the transmitted sequence. |
| encDst | The destination encoding for the returned value. |
Example
string text = "Привет, мир!";
ubyte[] koi8rText = encodeText(text, "utf-8", "koi8-r");
ubyte[] expected = [
0xf0, 0xd2, 0xc9, 0xd7, 0xc5, 0xd4, 0x2c, 0x20, 0xcd, 0xc9, 0xd2, 0x21
];
assert(koi8rText == expected);
text = "你好,世界!";
bool error = false;
try {
koi8rText = encodeText(text, "utf-8", "koi8-r");
} catch (EncodingException e) {
error = true;
}
assert(error);
Public imports
Enums
| KOI8RChar | |
| KOI8UChar |
Variables
| asciiTable |
Functions
| encodeFromUTF32 | transcode |
| safeEncodeFromUTF32 | decodeByEncodingName |
| transcode | encodeByEncodingName |
| transcode | getEncodingNameByType |
| transcode | encodeText |
Structs
| UniString |
Classes
| AsciiBasedEncoding | |
| EncodingSchemeKOI8R | |
| EncodingSchemeKOI8U |
Templates
| ConversionInjection |
This module provides an implementation of the KOI8R and KOI8U encodings (compatible with std.encoding.EncodingScheme), and the encodeText() function (based on libiconv) to translate text from any encoding to any other encoding.