SkUTF.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 Google LLC.
  2. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  3. #ifndef SkUTF_DEFINED
  4. #define SkUTF_DEFINED
  5. #include <cstddef>
  6. #include <cstdint>
  7. typedef int32_t SkUnichar;
  8. namespace SkUTF {
  9. /** Given a sequence of UTF-8 bytes, return the number of unicode codepoints.
  10. If the sequence is invalid UTF-8, return -1.
  11. */
  12. int CountUTF8(const char* utf8, size_t byteLength);
  13. /** Given a sequence of aligned UTF-16 characters in machine-endian form,
  14. return the number of unicode codepoints. If the sequence is invalid
  15. UTF-16, return -1.
  16. */
  17. int CountUTF16(const uint16_t* utf16, size_t byteLength);
  18. /** Given a sequence of aligned UTF-32 characters in machine-endian form,
  19. return the number of unicode codepoints. If the sequence is invalid
  20. UTF-32, return -1.
  21. */
  22. int CountUTF32(const int32_t* utf32, size_t byteLength);
  23. /** Given a sequence of UTF-8 bytes, return the first unicode codepoint.
  24. The pointer will be incremented to point at the next codepoint's start. If
  25. invalid UTF-8 is encountered, set *ptr to end and return -1.
  26. */
  27. SkUnichar NextUTF8(const char** ptr, const char* end);
  28. /** Given a sequence of aligned UTF-16 characters in machine-endian form,
  29. return the first unicode codepoint. The pointer will be incremented to
  30. point at the next codepoint's start. If invalid UTF-16 is encountered,
  31. set *ptr to end and return -1.
  32. */
  33. SkUnichar NextUTF16(const uint16_t** ptr, const uint16_t* end);
  34. /** Given a sequence of aligned UTF-32 characters in machine-endian form,
  35. return the first unicode codepoint. The pointer will be incremented to
  36. point at the next codepoint's start. If invalid UTF-32 is encountered,
  37. set *ptr to end and return -1.
  38. */
  39. SkUnichar NextUTF32(const int32_t** ptr, const int32_t* end);
  40. constexpr unsigned kMaxBytesInUTF8Sequence = 4;
  41. /** Convert the unicode codepoint into UTF-8. If `utf8` is non-null, place the
  42. result in that array. Return the number of bytes in the result. If `utf8`
  43. is null, simply return the number of bytes that would be used. For invalid
  44. unicode codepoints, return 0.
  45. */
  46. size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence] = nullptr);
  47. /** Convert the unicode codepoint into UTF-16. If `utf16` is non-null, place
  48. the result in that array. Return the number of UTF-16 code units in the
  49. result (1 or 2). If `utf16` is null, simply return the number of code
  50. units that would be used. For invalid unicode codepoints, return 0.
  51. */
  52. size_t ToUTF16(SkUnichar uni, uint16_t utf16[2] = nullptr);
  53. } // namespace SkUTF
  54. #endif // SkUTF_DEFINED