1 /* Part of SWI-Prolog
2
3 Author: Jan Wielemaker and Anjo Anjewierden
4 E-mail: J.Wielemaker@vu.nl
5 WWW: http://www.swi-prolog.org
6 Copyright (c) 2011-2016, University of Amsterdam
7 Vu University Amsterdam
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 1. Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
20 distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #ifndef UTF8_H_INCLUDED
37 #define UTF8_H_INCLUDED
38
39 #define UTF8_MALFORMED_REPLACEMENT 0xfffd
40
41 #define ISUTF8_MB(c) ((unsigned)(c) >= 0xc0 && (unsigned)(c) <= 0xfd)
42
43 #define ISUTF8_CB(c) (((c)&0xc0) == 0x80) /* Is continuation byte */
44 #define ISUTF8_FB2(c) (((c)&0xe0) == 0xc0)
45 #define ISUTF8_FB3(c) (((c)&0xf0) == 0xe0)
46 #define ISUTF8_FB4(c) (((c)&0xf8) == 0xf0)
47 #define ISUTF8_FB5(c) (((c)&0xfc) == 0xf8)
48 #define ISUTF8_FB6(c) (((c)&0xfe) == 0xfc)
49
50 #define UTF8_FBN(c) (!(c&0x80) ? 0 : \
51 ISUTF8_FB2(c) ? 1 : \
52 ISUTF8_FB3(c) ? 2 : \
53 ISUTF8_FB4(c) ? 3 : \
54 ISUTF8_FB5(c) ? 4 : \
55 ISUTF8_FB6(c) ? 5 : -1)
56 #define UTF8_FBV(c,n) ( n == 0 ? c : (c & ((0x01<<(6-n))-1)) )
57
58 #define utf8_get_char(in, chr) \
59 (*(in) & 0x80 ? _xos_utf8_get_char(in, chr) \
60 : (*(chr) = *(in), (char *)(in)+1))
61 #define utf8_put_char(out, chr) \
62 ((chr) < 0x80 ? out[0]=(char)(chr), out+1 \
63 : _xos_utf8_put_char(out, (chr)))
64
65 static char *_xos_utf8_get_char(const char *in, int *chr);
66 static char *_xos_utf8_put_char(char *out, int chr);
67
68
69 /*******************************
70 * INLINE FUNCTIONS *
71 *******************************/
72
73 static inline int
utf8_code_bytes(int chr)74 utf8_code_bytes(int chr)
75 { if ( chr < 0x80 ) return 1;
76 if ( chr < 0x800 ) return 2;
77 if ( chr < 0x10000 ) return 3;
78 if ( chr < 0x200000 ) return 4;
79 if ( chr < 0x4000000 ) return 5;
80 if ( chr < 0x80000000 ) return 6;
81 return -1;
82 }
83
84
85 #endif /*UTF8_H_INCLUDED*/
86