std.encoding
- The type AsciiChar represents an ASCII character.
- The type AsciiString represents an ASCII string.
- The type Latin1Char represents an ISO-8859-1 character.
- The type Latin1String represents an ISO-8859-1 string.
- The type Latin2Char represents an ISO-8859-2 character.
- The type Latin2String represents an ISO-8859-2 string.
- The type Windows1250Char represents a Windows-1250 character.
- The type Windows1250String represents a Windows-1250 string.
- The type Windows1252Char represents a Windows-1252 character.
- The type Windows1252String represents a Windows-1252 string.
auto e = EncodingScheme.create("utf-8");This library supplies EncodingScheme subclasses for ASCII, ISO-8859-1 (also known as LATIN-1), ISO-8859-2 (LATIN-2), WINDOWS-1250, WINDOWS-1252, UTF-8, and (on little-endian architectures) UTF-16LE and UTF-32LE; or (on big-endian architectures) UTF-16BE and UTF-32BE. This library provides a mechanism whereby other modules may add EncodingScheme subclasses for any other encoding.
Source: std/encoding.d
- Special value returned by safeDecode
- Defines various character sets.
- Defines an Latin1-encoded character.
- Defines an Latin1-encoded string (as an array of immutable(Latin1Char)).
- Defines a Latin2-encoded character.
- Defines an Latin2-encoded string (as an array of immutable(Latin2Char)).
- Defines a Windows1250-encoded character.
- Defines an Windows1250-encoded string (as an array of immutable(Windows1250Char)).
- Defines a Windows1252-encoded character.
- Defines an Windows1252-encoded string (as an array of immutable(Windows1252Char)).
- Returns true if c is a valid code pointNote that this includes the non-character code points U+FFFE and U+FFFF, since these are valid code points (even though they are not valid characters).
Supersedes: This function supersedes std.utf.startsValidDchar().
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:dchar c the code point to be tested - Returns the name of an encoding.The type of encoding cannot be deduced. Therefore, it is necessary to explicitly specify the encoding type.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Examples:
assert(encodingName!(char) == "UTF-8"); assert(encodingName!(wchar) == "UTF-16"); assert(encodingName!(dchar) == "UTF-32"); assert(encodingName!(AsciiChar) == "ASCII"); assert(encodingName!(Latin1Char) == "ISO-8859-1"); assert(encodingName!(Latin2Char) == "ISO-8859-2"); assert(encodingName!(Windows1250Char) == "windows-1250"); assert(encodingName!(Windows1252Char) == "windows-1252");
- Returns true iff it is possible to represent the specified codepoint in the encoding.The type of encoding cannot be deduced. Therefore, it is necessary to explicitly specify the encoding type.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Examples:
assert( canEncode!(Latin1Char)('A')); assert( canEncode!(Latin2Char)('A')); assert(!canEncode!(AsciiChar)('\u00A0')); assert( canEncode!(Latin1Char)('\u00A0')); assert( canEncode!(Latin2Char)('\u00A0')); assert( canEncode!(Windows1250Char)('\u20AC')); assert(!canEncode!(Windows1250Char)('\u20AD')); assert(!canEncode!(Windows1250Char)('\uFFFD')); assert( canEncode!(Windows1252Char)('\u20AC')); assert(!canEncode!(Windows1252Char)('\u20AD')); assert(!canEncode!(Windows1252Char)('\uFFFD')); assert(!canEncode!(char)(cast(dchar)0x110000));
- Returns true if the code unit is legal. For example, the byte 0x80 would not be legal in ASCII, because ASCII code units must always be in the range 0x00 to 0x7F.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
E c the code unit to be tested Examples:assert(!isValidCodeUnit(cast(char)0xC0)); assert(!isValidCodeUnit(cast(char)0xFF)); assert( isValidCodeUnit(cast(wchar)0xD800)); assert(!isValidCodeUnit(cast(dchar)0xD800)); assert(!isValidCodeUnit(cast(AsciiChar)0xA0)); assert( isValidCodeUnit(cast(Windows1250Char)0x80)); assert(!isValidCodeUnit(cast(Windows1250Char)0x81)); assert( isValidCodeUnit(cast(Windows1252Char)0x80)); assert(!isValidCodeUnit(cast(Windows1252Char)0x81));
- Returns true if the string is encoded correctly
Supersedes: This function supersedes std.utf.validate(), however note that this function returns a bool indicating whether the input was valid or not, whereas the older function would throw an exception.
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:const(E)[] s the string to be tested Examples:assert( isValid("\u20AC100")); assert(!isValid(cast(char[3])[167, 133, 175]));
- Returns the length of the longest possible substring, starting from the first code unit, which is validly encoded.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
const(E)[] s the string to be tested - Sanitizes a string by replacing malformed code unit sequences with valid code unit sequences. The result is guaranteed to be valid for this encoding.If the input string is already valid, this function returns the original, otherwise it constructs a new string by replacing all illegal code unit sequences with the encoding's replacement character, Invalid sequences will be replaced with the Unicode replacement character (U+FFFD) if the character repertoire contains it, otherwise invalid sequences will be replaced with '?'.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
immutable(E)[] s the string to be sanitized Examples:assert(sanitize("hello \xF0\x80world") == "hello \xEF\xBF\xBDworld");
- Returns the length of the first encoded sequence.The input to this function MUST be validly encoded. This is enforced by the function's in-contract.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
const(E)[] s the string to be sliced Examples:assert(firstSequence("\u20AC1000") == "\u20AC".length); assert(firstSequence("hel") == "h".length);
- Returns the length of the last encoded sequence.The input to this function MUST be validly encoded. This is enforced by the function's in-contract.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
const(E)[] s the string to be sliced Examples:assert(lastSequence("1000\u20AC") == "\u20AC".length); assert(lastSequence("hellö") == "ö".length);
-
The input to this function MUST be validly encoded. This is enforced by the function's in-contract.
Supersedes: This function supersedes std.utf.toUTFindex().
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Examples:assert(index("\u20AC100",1) == 3); assert(index("hällo",2) == 3);
- Decodes a single code point.This function removes one or more code units from the start of a string, and returns the decoded code point which those code units represent. The input to this function MUST be validly encoded. This is enforced by the function's in-contract.
Supersedes: This function supersedes std.utf.decode(), however, note that the function codePoints() supersedes it more conveniently.
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:S s the string whose first code point is to be decoded - Decodes a single code point from the end of a string.This function removes one or more code units from the end of a string, and returns the decoded code point which those code units represent. The input to this function MUST be validly encoded. This is enforced by the function's in-contract.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
const(E)[] s the string whose first code point is to be decoded - Decodes a single code point. The input does not have to be valid.This function removes one or more code units from the start of a string, and returns the decoded code point which those code units represent. This function will accept an invalidly encoded string as input. If an invalid sequence is found at the start of the string, this function will remove it, and return the value INVALID_SEQUENCE.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
S s the string whose first code point is to be decoded - Returns the number of code units required to encode a single code point.The input to this function MUST be a valid code point. This is enforced by the function's in-contract. The type of the output cannot be deduced. Therefore, it is necessary to explicitly specify the encoding as a template parameter.Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:
dchar c the code point to be encoded - Encodes a single code point.This function encodes a single code point into one or more code units. It returns a string containing those code units. The input to this function MUST be a valid code point. This is enforced by the function's in-contract. The type of the output cannot be deduced. Therefore, it is necessary to explicitly specify the encoding as a template parameter.
Supersedes: This function supersedes std.utf.encode(), however, note that the function codeUnits() supersedes it more conveniently.
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:dchar c the code point to be encoded - Encodes a single code point into an array.This function encodes a single code point into one or more code units The code units are stored in a user-supplied fixed-size array, which must be passed by reference. The input to this function MUST be a valid code point. This is enforced by the function's in-contract. The type of the output cannot be deduced. Therefore, it is necessary to explicitly specify the encoding as a template parameter.
Supersedes: This function supersedes std.utf.encode(), however, note that the function codeUnits() supersedes it more conveniently.
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:dchar c the code point to be encoded E[] array the destination array Returns:the number of code units written to the array - Encodes a single code point to a delegate.This function encodes a single code point into one or more code units. The code units are passed one at a time to the supplied delegate. The input to this function MUST be a valid code point. This is enforced by the function's in-contract. The type of the output cannot be deduced. Therefore, it is necessary to explicitly specify the encoding as a template parameter.
Supersedes: This function supersedes std.utf.encode(), however, note that the function codeUnits() supersedes it more conveniently.
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:dchar c the code point to be encoded void delegate(E) dg the delegate to invoke for each code unit - Encodes the contents of s in units of type Tgt, writing the result to an output range.Returns:The number of Tgt elements written.Parameters:
Tgt Element type of range. Src[] s Input array. R range Output range. - Returns a foreachable struct which can bidirectionally iterate over all code points in a string.The input to this function MUST be validly encoded. This is enforced by the function's in-contract. You can foreach either with or without an index. If an index is specified, it will be initialized at each iteration with the offset into the string at which the code point begins.
Supersedes: This function supersedes std.utf.decode().
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:immutable(E)[] s the string to be decoded Example:
string s = "hello world"; foreach(c;codePoints(s)) { // do something with c (which will always be a dchar) }
Note that, currently, foreach(c:codePoints(s)) is superior to foreach(c;s) in that the latter will fall over on encountering U+FFFF.Examples:string s = "hello"; string t; foreach(c;codePoints(s)) { t ~= cast(char)c; } assert(s == t);
- Returns a foreachable struct which can bidirectionally iterate over all code units in a code point.The input to this function MUST be a valid code point. This is enforced by the function's in-contract. The type of the output cannot be deduced. Therefore, it is necessary to explicitly specify the encoding type in the template parameter.
Supersedes: This function supersedes std.utf.encode().
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:dchar c the code point to be encoded Examples:char[] a; foreach(c;codeUnits!(char)(cast(dchar)'\u20AC')) { a ~= c; } assert(a.length == 3); assert(a[0] == 0xE2); assert(a[1] == 0x82); assert(a[2] == 0xAC);
- Convert a string from one encoding to another.
Supersedes: This function supersedes std.utf.toUTF8(), std.utf.toUTF16() and std.utf.toUTF32() (but note that to!() supersedes it more conveniently).
Standards:Unicode 5.0, ASCII, ISO-8859-1, ISO-8859-2, WINDOWS-1250, WINDOWS-1252Parameters:immutable(Src)[] s Source string. Must be validly encoded. This is enforced by the function's in-contract. immutable(Dst)[] r Destination string See Also:Examples:wstring ws; // transcode from UTF-8 to UTF-16 transcode("hello world",ws); assert(ws == "hello world"w); Latin1String ls; // transcode from UTF-16 to ISO-8859-1 transcode(ws, ls); assert(ws == "hello world");
- The base class for exceptions thrown by this module
- Abstract base class of all encoding schemes
- Registers a subclass of EncodingScheme.This function allows user-defined subclasses of EncodingScheme to be declared in other modules.
Example:
class Amiga1251 : EncodingScheme { shared static this() { EncodingScheme.register("path.to.Amiga1251"); } }
- Obtains a subclass of EncodingScheme which is capable of encoding and decoding the named encoding scheme.This function is only aware of EncodingSchemes which have been registered with the register() function.
Example:
auto scheme = EncodingScheme.create("Amiga-1251");
- Returns the standard name of the encoding scheme
- Returns true if the character c can be represented in this encoding scheme.
- Returns the number of ubytes required to encode this code point.The input to this function MUST be a valid code point.Parameters:
dchar c the code point to be encoded Returns:the number of ubytes required. - Encodes a single code point into a user-supplied, fixed-size buffer.This function encodes a single code point into one or more ubytes. The supplied buffer must be code unit aligned. (For example, UTF-16LE or UTF-16BE must be wchar-aligned, UTF-32LE or UTF-32BE must be dchar-aligned, etc.) The input to this function MUST be a valid code point.Parameters:
dchar c the code point to be encoded ubyte[] buffer the destination array Returns:the number of ubytes written. - Decodes a single code point.This function removes one or more ubytes from the start of an array, and returns the decoded code point which those ubytes represent. The input to this function MUST be validly encoded.Parameters:
const(ubyte)[] s the array whose first code point is to be decoded - Decodes a single code point. The input does not have to be valid.This function removes one or more ubytes from the start of an array, and returns the decoded code point which those ubytes represent. This function will accept an invalidly encoded array as input. If an invalid sequence is found at the start of the string, this function will remove it, and return the value INVALID_SEQUENCE.Parameters:
const(ubyte)[] s the array whose first code point is to be decoded - Returns the sequence of ubytes to be used to represent any character which cannot be represented in the encoding scheme.Normally this will be a representation of some substitution character, such as U+FFFD or '?'.
- Returns true if the array is encoded correctlyParameters:
const(ubyte)[] s the array to be tested - Returns the length of the longest possible substring, starting from the first element, which is validly encoded.Parameters:
const(ubyte)[] s the array to be tested - Sanitizes an array by replacing malformed ubyte sequences with valid ubyte sequences. The result is guaranteed to be valid for this encoding scheme.If the input array is already valid, this function returns the original, otherwise it constructs a new array by replacing all illegal sequences with the encoding scheme's replacement sequence.Parameters:
immutable(ubyte)[] s the string to be sanitized - Returns the length of the first encoded sequence.The input to this function MUST be validly encoded. This is enforced by the function's in-contract.Parameters:
const(ubyte)[] s the array to be sliced - Returns the total number of code points encoded in a ubyte array.The input to this function MUST be validly encoded. This is enforced by the function's in-contract.Parameters:
const(ubyte)[] s the string to be counted -
The input to this function MUST be validly encoded. This is enforced by the function's in-contract.
- EncodingScheme to handle ASCIIThis scheme recognises the following names: "ANSI_X3.4-1968", "ANSI_X3.4-1986", "ASCII", "IBM367", "ISO646-US", "ISO_646.irv:1991", "US-ASCII", "cp367", "csASCII" "iso-ir-6", "us"
- EncodingScheme to handle Latin-1This scheme recognises the following names: "CP819", "IBM819", "ISO-8859-1", "ISO_8859-1", "ISO_8859-1:1987", "csISOLatin1", "iso-ir-100", "l1", "latin1"
- EncodingScheme to handle Latin-2This scheme recognises the following names: "Latin 2", "ISO-8859-2", "ISO_8859-2", "ISO_8859-2:1999", "Windows-28592"
- EncodingScheme to handle Windows-1250This scheme recognises the following names: "windows-1250"
- EncodingScheme to handle Windows-1252This scheme recognises the following names: "windows-1252"
- EncodingScheme to handle UTF-8This scheme recognises the following names: "UTF-8"
- EncodingScheme to handle UTF-16 in native byte orderThis scheme recognises the following names: "UTF-16LE" (little-endian architecture only) "UTF-16BE" (big-endian architecture only)
- EncodingScheme to handle UTF-32 in native byte orderThis scheme recognises the following names: "UTF-32LE" (little-endian architecture only) "UTF-32BE" (big-endian architecture only)