D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Sat Jan 2 16:41:12 2010

# 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

License:
Boost License 1.0.

Authors:
Walter Bright

Copyright Digital Mars 2000 - 2009. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at ) http:
//www.boost.org/LICENSE_1_0.txt

class UtfException: object.Exception;
  Exception class that is thrown upon any errors.

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(in char[] s, size_t 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(in wchar[] s, size_t i);
  stride() returns the length of a UTF-16 sequence starting at index i in string s.

uint stride(in dchar[] s, size_t 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.

size_t toUCSindex(in char[] s, size_t i);
size_t toUCSindex(in wchar[] s, size_t i);
size_t toUCSindex(in dchar[] s, size_t 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.

size_t toUTFindex(in char[] s, size_t n);
size_t toUTFindex(in wchar[] s, size_t n);
size_t toUTFindex(in dchar[] s, size_t n);
  Given a UCS index n into an array of characters s[], return the UTF index.

dchar decode(in char[] s, ref size_t idx);
dchar decode(in wchar[] s, ref size_t idx);
dchar decode(in dchar[] s, ref size_t 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.

size_t encode(ref char[4u] buf, in dchar c);
size_t encode(ref wchar[2u] buf, dchar c);
  Encodes character c into fixed-size array s. Returns the actual length of the encoded character (a number between 1 and 4 for char[4] buffers, and between 1 and 2 for wchar[2] buffers).

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[].

ubyte codeLength(C)(dchar c);
  Returns the code length of c in the encoding using C as a code point. The code is returned in character count, not in bytes.

void validate(S)(in S s);
  Checks to see if string is well formed or not. S can be an array of char, wchar, or dchar. Throws a UtfException if it is not. Use to check all untrusted input for correctness.

string toUTF8(string s);
string toUTF8(const(wchar)[] s);
string toUTF8(const(dchar)[] s);
  Encodes string s into UTF-8 and returns the encoded string.

wstring toUTF16(const(char)[] s);
const(wchar*) toUTF16z(in char[] s);
wstring toUTF16(wstring s);
wstring 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.

dstring toUTF32(const(char)[] s);
dstring toUTF32(const(wchar)[] s);
dstring toUTF32(dstring s);
  Encodes string s into UTF-32 and returns the encoded string.

uint count(E)(const(E)[] s);
  Returns the total number of code points encoded in a string.

  The input to this function MUST be validly encoded.

  Supercedes:
  This function supercedes std.utf.toUCSindex().

  Standards:
  Unicode 5.0, ASCII, ISO-8859-1, WINDOWS-1252

  Parameters:

  s | the string to be counted

Copyright Digital Mars 2000 - 2009. | Page generated by Ddoc.