--- /dev/null
+#include "config.h"
+
+#include "input.h"
+
+#include <ctype.h>
+#include "lexer.h"
+#include "diagnostic.h"
+
+typedef size_t (*decode_func)(input_t *input, utf32 *buffer, size_t buffer_size);
+
+struct input_t {
+ FILE *file;
+ decode_func decode;
+
+ /* state for utf-8 decoder */
+ utf32 utf8_part_decoded_min_code;
+ utf32 utf8_part_decoded_char;
+ size_t utf8_part_decoded_rest_len;
+};
+
+static input_error_callback_func input_error;
+
+void set_input_error_callback(input_error_callback_func new_func)
+{
+ input_error = new_func;
+}
+
+static size_t read_block(FILE *file, unsigned char *const read_buf,
+ size_t const n)
+{
+ size_t const s = fread(read_buf, 1, n, file);
+ if (s == 0) {
+ /* on OS/X ferror appears to return true on eof as well when running
+ * the application in gdb... */
+ if (!feof(file) && ferror(file))
+ input_error(0, 0, "read from input failed");
+ return 0;
+ }
+ return s;
+}
+
+static size_t decode_iso_8859_1(input_t *input, utf32 *buffer,
+ size_t buffer_size)
+{
+ unsigned char read_buf[buffer_size];
+ size_t const s = read_block(input->file, read_buf, sizeof(read_buf));
+
+ unsigned char const *src = read_buf;
+ unsigned char const *end = read_buf + s;
+ utf32 *dst = buffer;
+ while (src != end)
+ *dst++ = *src++;
+
+ return s;
+}
+
+static size_t decode_iso_8859_15(input_t *input, utf32 *buffer,
+ size_t buffer_size)
+{
+ unsigned char read_buf[buffer_size];
+ size_t const s = read_block(input->file, read_buf, sizeof(read_buf));
+
+ unsigned char const *src = read_buf;
+ unsigned char const *end = read_buf + s;
+ utf32 *dst = buffer;
+ while (src != end) {
+ utf32 tc = *src++;
+ switch (tc) {
+ case 0xA4: tc = 0x20AC; break; // €
+ case 0xA6: tc = 0x0160; break; // Š
+ case 0xA8: tc = 0x0161; break; // š
+ case 0xB4: tc = 0x017D; break; // Ž
+ case 0xB8: tc = 0x017E; break; // ž
+ case 0xBC: tc = 0x0152; break; // Œ
+ case 0xBD: tc = 0x0153; break; // œ
+ case 0xBE: tc = 0x0178; break; // Ÿ
+ }
+ *dst++ = tc;
+ }
+
+ return s;
+}
+
+static size_t decode_utf8(input_t *input, utf32 *buffer, size_t buffer_size)
+{
+ unsigned char read_buf[buffer_size];
+
+ while (true) {
+ size_t const s = read_block(input->file, read_buf, sizeof(read_buf));
+ if (s == 0) {
+ if (input->utf8_part_decoded_rest_len > 0)
+ input_error(0, 0, "incomplete input char at end of input");
+ return 0;
+ }
+
+ unsigned char const *src = read_buf;
+ unsigned char const *end = read_buf + s;
+ utf32 *dst = buffer;
+ utf32 decoded;
+ utf32 min_code;
+
+ if (input->utf8_part_decoded_rest_len != 0) {
+ min_code = input->utf8_part_decoded_min_code;
+ decoded = input->utf8_part_decoded_char;
+ size_t const rest_len = input->utf8_part_decoded_rest_len;
+ input->utf8_part_decoded_rest_len = 0;
+ switch (rest_len) {
+ case 4: goto realign;
+ case 3: goto three_more;
+ case 2: goto two_more;
+ default: goto one_more;
+ }
+ }
+
+ while (src != end) {
+ if ((*src & 0x80) == 0) {
+ decoded = *src++;
+ } else if ((*src & 0xE0) == 0xC0) {
+ min_code = 0x80;
+ decoded = *src++ & 0x1F;
+one_more:
+ if (src == end) {
+ input->utf8_part_decoded_min_code = min_code;
+ input->utf8_part_decoded_char = decoded;
+ input->utf8_part_decoded_rest_len = 1;
+ break;
+ }
+ if ((*src & 0xC0) == 0x80) {
+ decoded = (decoded << 6) | (*src++ & 0x3F);
+ } else {
+ goto invalid_char;
+ }
+ if (decoded < min_code ||
+ decoded > 0x10FFFF ||
+ (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
+ (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
+ (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
+ input_error(0, 0, "invalid byte sequence in input");
+ }
+ } else if ((*src & 0xF0) == 0xE0) {
+ min_code = 0x800;
+ decoded = *src++ & 0x0F;
+two_more:
+ if (src == end) {
+ input->utf8_part_decoded_min_code = min_code;
+ input->utf8_part_decoded_char = decoded;
+ input->utf8_part_decoded_rest_len = 2;
+ break;
+ }
+ if ((*src & 0xC0) == 0x80) {
+ decoded = (decoded << 6) | (*src++ & 0x3F);
+ } else {
+ goto invalid_char;
+ }
+ goto one_more;
+ } else if ((*src & 0xF8) == 0xF0) {
+ min_code = 0x10000;
+ decoded = *src++ & 0x07;
+three_more:
+ if (src == end) {
+ input->utf8_part_decoded_min_code = min_code;
+ input->utf8_part_decoded_char = decoded;
+ input->utf8_part_decoded_rest_len = 3;
+ break;
+ }
+ if ((*src & 0xC0) == 0x80) {
+ decoded = (decoded << 6) | (*src++ & 0x3F);
+ } else {
+ goto invalid_char;
+ }
+ goto two_more;
+ } else {
+invalid_char:
+ input_error(0, 0, "invalid byte sequence in input");
+realign:
+ do {
+ ++src;
+ if (src == end) {
+ input->utf8_part_decoded_rest_len = 4;
+ break;
+ }
+ } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
+ continue;
+ }
+ *dst++ = decoded;
+ }
+
+ /* we're done when we could read more than 1 char */
+ if (buffer != dst)
+ return dst - buffer;
+ }
+}
+
+static size_t decode_windows_1252(input_t *input, utf32 *buffer,
+ size_t buffer_size)
+{
+ unsigned char read_buf[buffer_size];
+ size_t const s = read_block(input->file, read_buf, sizeof(read_buf));
+
+ unsigned char const *src = read_buf;
+ unsigned char const *end = read_buf + s;
+ utf32 *dst = buffer;
+ while (src != end) {
+ utf32 tc = *src++;
+ switch (tc) {
+ case 0x80: tc = 0x20AC; break; // €
+ case 0x82: tc = 0x201A; break; // ‚
+ case 0x83: tc = 0x0192; break; // ƒ
+ case 0x84: tc = 0x201E; break; // „
+ case 0x85: tc = 0x2026; break; // …
+ case 0x86: tc = 0x2020; break; // †
+ case 0x87: tc = 0x2021; break; // ‡
+ case 0x88: tc = 0x02C6; break; // ˆ
+ case 0x89: tc = 0x2030; break; // ‰
+ case 0x8A: tc = 0x0160; break; // Š
+ case 0x8B: tc = 0x2039; break; // ‹
+ case 0x8C: tc = 0x0152; break; // Œ
+ case 0x8E: tc = 0x017D; break; // Ž
+ case 0x91: tc = 0x2018; break; // ‘
+ case 0x92: tc = 0x2019; break; // ’
+ case 0x93: tc = 0x201C; break; // “
+ case 0x94: tc = 0x201D; break; // ”
+ case 0x95: tc = 0x2022; break; // •
+ case 0x96: tc = 0x2013; break; // –
+ case 0x97: tc = 0x2014; break; // —
+ case 0x98: tc = 0x02DC; break; // ˜
+ case 0x99: tc = 0x2122; break; // ™
+ case 0x9A: tc = 0x0161; break; // š
+ case 0x9B: tc = 0x203A; break; // ›
+ case 0x9C: tc = 0x0153; break; // œ
+ case 0x9E: tc = 0x017E; break; // ž
+ case 0x9F: tc = 0x0178; break; // Ÿ
+ }
+ *dst++ = tc;
+ }
+
+ return s;
+}
+
+typedef struct named_decoder_t {
+ char const *name;
+ decode_func decoder;
+} named_decoder_t;
+
+static named_decoder_t const decoders[] = {
+ { "CP819", decode_iso_8859_1 }, // official alias
+ { "IBM819", decode_iso_8859_1 }, // official alias
+ { "ISO-8859-1", decode_iso_8859_1 }, // official alias
+ { "ISO-8859-15", decode_iso_8859_15 }, // official name
+ { "ISO8859-1", decode_iso_8859_1 },
+ { "ISO8859-15", decode_iso_8859_15 },
+ { "ISO_8859-1", decode_iso_8859_1 }, // official alias
+ { "ISO_8859-15", decode_iso_8859_15 }, // official alias
+ { "ISO_8859-1:1987", decode_iso_8859_1 }, // official name
+ { "Latin-9", decode_iso_8859_15 }, // official alias
+ { "UTF-8", decode_utf8 }, // official name
+ { "csISOLatin1", decode_iso_8859_1 }, // official alias
+ { "cp1252", decode_windows_1252 },
+ { "iso-ir-100", decode_iso_8859_1 }, // official alias
+ { "l1", decode_iso_8859_1 }, // official alias
+ { "latin1", decode_iso_8859_1 }, // official alias
+ { "windows-1252", decode_windows_1252 }, // official name
+
+ { NULL, NULL }
+};
+
+/** strcasecmp is not part of C99 so we need our own implementation here */
+static int my_strcasecmp(const char *s1, const char *s2)
+{
+ for ( ; *s1 != 0; ++s1, ++s2) {
+ if (tolower(*s1) != tolower(*s2))
+ break;
+ }
+ return (unsigned char)*s1 - (unsigned char)*s2;
+}
+
+input_t *input_from_stream(FILE *file, const char *encoding)
+{
+ input_t *result = XMALLOCZ(input_t);
+ result->file = file;
+
+ if (encoding == NULL) {
+ result->decode = decode_utf8;
+ } else {
+ for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
+ if (my_strcasecmp(encoding, i->name) != 0)
+ continue;
+ result->decode = i->decoder;
+ break;
+ }
+ if (result->decode == NULL) {
+ fprintf(stderr, "error: input encoding \"%s\" not supported\n",
+ encoding);
+ result->decode = decode_utf8;
+ }
+ }
+
+ return result;
+}
+
+size_t decode(input_t *input, utf32 *buffer, size_t buffer_size)
+{
+ return input->decode(input, buffer, buffer_size);
+}
+
+void input_free(input_t *input)
+{
+ xfree(input);
+}
--- /dev/null
+#ifndef INPUT_H
+#define INPUT_H
+
+#include <stdio.h>
+#include "unicode.h"
+
+typedef struct input_t input_t;
+
+input_t *input_from_stream(FILE *stream, const char *encoding);
+
+/** Type for a function being called on an input (or encoding) errors. */
+typedef void (*input_error_callback_func)(unsigned delta_lines,
+ unsigned delta_cols,
+ const char *message);
+
+void set_input_error_callback(input_error_callback_func func);
+
+size_t decode(input_t *input, utf32 *buffer, size_t buffer_size);
+
+void input_free(input_t *input);
+
+#endif
*/
#include <config.h>
+#include "input.h"
#include "diagnostic.h"
#include "lexer.h"
#include "symbol_t.h"
#include <strings.h>
#endif
-//#define DEBUG_CHARS
-#define MAX_PUTBACK 3
+#define MAX_PUTBACK 16 // 3 would be enough, but 16 gives a nicer alignment
#define BUF_SIZE 1024
-static utf32 c;
-static source_position_t lexer_pos;
-token_t lexer_token;
-static symbol_t *symbol_L;
-static FILE *input;
-static utf32 buf[BUF_SIZE + MAX_PUTBACK];
-static const utf32 *bufend;
-static const utf32 *bufpos;
-static strset_t stringset;
-bool allow_dollar_in_symbol = true;
+static input_t *input;
+static utf32 input_buf[BUF_SIZE + MAX_PUTBACK];
+static const utf32 *bufpos;
+static const utf32 *bufend;
+static utf32 c;
+static source_position_t lexer_pos;
+token_t lexer_token;
+static symbol_t *symbol_L;
+static strset_t stringset;
+static char *encoding;
+bool allow_dollar_in_symbol = true;
/**
* Prints a parse error message at the current token.
internal_errorf(&lexer_pos, "%s", msg);
}
-static size_t read_block(unsigned char *const read_buf, size_t const n)
-{
- size_t const s = fread(read_buf, 1, n, input);
- if (s == 0) {
- /* on OS/X ferror appears to return true on eof as well when running
- * the application in gdb... */
- if (!feof(input) && ferror(input))
- parse_error("read from input failed");
- buf[MAX_PUTBACK] = EOF;
- bufpos = buf + MAX_PUTBACK;
- bufend = buf + MAX_PUTBACK + 1;
- }
- return s;
-}
-
-static void decode_iso_8859_1(void)
-{
- unsigned char read_buf[BUF_SIZE];
- size_t const s = read_block(read_buf, sizeof(read_buf));
- if (s == 0)
- return;
-
- unsigned char const *src = read_buf;
- unsigned char const *end = read_buf + s;
- utf32 *dst = buf + MAX_PUTBACK;
- while (src != end)
- *dst++ = *src++;
-
- bufpos = buf + MAX_PUTBACK;
- bufend = dst;
-}
-
-static void decode_iso_8859_15(void)
-{
- unsigned char read_buf[BUF_SIZE];
- size_t const s = read_block(read_buf, sizeof(read_buf));
- if (s == 0)
- return;
-
- unsigned char const *src = read_buf;
- unsigned char const *end = read_buf + s;
- utf32 *dst = buf + MAX_PUTBACK;
- while (src != end) {
- utf32 tc = *src++;
- switch (tc) {
- case 0xA4: tc = 0x20AC; break; // €
- case 0xA6: tc = 0x0160; break; // Š
- case 0xA8: tc = 0x0161; break; // š
- case 0xB4: tc = 0x017D; break; // Ž
- case 0xB8: tc = 0x017E; break; // ž
- case 0xBC: tc = 0x0152; break; // Œ
- case 0xBD: tc = 0x0153; break; // œ
- case 0xBE: tc = 0x0178; break; // Ÿ
- }
- *dst++ = tc;
- }
-
- bufpos = buf + MAX_PUTBACK;
- bufend = dst;
-}
-
-static void decode_utf8(void)
-{
- static utf32 part_decoded_min_code;
- static utf32 part_decoded_char;
- static size_t part_decoded_rest_len;
-
- do {
- unsigned char read_buf[BUF_SIZE];
- size_t const s = read_block(read_buf, sizeof(read_buf));
- if (s == 0) {
- if (part_decoded_rest_len > 0)
- parse_error("incomplete input char at end of input");
- return;
- }
-
- unsigned char const *src = read_buf;
- unsigned char const *end = read_buf + s;
- utf32 *dst = buf + MAX_PUTBACK;
- utf32 decoded;
- utf32 min_code;
-
- if (part_decoded_rest_len != 0) {
- min_code = part_decoded_min_code;
- decoded = part_decoded_char;
- size_t const rest_len = part_decoded_rest_len;
- part_decoded_rest_len = 0;
- switch (rest_len) {
- case 4: goto realign;
- case 3: goto three_more;
- case 2: goto two_more;
- default: goto one_more;
- }
- }
-
- while (src != end) {
- if ((*src & 0x80) == 0) {
- decoded = *src++;
- } else if ((*src & 0xE0) == 0xC0) {
- min_code = 0x80;
- decoded = *src++ & 0x1F;
-one_more:
- if (src == end) {
- part_decoded_min_code = min_code;
- part_decoded_char = decoded;
- part_decoded_rest_len = 1;
- break;
- }
- if ((*src & 0xC0) == 0x80) {
- decoded = (decoded << 6) | (*src++ & 0x3F);
- } else {
- goto invalid_char;
- }
- if (decoded < min_code ||
- decoded > 0x10FFFF ||
- (0xD800 <= decoded && decoded < 0xE000) || // high/low surrogates
- (0xFDD0 <= decoded && decoded < 0xFDF0) || // noncharacters
- (decoded & 0xFFFE) == 0xFFFE) { // noncharacters
- parse_error("invalid byte sequence in input");
- }
- } else if ((*src & 0xF0) == 0xE0) {
- min_code = 0x800;
- decoded = *src++ & 0x0F;
-two_more:
- if (src == end) {
- part_decoded_min_code = min_code;
- part_decoded_char = decoded;
- part_decoded_rest_len = 2;
- break;
- }
- if ((*src & 0xC0) == 0x80) {
- decoded = (decoded << 6) | (*src++ & 0x3F);
- } else {
- goto invalid_char;
- }
- goto one_more;
- } else if ((*src & 0xF8) == 0xF0) {
- min_code = 0x10000;
- decoded = *src++ & 0x07;
-three_more:
- if (src == end) {
- part_decoded_min_code = min_code;
- part_decoded_char = decoded;
- part_decoded_rest_len = 3;
- break;
- }
- if ((*src & 0xC0) == 0x80) {
- decoded = (decoded << 6) | (*src++ & 0x3F);
- } else {
- goto invalid_char;
- }
- goto two_more;
- } else {
-invalid_char:
- parse_error("invalid byte sequence in input");
-realign:
- do {
- ++src;
- if (src == end) {
- part_decoded_rest_len = 4;
- break;
- }
- } while ((*src & 0xC0) == 0x80 || (*src & 0xF8) == 0xF8);
- continue;
- }
- *dst++ = decoded;
- }
-
- bufpos = buf + MAX_PUTBACK;
- bufend = dst;
- } while (bufpos == bufend);
-}
-
-static void decode_windows_1252(void)
-{
- unsigned char read_buf[BUF_SIZE];
- size_t const s = read_block(read_buf, sizeof(read_buf));
- if (s == 0)
- return;
-
- unsigned char const *src = read_buf;
- unsigned char const *end = read_buf + s;
- utf32 *dst = buf + MAX_PUTBACK;
- while (src != end) {
- utf32 tc = *src++;
- switch (tc) {
- case 0x80: tc = 0x20AC; break; // €
- case 0x82: tc = 0x201A; break; // ‚
- case 0x83: tc = 0x0192; break; // ƒ
- case 0x84: tc = 0x201E; break; // „
- case 0x85: tc = 0x2026; break; // …
- case 0x86: tc = 0x2020; break; // †
- case 0x87: tc = 0x2021; break; // ‡
- case 0x88: tc = 0x02C6; break; // ˆ
- case 0x89: tc = 0x2030; break; // ‰
- case 0x8A: tc = 0x0160; break; // Š
- case 0x8B: tc = 0x2039; break; // ‹
- case 0x8C: tc = 0x0152; break; // Œ
- case 0x8E: tc = 0x017D; break; // Ž
- case 0x91: tc = 0x2018; break; // ‘
- case 0x92: tc = 0x2019; break; // ’
- case 0x93: tc = 0x201C; break; // “
- case 0x94: tc = 0x201D; break; // ”
- case 0x95: tc = 0x2022; break; // •
- case 0x96: tc = 0x2013; break; // –
- case 0x97: tc = 0x2014; break; // —
- case 0x98: tc = 0x02DC; break; // ˜
- case 0x99: tc = 0x2122; break; // ™
- case 0x9A: tc = 0x0161; break; // š
- case 0x9B: tc = 0x203A; break; // ›
- case 0x9C: tc = 0x0153; break; // œ
- case 0x9E: tc = 0x017E; break; // ž
- case 0x9F: tc = 0x0178; break; // Ÿ
- }
- *dst++ = tc;
- }
-
- bufpos = buf + MAX_PUTBACK;
- bufend = dst;
-}
-
-typedef void (*decoder_t)(void);
-
-static decoder_t decoder = decode_utf8;
-
-typedef struct named_decoder_t {
- char const *name;
- decoder_t decoder;
-} named_decoder_t;
-
-static named_decoder_t const decoders[] = {
- { "CP819", decode_iso_8859_1 }, // offical alias
- { "IBM819", decode_iso_8859_1 }, // offical alias
- { "ISO-8859-1", decode_iso_8859_1 }, // offical alias
- { "ISO-8859-15", decode_iso_8859_15 }, // offical name
- { "ISO8859-1", decode_iso_8859_1 },
- { "ISO8859-15", decode_iso_8859_15 },
- { "ISO_8859-1", decode_iso_8859_1 }, // offical alias
- { "ISO_8859-15", decode_iso_8859_15 }, // offical alias
- { "ISO_8859-1:1987", decode_iso_8859_1 }, // offical name
- { "Latin-9", decode_iso_8859_15 }, // offical alias
- { "UTF-8", decode_utf8 }, // offical name
- { "csISOLatin1", decode_iso_8859_1 }, // offical alias
- { "cp1252", decode_windows_1252 },
- { "iso-ir-100", decode_iso_8859_1 }, // offical alias
- { "l1", decode_iso_8859_1 }, // offical alias
- { "latin1", decode_iso_8859_1 }, // offical alias
- { "windows-1252", decode_windows_1252 }, // official name
-
- { NULL, NULL }
-};
-
-/** strcasecmp is not part of C99 so we need our own implementation here */
-static int my_strcasecmp(const char *s1, const char *s2)
-{
- for ( ; *s1 != 0; ++s1, ++s2) {
- if (tolower(*s1) != tolower(*s2))
- break;
- }
- return (unsigned char)*s1 - (unsigned char)*s2;
-}
-
-void select_input_encoding(char const* const encoding)
-{
- for (named_decoder_t const *i = decoders; i->name != NULL; ++i) {
- if (my_strcasecmp(encoding, i->name) != 0)
- continue;
- decoder = i->decoder;
- return;
- }
- fprintf(stderr, "error: input encoding \"%s\" not supported\n", encoding);
-}
-
static inline void next_real_char(void)
{
assert(bufpos <= bufend);
if (bufpos >= bufend) {
- if (input == NULL) {
+ size_t n = decode(input, input_buf+MAX_PUTBACK, BUF_SIZE);
+ if (n == 0) {
c = EOF;
return;
}
- decoder();
+ bufpos = input_buf + MAX_PUTBACK;
+ bufend = bufpos + n;
}
c = *bufpos++;
++lexer_pos.colno;
*/
static inline void put_back(utf32 const pc)
{
- assert(bufpos > buf);
- *(--bufpos - buf + buf) = pc;
+ *(--bufpos - input_buf + input_buf) = pc;
--lexer_pos.colno;
-
-#ifdef DEBUG_CHARS
- printf("putback '%lc'\n", pc);
-#endif
}
static inline void next_char(void);
/* filter trigraphs */
if (UNLIKELY(c == '\\')) {
maybe_concat_lines();
- goto end_of_next_char;
+ return;
}
if (LIKELY(c != '?'))
- goto end_of_next_char;
+ return;
next_real_char();
if (LIKELY(c != '?')) {
put_back(c);
c = '?';
- goto end_of_next_char;
+ return;
}
next_real_char();
c = '?';
break;
}
-
-end_of_next_char:;
-#ifdef DEBUG_CHARS
- printf("nchar '%c'\n", c);
-#endif
}
#define SYMBOL_CHARS \
symbol_L = symbol_table_insert("L");
}
+static void input_error(unsigned delta_lines, unsigned delta_cols,
+ const char *message)
+{
+ lexer_pos.lineno += delta_lines;
+ lexer_pos.colno += delta_cols;
+ errorf(&lexer_pos, "%s", message);
+}
+
+void select_input_encoding(char const* new_encoding)
+{
+ if (encoding != NULL)
+ xfree(encoding);
+ encoding = xstrdup(new_encoding);
+}
+
void lexer_open_stream(FILE *stream, const char *input_name)
{
- input = stream;
+ if (input != NULL) {
+ input_free(input);
+ input = NULL;
+ }
+
lexer_pos.lineno = 0;
lexer_pos.colno = 0;
lexer_pos.input_name = input_name;
+ set_input_error_callback(input_error);
+ input = input_from_stream(stream, encoding);
bufpos = NULL;
bufend = NULL;
void exit_lexer(void)
{
+ if (input != NULL) {
+ input_free(input);
+ input = NULL;
+ }
strset_destroy(&stringset);
}
#ifndef STRING_REP_H
#define STRING_REP_H
-#include <assert.h>
#include <stdlib.h>
+#include "unicode.h"
typedef struct string_t {
const char *begin; /**< UTF-8 encoded string, the last character is
size_t size; /**< size of string in bytes (not characters) */
} string_t;
-typedef unsigned int utf32;
-#define UTF32_PRINTF_FORMAT "%u"
-
-/**
- * "parse" an utf8 character from a string.
- * Warning: This function only works for valid utf-8 inputs. The behaviour
- * is undefined for invalid utf-8 input.
- *
- * @param p A pointer to a pointer into the string. The pointer
- * is incremented for each consumed char
- */
-static inline utf32 read_utf8_char(const char **p)
-{
- const unsigned char *c = (const unsigned char *) *p;
- utf32 result;
-
- if ((*c & 0x80) == 0) {
- /* 1 character encoding: 0b0??????? */
- result = *c++;
- } else if ((*c & 0xE0) == 0xC0) {
- /* 2 character encoding: 0b110?????, 0b10?????? */
- result = *c++ & 0x1F;
- result = (result << 6) | (*c++ & 0x3F);
- } else if ((*c & 0xF0) == 0xE0) {
- /* 3 character encoding: 0b1110????, 0b10??????, 0b10?????? */
- result = *c++ & 0x0F;
- result = (result << 6) | (*c++ & 0x3F);
- result = (result << 6) | (*c++ & 0x3F);
- } else {
- /* 4 character enc.: 0b11110???, 0b10??????, 0b10??????, 0b10?????? */
- assert((*c & 0xF8) == 0xF0);
- result = *c++ & 0x07;
- result = (result << 6) | (*c++ & 0x3F);
- result = (result << 6) | (*c++ & 0x3F);
- result = (result << 6) | (*c++ & 0x3F);
- }
-
- *p = (const char*) c;
- return result;
-}
-
static inline size_t wstrlen(const string_t *string)
{
size_t result = 0;
--- /dev/null
+#ifndef UNICODE_H
+#define UNICODE_H
+
+#include <assert.h>
+
+typedef unsigned int utf32;
+#define UTF32_PRINTF_FORMAT "%u"
+
+/**
+ * "parse" an utf8 character from a string.
+ * Warning: This function only works for valid utf-8 inputs. The behaviour
+ * is undefined for invalid utf-8 input.
+ *
+ * @param p A pointer to a pointer into the string. The pointer
+ * is incremented for each consumed char
+ */
+static inline utf32 read_utf8_char(const char **p)
+{
+ const unsigned char *c = (const unsigned char *) *p;
+ utf32 result;
+
+ if ((*c & 0x80) == 0) {
+ /* 1 character encoding: 0b0??????? */
+ result = *c++;
+ } else if ((*c & 0xE0) == 0xC0) {
+ /* 2 character encoding: 0b110?????, 0b10?????? */
+ result = *c++ & 0x1F;
+ result = (result << 6) | (*c++ & 0x3F);
+ } else if ((*c & 0xF0) == 0xE0) {
+ /* 3 character encoding: 0b1110????, 0b10??????, 0b10?????? */
+ result = *c++ & 0x0F;
+ result = (result << 6) | (*c++ & 0x3F);
+ result = (result << 6) | (*c++ & 0x3F);
+ } else {
+ /* 4 character enc.: 0b11110???, 0b10??????, 0b10??????, 0b10?????? */
+ assert((*c & 0xF8) == 0xF0);
+ result = *c++ & 0x07;
+ result = (result << 6) | (*c++ & 0x3F);
+ result = (result << 6) | (*c++ & 0x3F);
+ result = (result << 6) | (*c++ & 0x3F);
+ }
+
+ *p = (const char*) c;
+ return result;
+}
+
+#endif