- Rework the way literals are handled, these are now kept as strings until
[cparser] / string_rep.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef STRING_REP_H
21 #define STRING_REP_H
22
23 #include <assert.h>
24 #include <stdlib.h>
25
26 typedef struct string_t {
27         const char *begin; /**< UTF-8 encoded string, the last character is
28                                                 * guaranteed to be 0 */
29         size_t      size;  /**< size of string in bytes (not characters) */
30 } string_t;
31
32 typedef unsigned int utf32;
33 #define UTF32_PRINTF_FORMAT "%u"
34
35 /**
36  * "parse" an utf8 character from a string.
37  * Warning: This function only works for valid utf-8 inputs. The behaviour
38  * is undefined for invalid utf-8 input.
39  *
40  * @param p    A pointer to a pointer into the string. The pointer
41  *             is incremented for each consumed char
42  */
43 static inline utf32 read_utf8_char(const char **p)
44 {
45         const unsigned char *c      = (const unsigned char *) *p;
46         utf32                result;
47
48         if ((*c & 0x80) == 0) {
49                 /* 1 character encoding: 0b0??????? */
50                 result = *c++;
51         } else if ((*c & 0xE0) == 0xC0) {
52                 /* 2 character encoding: 0b110?????, 0b10?????? */
53                 result = *c++ & 0x1F;
54                 result = (result << 6) | (*c++ & 0x3F);
55         } else if ((*c & 0xF0) == 0xE0) {
56                 /* 3 character encoding: 0b1110????, 0b10??????, 0b10?????? */
57                 result = *c++ & 0x0F;
58                 result = (result << 6) | (*c++ & 0x3F);
59                 result = (result << 6) | (*c++ & 0x3F);
60         } else {
61                 /* 4 character enc.: 0b11110???, 0b10??????, 0b10??????, 0b10?????? */
62                 assert((*c & 0xF8) == 0xF0);
63                 result = *c++ & 0x07;
64                 result = (result << 6) | (*c++ & 0x3F);
65                 result = (result << 6) | (*c++ & 0x3F);
66                 result = (result << 6) | (*c++ & 0x3F);
67         }
68
69         *p = (const char*) c;
70         return result;
71 }
72
73 static inline size_t wstrlen(const string_t *string)
74 {
75         size_t      result = 0;
76         const char *p      = string->begin;
77         const char *end    = p + string->size;
78         while (p < end) {
79                 read_utf8_char(&p);
80                 ++result;
81         }
82         return result;
83 }
84
85 #endif