ast2firm: Implement casting from complex to real types.
[cparser] / unicode.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #ifndef UNICODE_H
6 #define UNICODE_H
7
8 #include <assert.h>
9 #include "adt/obst.h"
10
11 typedef unsigned int utf32;
12 #define UTF32_PRINTF_FORMAT "%u"
13
14 /**
15  * "parse" an utf8 character from a string.
16  * Warning: This function only works for valid utf-8 inputs. The behaviour
17  * is undefined for invalid utf-8 input.
18  *
19  * @param p    A pointer to a pointer into the string. The pointer
20  *             is incremented for each consumed char
21  */
22 static inline utf32 read_utf8_char(const char **p)
23 {
24         const unsigned char *c      = (const unsigned char *) *p;
25         utf32                result;
26
27         if ((*c & 0x80) == 0) {
28                 /* 1 character encoding: 0b0??????? */
29                 result = *c++;
30         } else if ((*c & 0xE0) == 0xC0) {
31                 /* 2 character encoding: 0b110?????, 0b10?????? */
32                 result = *c++ & 0x1F;
33                 result = (result << 6) | (*c++ & 0x3F);
34         } else if ((*c & 0xF0) == 0xE0) {
35                 /* 3 character encoding: 0b1110????, 0b10??????, 0b10?????? */
36                 result = *c++ & 0x0F;
37                 result = (result << 6) | (*c++ & 0x3F);
38                 result = (result << 6) | (*c++ & 0x3F);
39         } else {
40                 /* 4 character enc.: 0b11110???, 0b10??????, 0b10??????, 0b10?????? */
41                 assert((*c & 0xF8) == 0xF0);
42                 result = *c++ & 0x07;
43                 result = (result << 6) | (*c++ & 0x3F);
44                 result = (result << 6) | (*c++ & 0x3F);
45                 result = (result << 6) | (*c++ & 0x3F);
46         }
47
48         *p = (const char*) c;
49         return result;
50 }
51
52 static inline void obstack_grow_utf8(struct obstack *const obst, utf32 const c)
53 {
54         if (c < 0x80U) {
55                 obstack_1grow(obst, c);
56         } else if (c < 0x800) {
57                 obstack_1grow(obst, 0xC0 |  (c >>  6));
58                 goto one_more;
59         } else if (c < 0x10000) {
60                 obstack_1grow(obst, 0xE0 |  (c >> 12));
61                 goto two_more;
62         } else {
63                 obstack_1grow(obst, 0xF0 |  (c >> 18));
64                 obstack_1grow(obst, 0x80 | ((c >> 12) & 0x3F));
65 two_more:
66                 obstack_1grow(obst, 0x80 | ((c >>  6) & 0x3F));
67 one_more:
68                 obstack_1grow(obst, 0x80 | ( c        & 0x3F));
69         }
70 }
71
72 #endif