X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Futf.c;h=dda80f0d1b07f2d8659e3c2b11dc30028c623f92;hb=6637b976edd508cc9413ce954fe4275eab8c2c69;hp=0adf01b5a1ee2414fa8bd6fcacc9dfafa4ab1afb;hpb=9240146c102f97045836d33fa1a79b267a050172;p=jansson.git diff --git a/src/utf.c b/src/utf.c index 0adf01b..dda80f0 100644 --- a/src/utf.c +++ b/src/utf.c @@ -1,6 +1,14 @@ +/* + * Copyright (c) 2009 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + #include +#include -int utf8_encode(int codepoint, char *buffer, int *size) +int utf8_encode(int32_t codepoint, char *buffer, int *size) { if(codepoint < 0) return -1; @@ -72,9 +80,10 @@ int utf8_check_first(char byte) } } -int utf8_check_full(const char *buffer, int size) +int utf8_check_full(const char *buffer, int size, int32_t *codepoint) { - int i, value = 0; + int i; + int32_t value = 0; unsigned char u = (unsigned char)buffer[0]; if(size == 2) @@ -121,9 +130,38 @@ int utf8_check_full(const char *buffer, int size) return 0; } + if(codepoint) + *codepoint = value; + return 1; } +const char *utf8_iterate(const char *buffer, int32_t *codepoint) +{ + int count; + int32_t value; + + if(!*buffer) + return buffer; + + count = utf8_check_first(buffer[0]); + if(count <= 0) + return NULL; + + if(count == 1) + value = (unsigned char)buffer[0]; + else + { + if(!utf8_check_full(buffer, count, &value)) + return NULL; + } + + if(codepoint) + *codepoint = value; + + return buffer + count; +} + int utf8_check_string(const char *string, int length) { int i; @@ -141,7 +179,7 @@ int utf8_check_string(const char *string, int length) if(i + count > length) return 0; - if(!utf8_check_full(&string[i], count)) + if(!utf8_check_full(&string[i], count, NULL)) return 0; i += count - 1;