Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 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 | |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 6 | #include "include/core/SkTypes.h" |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 7 | #include <cstddef> |
| 8 | #include <cstdint> |
| 9 | |
| 10 | typedef int32_t SkUnichar; |
| 11 | |
| 12 | namespace SkUTF { |
| 13 | |
| 14 | /** Given a sequence of UTF-8 bytes, return the number of unicode codepoints. |
| 15 | If the sequence is invalid UTF-8, return -1. |
| 16 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 17 | SK_SPI int CountUTF8(const char* utf8, size_t byteLength); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 18 | |
| 19 | /** Given a sequence of aligned UTF-16 characters in machine-endian form, |
| 20 | return the number of unicode codepoints. If the sequence is invalid |
| 21 | UTF-16, return -1. |
| 22 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 23 | SK_SPI int CountUTF16(const uint16_t* utf16, size_t byteLength); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 24 | |
| 25 | /** Given a sequence of aligned UTF-32 characters in machine-endian form, |
| 26 | return the number of unicode codepoints. If the sequence is invalid |
| 27 | UTF-32, return -1. |
| 28 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 29 | SK_SPI int CountUTF32(const int32_t* utf32, size_t byteLength); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 30 | |
| 31 | /** Given a sequence of UTF-8 bytes, return the first unicode codepoint. |
| 32 | The pointer will be incremented to point at the next codepoint's start. If |
| 33 | invalid UTF-8 is encountered, set *ptr to end and return -1. |
| 34 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 35 | SK_SPI SkUnichar NextUTF8(const char** ptr, const char* end); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 36 | |
| 37 | /** Given a sequence of aligned UTF-16 characters in machine-endian form, |
| 38 | return the first unicode codepoint. The pointer will be incremented to |
| 39 | point at the next codepoint's start. If invalid UTF-16 is encountered, |
| 40 | set *ptr to end and return -1. |
| 41 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 42 | SK_SPI SkUnichar NextUTF16(const uint16_t** ptr, const uint16_t* end); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 43 | |
| 44 | /** Given a sequence of aligned UTF-32 characters in machine-endian form, |
| 45 | return the first unicode codepoint. The pointer will be incremented to |
| 46 | point at the next codepoint's start. If invalid UTF-32 is encountered, |
| 47 | set *ptr to end and return -1. |
| 48 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 49 | SK_SPI SkUnichar NextUTF32(const int32_t** ptr, const int32_t* end); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 50 | |
| 51 | constexpr unsigned kMaxBytesInUTF8Sequence = 4; |
| 52 | |
| 53 | /** Convert the unicode codepoint into UTF-8. If `utf8` is non-null, place the |
| 54 | result in that array. Return the number of bytes in the result. If `utf8` |
| 55 | is null, simply return the number of bytes that would be used. For invalid |
| 56 | unicode codepoints, return 0. |
| 57 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 58 | SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence] = nullptr); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 59 | |
| 60 | /** Convert the unicode codepoint into UTF-16. If `utf16` is non-null, place |
| 61 | the result in that array. Return the number of UTF-16 code units in the |
| 62 | result (1 or 2). If `utf16` is null, simply return the number of code |
| 63 | units that would be used. For invalid unicode codepoints, return 0. |
| 64 | */ |
Brian Osman | 417d299 | 2019-11-18 17:19:42 -0500 | [diff] [blame^] | 65 | SK_SPI size_t ToUTF16(SkUnichar uni, uint16_t utf16[2] = nullptr); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 66 | |
| 67 | } // namespace SkUTF |
| 68 | |
| 69 | #endif // SkUTF_DEFINED |