utf8rewind
 All Files Functions Typedefs Macros Groups Pages
utf8rewind.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 Quinten Lansu
3 
4  Permission is hereby granted, free of charge, to any person
5  obtaining a copy of this software and associated documentation
6  files (the "Software"), to deal in the Software without
7  restriction, including without limitation the rights to use,
8  copy, modify, merge, publish, distribute, sublicense, and/or
9  sell copies of the Software, and to permit persons to whom the
10  Software is furnished to do so, subject to the following
11  conditions:
12 
13  The above copyright notice and this permission notice shall be
14  included in all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
31 #ifndef _UTF8REWIND_H_
32 #define _UTF8REWIND_H_
33 
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <wchar.h>
41 
42 #define UTF8_ERR_INVALID_DATA (-1)
43 #define UTF8_ERR_NOT_ENOUGH_SPACE (-2)
44 #define UTF8_ERR_UNMATCHED_HIGH_SURROGATE_PAIR (-3)
45 #define UTF8_ERR_UNMATCHED_LOW_SURROGATE_PAIR (-4)
46 
49 
50 #ifndef UTF8_WCHAR_SIZE
51  #if (__SIZEOF_WCHAR_T__ == 4) || (WCHAR_MAX > UINT16_MAX) || (__WCHAR_MAX__ > UINT16_MAX)
52  #define UTF8_WCHAR_SIZE (4)
53  #else
54  #define UTF8_WCHAR_SIZE (2)
55  #endif
56 #endif
57 
58 #if (UTF8_WCHAR_SIZE == 4)
59  #define UTF8_WCHAR_UTF32 (1)
60 #elif (UTF8_WCHAR_SIZE == 2)
61  #define UTF8_WCHAR_UTF16 (1)
62 #else
63  #error Invalid size for wchar_t type.
64 #endif
65 
67 
68 #if defined(__cplusplus)
69 extern "C" {
70 #endif
71 
72 typedef uint16_t utf16_t;
73 typedef uint32_t unicode_t;
75 
91 size_t utf8len(const char* text);
92 
94 
133 size_t utf16toutf8(const utf16_t* input, size_t inputSize, char* target, size_t targetSize, int32_t* errors);
134 
136 
193 size_t utf32toutf8(const unicode_t* input, size_t inputSize, char* target, size_t targetSize, int32_t* errors);
194 
196 
252 size_t widetoutf8(const wchar_t* input, size_t inputSize, char* target, size_t targetSize, int32_t* errors);
253 
255 
295 size_t utf8toutf16(const char* input, size_t inputSize, utf16_t* target, size_t targetSize, int32_t* errors);
296 
298 
338 size_t utf8toutf32(const char* input, size_t inputSize, unicode_t* target, size_t targetSize, int32_t* errors);
339 
341 
407 size_t utf8towide(const char* input, size_t inputSize, wchar_t* target, size_t targetSize, int32_t* errors);
408 
410 
458 const char* utf8seek(const char* text, const char* textStart, off_t offset, int direction);
459 
460 #if defined(__cplusplus)
461 }
462 #endif
463 
464 #endif
size_t utf8towide(const char *input, size_t inputSize, wchar_t *target, size_t targetSize, int32_t *errors)
Convert a UTF-8 encoded string to a wide string.
size_t utf8toutf16(const char *input, size_t inputSize, utf16_t *target, size_t targetSize, int32_t *errors)
Convert a UTF-8 encoded string to a UTF-16 encoded string.
size_t utf32toutf8(const unicode_t *input, size_t inputSize, char *target, size_t targetSize, int32_t *errors)
Convert a UTF-32 encoded string to a UTF-8 encoded string.
size_t utf8len(const char *text)
Get the length in codepoints of a UTF-8 encoded string.
const char * utf8seek(const char *text, const char *textStart, off_t offset, int direction)
Seek into a UTF-8 encoded string.
size_t utf16toutf8(const utf16_t *input, size_t inputSize, char *target, size_t targetSize, int32_t *errors)
Convert a UTF-16 encoded string to a UTF-8 encoded string.
size_t utf8toutf32(const char *input, size_t inputSize, unicode_t *target, size_t targetSize, int32_t *errors)
Convert a UTF-8 encoded string to a UTF-32 encoded string.
uint16_t utf16_t
Definition: utf8rewind.h:72
uint32_t unicode_t
Definition: utf8rewind.h:73
size_t widetoutf8(const wchar_t *input, size_t inputSize, char *target, size_t targetSize, int32_t *errors)
Convert a wide string to a UTF-8 encoded string.