Skip to content
Steven Arnow edited this page Jun 8, 2013 · 1 revision

DARNIT_UTF8

DARNIT_UTF8 provides functions to convert between unicode and UTF-8, and some functions to isolate induvidual UTF-8 characters.

UTF-8 is a multi-byte way of representating Unicode with. Unicode is the standard way of encoding symbols from pretty much every language in existance, and that has ever existed. UTF-8 does however not encode every character in the same amount of bytes. The lower 7-bits of ASCII is compatible (enough for English and most symbols used in USA,) but everything else is encoded with two or more bytes.

d_utf8_start_char

int d_utf8_start_char(char c);

Tells if a byte is the first byte of a UTF-8 character or not.

Arguments

  • c - The byte to test

Return value

Returns 0 if the byte is a valid UTF-8 starting byte, -1 if not.

d_utf8_valid

int d_utf8_valid(const char *str);

Tells if the string of bytes from byte 0 in the string makes up a valid UTF-8 character

Arguments

  • str - The string containing the character to test

Return value

Returns -1 if the string does not immideatly contain a valid UTF-8 character, 0 if it does.

d_utf8_char_length

int d_utf8_char_length(const char *str);

Tells the length of the first UTF-8 character in the string. Length is returned in bytes.

Arguments

  • str - The string to mesure the first characters length in

Return value

Returns the length in bytes. If the first byte in the string is NULL, 0 is returned. Otherwise, at least 1 is returned.

d_utf8_decode

unsigned int d_utf8_decode(const char *str);

Decodes an UTF-8 character into unicode.

Arguments

  • str - The string containing the character to encode

Return value

Returns the Unicode codepoint for the first character in str.

d_utf8_encode

int d_utf8_encode(unsigned int ch, char *str, int buf_len);

Encodes a Unicode character into UTF-8.

Arguments

  • ch - The Unicode character to encode
  • str - The buffer to place the UTF-8 encoding of the character in
  • buff_len - The length of the buffer

Return value

Returns the number of bytes used for the character in the buffer

d_utf8_encoded_length

int d_utf8_encoded_length(unsigned int ch);

Tells the number of bytes it will take to encode ch in UTF-8.

Arguments

  • ch - The character to check the UTF-8 length for

Return value

Returns the number of bytes it would take to encode ch in unicode.

d_utf8_chars_in_string

int d_utf8_chars_in_string(const char *str);

Returns the number of characters in the string.

Arguments

  • str - The string to count UTF-8 characters in

Return value

Returns the number of characters found in the string.

d_utf8_chars_in_string_limited

int d_utf8_chars_in_string_limited(const char *str, int buf_len);

Returns the number of UTF-8 characters in str, but will go no longer than buff_len bytes into the buffer.

Arguments

  • str - The string to count UTF-8 characters in
  • buf_len - The maximum number of bytes to use in str

Return value

Returns the number of UTF-8 characters found in str.

d_utf8_char_pos

int d_utf8_char_pos(const char *str, unsigned int unicode_pos);

Finds the byte position where UTF-8 character #unicode_pos starts

Arguments

  • str - The string to look in
  • unicode_pos - The UTF-8 character position to find

Return value

Returns -1 if the UTF-8 character position wasn't found, 0 or higher is the byte position for the character.

Clone this wiki locally