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