1 | #pragma once |
2 | |
3 | #include <string> |
4 | #include <cassert> |
5 | #include <cstring> |
6 | #include <cstdint> |
7 | |
8 | namespace duckdb { |
9 | |
10 | enum class UnicodeType { INVALID, ASCII, UNICODE }; |
11 | enum class UnicodeInvalidReason { BYTE_MISMATCH, INVALID_UNICODE }; |
12 | |
13 | class Utf8Proc { |
14 | public: |
15 | //! Distinguishes ASCII, Valid UTF8 and Invalid UTF8 strings |
16 | static UnicodeType Analyze(const char *s, size_t len, UnicodeInvalidReason *invalid_reason = nullptr, size_t *invalid_pos = nullptr); |
17 | //! Performs UTF NFC normalization of string, return value needs to be free'd |
18 | static char* Normalize(const char* s, size_t len); |
19 | //! Returns whether or not the UTF8 string is valid |
20 | static bool IsValid(const char *s, size_t len); |
21 | //! Returns the position (in bytes) of the next grapheme cluster |
22 | static size_t NextGraphemeCluster(const char *s, size_t len, size_t pos); |
23 | //! Returns the position (in bytes) of the previous grapheme cluster |
24 | static size_t PreviousGraphemeCluster(const char *s, size_t len, size_t pos); |
25 | |
26 | //! Transform a codepoint to utf8 and writes it to "c", sets "sz" to the size of the codepoint |
27 | static bool CodepointToUtf8(int cp, int &sz, char *c); |
28 | //! Returns the codepoint length in bytes when encoded in UTF8 |
29 | static int CodepointLength(int cp); |
30 | //! Transform a UTF8 string to a codepoint; returns the codepoint and writes the length of the codepoint (in UTF8) to sz |
31 | static int32_t UTF8ToCodepoint(const char *c, int &sz); |
32 | //! Returns the render width of a single character in a string |
33 | static size_t RenderWidth(const char *s, size_t len, size_t pos); |
34 | static size_t RenderWidth(const std::string &str); |
35 | |
36 | }; |
37 | |
38 | } |
39 | |