]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/utf8.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / utf8.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2012 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <uchar.h>
27
28 #include "macro.h"
29 #include "missing.h"
30
31 #define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
32 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
33
34 bool unichar_is_valid(char32_t c);
35
36 const char *utf8_is_valid(const char *s) _pure_;
37 char *ascii_is_valid(const char *s) _pure_;
38
39 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_;
40 #define utf8_is_printable(str, length) utf8_is_printable_newline(str, length, true)
41
42 char *utf8_escape_invalid(const char *s);
43 char *utf8_escape_non_printable(const char *str);
44
45 size_t utf8_encode_unichar(char *out_utf8, char32_t g);
46 char *utf16_to_utf8(const void *s, size_t length);
47
48 int utf8_encoded_valid_unichar(const char *str);
49 int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar);
50
51 static inline bool utf16_is_surrogate(char16_t c) {
52 return (0xd800 <= c && c <= 0xdfff);
53 }
54
55 static inline bool utf16_is_trailing_surrogate(char16_t c) {
56 return (0xdc00 <= c && c <= 0xdfff);
57 }
58
59 static inline char32_t utf16_surrogate_pair_to_unichar(char16_t lead, char16_t trail) {
60 return ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000;
61 }