encoding.h - エンジンの C API リファレンス

encoding.h
  1. #pragma once
  2. namespace stingray_plugin_foundation {
  3. template<class T> class Array;
  4. class Allocator;
  5. // Functions for string encoding.
  6. namespace encoding
  7. {
  8. // Encodes the unicode character `c` in UTF-8 at the position pointed
  9. // to by `buffer`. `buffer` must have room for at least 5 bytes.
  10. // Returns a pointer to the first character in buffer after the end of
  11. // the encoding.
  12. char *utf8_encode(int codepoint, char *utf8);
  13. // Decodes the UTF-8 character at the start of `buffer` into a unicode
  14. // code point and returns it in `res`. Returns a pointer to the first
  15. // character in the buffer after the UTF-8 character that was just
  16. // consumed.
  17. const char *utf8_decode(const char *utf8, int &codepoint);
  18. // Returns the number of bytes used by the UTF-8 codepoint pointed to by
  19. // buffer. Advance by this amount to get to the next code point.
  20. unsigned utf8_codepoint_bytes(const char *utf8);
  21. // Decodes an UTF-8 string to a vector of codepoints.
  22. void utf8_decode(const char *utf8, Array<unsigned> &codepoints);
  23. // Encode a vector of codepoints to an array of UTF-8 characters.
  24. void utf8_encode(const Array<unsigned> &codepoints, Array<char> &utf8);
  25. // Encode an array of codepoints to an array of UTF-8 characters.
  26. void utf8_encode(const unsigned *codepoints, unsigned size, Array<char> &utf8);
  27. // Determines the begin and end position of the utf8 codepoint at
  28. // `utf8`[index]. `end` is an index pointing to the byte after the
  29. // utf-8 codepoint.
  30. void utf8_location(const char *utf8, unsigned index, unsigned &begin, unsigned &end);
  31. // Returns a pointer to the next character if the character at utf8 is valid, otherwise
  32. // returns nullptr.
  33. const char *utf8_valid_first(const char *utf8);
  34. // Returns true if the string utf8 contains only valid UTF-8 code points and
  35. // false otherwise.
  36. bool utf8_valid_all(const char *utf8);
  37. // Returns the number of of bytes needed to store the UCS-2 encoded wchar_t *
  38. // as an UTF-8 value, including the zero byte at the end of the UTF-8 string.
  39. unsigned wstr_to_utf8_bytes(const wchar_t *ucs2);
  40. // Converts an UCS-2 encoded w_char * to an UTF-8 encoded char *.
  41. // `size` specifies the size of the buffer. If the conversion requires
  42. // more than `size` bytes, an assert is thrown.
  43. void wstr_to_utf8(const wchar_t *ucs2, char *utf8, unsigned size);
  44. // Converts an UCS-2 encoded w_char * to an UTF-8 encoded char *,
  45. // allocating memory as necessary.
  46. void wstr_to_utf8(const wchar_t *ucs2, Array<char> &utf8);
  47. // Returns the number of of tokens needed to store the UTF-8 encoded char *
  48. // as an UCS-2 value, including the zero token at the end of the UCS-2 string.
  49. unsigned utf8_to_wstr_tokens(const char *utf8);
  50. // Converts an UTF-8 encoded char * to an UCS-2 encoded wchar_t *.
  51. // `tokens` specifies the number of tokens in the buffer. If the
  52. // converted string contains more tokens, an assert is thrown.
  53. void utf8_to_wstr(const char *utf8, wchar_t *ucs2, unsigned tokens);
  54. // Converts an UTF-8 encoded char * to an UCS-2 encoded wchar_t *,
  55. // allocating memory as necessary.
  56. void utf8_to_wstr(const char *utf8, Array<wchar_t> &ucs2);
  57. // Converts an UTF-8 encoded char * to an UCS-2 encoded wchar_t *,
  58. // allocated using the specified allocator.
  59. wchar_t *utf8_to_wstr(const char *utf8, Allocator &a);
  60. } // namespace encoding
  61. } // namespace stingray_plugin_foundation
  62. #include "encoding.inl"