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 | |
6 | #include "include/core/SkTypes.h" |
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 | */ |
17 | SK_SPI int CountUTF8(const char* utf8, size_t byteLength); |
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 | */ |
23 | SK_SPI int CountUTF16(const uint16_t* utf16, size_t byteLength); |
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 | */ |
29 | SK_SPI int CountUTF32(const int32_t* utf32, size_t byteLength); |
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 | */ |
35 | SK_SPI SkUnichar NextUTF8(const char** ptr, const char* end); |
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 | */ |
42 | SK_SPI SkUnichar NextUTF16(const uint16_t** ptr, const uint16_t* end); |
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 | */ |
49 | SK_SPI SkUnichar NextUTF32(const int32_t** ptr, const int32_t* end); |
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 | */ |
58 | SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence] = nullptr); |
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 | */ |
65 | SK_SPI size_t ToUTF16(SkUnichar uni, uint16_t utf16[2] = nullptr); |
66 | |
67 | } // namespace SkUTF |
68 | |
69 | #endif // SkUTF_DEFINED |
70 | |