- Comments
- D
- Search
- Home

Last update Sun Jul 1 11:16:39 2007

# std.utf

Encode and decode UTF-8, UTF-16 and UTF-32 strings.
For Win32 systems, the C wchar_t type is UTF-16 and corresponds to the D wchar type. For linux systems, the C wchar_t type is UTF-32 and corresponds to the D utf.dchar type.
UTF character support is restricted to (\u0000 <= character <= \U0010FFFF).
See Also:
Wikipedia
http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
http://anubis.dkuug.dk/JTC1/SC2/WG2/docs/n1335

class UtfException: object.Exception;
  Exception class that is thrown upon any errors.
  uint idx;
    index in string of where error occurred
bool isValidDchar(dchar c);
  Test if c is a valid UTF-32 character.
  \uFFFE and \uFFFF are considered valid by this function, as they are permitted for internal use by an application, but they are not allowed for interchange by the Unicode standard.
  Returns:
  true if it is, false if not.
uint stride(const char[] s, uint i);
  stride() returns the length of a UTF-8 sequence starting at index i in string s.
  Returns:
  The number of bytes in the UTF-8 sequence or 0xFF meaning s[i] is not the start of of UTF-8 sequence.
uint stride(const wchar[] s, uint i);
  stride() returns the length of a UTF-16 sequence starting at index i in string s.
uint stride(const dchar[] s, uint i);
  stride() returns the length of a UTF-32 sequence starting at index i in string s.
  Returns:
  The return value will always be 1.
uint toUCSindex(const char[] s, uint i);
uint toUCSindex(const wchar[] s, uint i);
uint toUCSindex(const dchar[] s, uint i);
  Given an index i into an array of characters s[], and assuming that index i is at the start of a UTF character, determine the number of UCS characters up to that index i.
uint toUTFindex(const char[] s, uint n);
uint toUTFindex(const wchar[] s, uint n);
uint toUTFindex(const dchar[] s, uint n);
  Given a UCS index n into an array of characters s[], return the UTF index.
dchar decode(const char[] s, ref uint idx);
dchar decode(const wchar[] s, ref uint idx);
dchar decode(const dchar[] s, ref uint idx);
  Decodes and returns character starting at s[idx]. idx is advanced past the decoded character. If the character is not well formed, a UtfException is thrown and idx remains unchanged.
void encode(ref char[] s, dchar c);
void encode(ref wchar[] s, dchar c);
void encode(ref dchar[] s, dchar c);
  Encodes character c and appends it to array s[].
void validate(const char[] s);
void validate(const wchar[] s);
void validate(const dchar[] s);
  Checks to see if string is well formed or not. Throws a UtfException if it is not. Use to check all untrusted input for correctness.
const(char)[] toUTF8(const(char)[] s);
const(char)[] toUTF8(const(wchar)[] s);
const(char)[] toUTF8(const(dchar)[] s);
  Encodes string s into UTF-8 and returns the encoded string.
const(wchar)[] toUTF16(const(char)[] s);
const(wchar*) toUTF16z(const(char)[] s);
const(wchar)[] toUTF16(const(wchar)[] s);
const(wchar)[] toUTF16(const(dchar)[] s);
  Encodes string s into UTF-16 and returns the encoded string. toUTF16z() is suitable for calling the 'W' functions in the Win32 API that take an LPWSTR or LPCWSTR argument.
const(dchar)[] toUTF32(const(char)[] s);
const(dchar)[] toUTF32(const(wchar)[] s);
const(dchar)[] toUTF32(const(dchar)[] s);
  Encodes string s into UTF-32 and returns the encoded string.

Copyright © 1999-2007 by Digital Mars, All Rights Reserved | Page generated by Ddoc.