Fcitx
cutf8.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2010-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 /// \addtogroup FcitxUtils
9 /// \{
10 ///
11 /// \file
12 /// \brief C-style utf8 utility functions.
13 #ifndef _FCITX_UTILS_CUTF8_H_
14 #define _FCITX_UTILS_CUTF8_H_
15 
16 #include <cstdint>
17 #include <cstdlib>
18 #include <fcitx-utils/fcitxutils_export.h>
19 
20 //// Max length of a utf8 character
21 #define FCITX_UTF8_MAX_LENGTH 6
22 
23 /// \brief Get utf8 string length
24 FCITXUTILS_EXPORT size_t fcitx_utf8_strlen(const char *s);
25 
26 /// \brief Get UCS-4 char in the utf8 string
27 FCITXUTILS_EXPORT char *fcitx_utf8_get_char(const char *in, uint32_t *chr);
28 
29 /// \brief Get the number of bytes of next character.
30 FCITXUTILS_EXPORT unsigned int fcitx_utf8_char_len(const char *in);
31 
32 /// \brief Get the pointer to the nth character.
33 ///
34 /// This function will not touch the content for s, so const pointer
35 /// can be safely passed and converted.
36 FCITXUTILS_EXPORT char *fcitx_utf8_get_nth_char(const char *s, uint32_t n);
37 
38 /// \brief Check if the string is valid utf8 string.
39 FCITXUTILS_EXPORT bool fcitx_utf8_check_string(const char *s);
40 
41 /// \brief Get validated character.
42 ///
43 /// Returns the UCS-4 value if its valid character. Returns (uint32_t) -1 if
44 /// it is not a valid char, (uint32_t)-2 if length is not enough.
45 FCITXUTILS_EXPORT uint32_t fcitx_utf8_get_char_validated(const char *p,
46  int max_len,
47  int *plen);
48 
49 /// \brief Copy most byte length, but keep utf8 valid.
50 FCITXUTILS_EXPORT void fcitx_utf8_strncpy(char *str, const char *s,
51  size_t byte);
52 
53 /// \brief Count most byte length, utf8 string length.
54 FCITXUTILS_EXPORT size_t fcitx_utf8_strnlen(const char *str, size_t byte);
55 
56 /// \brief Count most byte length, utf8 string length and validates the string
57 FCITXUTILS_EXPORT size_t fcitx_utf8_strnlen_validated(const char *str,
58  size_t byte);
59 
60 /// \brief Return the utf8 bytes of a UCS4 char.
61 FCITXUTILS_EXPORT int fcitx_ucs4_char_len(uint32_t c);
62 
63 /// \brief Convert ucs4 char to utf8, need to have enough memory for it.
64 FCITXUTILS_EXPORT int fcitx_ucs4_to_utf8(uint32_t c, char *output);
65 
66 #endif
FCITXUTILS_EXPORT void fcitx_utf8_strncpy(char *str, const char *s, size_t byte)
Copy most byte length, but keep utf8 valid.
Definition: cutf8.cpp:315
FCITXUTILS_EXPORT char * fcitx_utf8_get_nth_char(const char *s, uint32_t n)
Get the pointer to the nth character.
Definition: cutf8.cpp:191
FCITXUTILS_EXPORT uint32_t fcitx_utf8_get_char_validated(const char *p, int max_len, int *plen)
Get validated character.
Definition: cutf8.cpp:275
FCITXUTILS_EXPORT size_t fcitx_utf8_strlen(const char *s)
Get utf8 string length.
Definition: cutf8.cpp:32
FCITXUTILS_EXPORT unsigned int fcitx_utf8_char_len(const char *in)
Get the number of bytes of next character.
Definition: cutf8.cpp:45
FCITXUTILS_EXPORT size_t fcitx_utf8_strnlen_validated(const char *str, size_t byte)
Count most byte length, utf8 string length and validates the string.
Definition: cutf8.cpp:337
FCITXUTILS_EXPORT size_t fcitx_utf8_strnlen(const char *str, size_t byte)
Count most byte length, utf8 string length.
Definition: cutf8.cpp:355
FCITXUTILS_EXPORT int fcitx_ucs4_to_utf8(uint32_t c, char *output)
Convert ucs4 char to utf8, need to have enough memory for it.
Definition: cutf8.cpp:99
FCITXUTILS_EXPORT int fcitx_ucs4_char_len(uint32_t c)
Return the utf8 bytes of a UCS4 char.
Definition: cutf8.cpp:79
FCITXUTILS_EXPORT char * fcitx_utf8_get_char(const char *in, uint32_t *chr)
Get UCS-4 char in the utf8 string.
Definition: cutf8.cpp:146
FCITXUTILS_EXPORT bool fcitx_utf8_check_string(const char *s)
Check if the string is valid utf8 string.
Definition: cutf8.cpp:298