chiark / gitweb /
ffe49c128587ec310b063fb8bbe388ab684103b5
[elogind.git] / src / basic / utf8.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <uchar.h>
12
13 #include "macro.h"
14 #include "missing.h"
15
16 #define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
17 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
18
19 bool unichar_is_valid(char32_t c);
20
21 const char *utf8_is_valid(const char *s) _pure_;
22 char *ascii_is_valid(const char *s) _pure_;
23 char *ascii_is_valid_n(const char *str, size_t len);
24
25 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_;
26 #define utf8_is_printable(str, length) utf8_is_printable_newline(str, length, true)
27
28 char *utf8_escape_invalid(const char *s);
29 char *utf8_escape_non_printable(const char *str);
30
31 size_t utf8_encode_unichar(char *out_utf8, char32_t g);
32 char *utf16_to_utf8(const void *s, size_t length);
33
34 int utf8_encoded_valid_unichar(const char *str);
35 int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar);
36
37 static inline bool utf16_is_surrogate(char16_t c) {
38         return (0xd800 <= c && c <= 0xdfff);
39 }
40
41 static inline bool utf16_is_trailing_surrogate(char16_t c) {
42         return (0xdc00 <= c && c <= 0xdfff);
43 }
44
45 static inline char32_t utf16_surrogate_pair_to_unichar(char16_t lead, char16_t trail) {
46                 return ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000;
47 }
48
49 size_t utf8_n_codepoints(const char *str);
50 size_t utf8_console_width(const char *str);