das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
utf8.h
1/*
2 Basic UTF-8 manipulation routines
3 by Jeff Bezanson
4 placed in the public domain Fall 2005
5
6 This code is designed to provide the utilities you need to manipulate
7 UTF-8 as an internal string encoding. These functions do not perform the
8 error checking normally needed when handling UTF-8 data, so if you happen
9 to be from the Unicode Consortium you will want to flay me alive.
10 I do this because error checking can be performed at the boundaries (I/O),
11 with these routines reserved for higher performance on data known to be
12 valid.
13 A UTF-8 validation routine is included.
14
15 das2C: nothing here is exported from das3.dll or libdas3.so, link libdas3.a
16 (libdas3.lib on Windows) to use it.
17*/
18
19#ifndef UTF8_H
20#define UTF8_H
21
22#include <stdio.h>
23#include <stdarg.h>
24#include <wchar.h>
25#include <wctype.h>
26#include <stdarg.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* is c the start of a utf8 sequence? */
33#define isutf(c) (((c)&0xC0)!=0x80)
34
35#define UEOF ((uint32_t)-1)
36
37/* convert UTF-8 data to wide character */
38size_t u8_toucs(uint32_t *dest, size_t sz, const char *src, size_t srcsz);
39
40/* the opposite conversion */
41size_t u8_toutf8(char *dest, size_t sz, const uint32_t *src, size_t srcsz);
42
43/* single character to UTF-8, returns # bytes written */
44size_t u8_wc_toutf8(char *dest, uint32_t ch);
45
46/* character number to byte offset */
47size_t u8_offset(const char *str, size_t charnum);
48
49/* byte offset to character number */
50size_t u8_charnum(const char *s, size_t offset);
51
52/* return next character, updating an index variable */
53uint32_t u8_nextchar(const char *s, size_t *i);
54
55/* next character without NUL character terminator */
56uint32_t u8_nextmemchar(const char *s, size_t *i);
57
58/* move to next character */
59void u8_inc(const char *s, size_t *i);
60
61/* move to previous character */
62void u8_dec(const char *s, size_t *i);
63
64/* returns length of next utf-8 sequence */
65size_t u8_seqlen(const char *s);
66
67/* returns the # of bytes needed to encode a certain character */
68size_t u8_charlen(uint32_t ch);
69
70/* computes the # of bytes needed to encode a WC string as UTF-8 */
71size_t u8_codingsize(uint32_t *wcstr, size_t n);
72
73char read_escape_control_char(char c);
74
75/* assuming src points to the character after a backslash, read an
76 escape sequence, storing the result in dest and returning the number of
77 input characters processed */
78size_t u8_read_escape_sequence(const char *src, size_t ssz, uint32_t *dest);
79
80/* given a wide character, convert it to an ASCII escape sequence stored in
81 buf, where buf is "sz" bytes. returns the number of characters output.
82 sz must be at least 3. */
83int u8_escape_wchar(char *buf, size_t sz, uint32_t ch);
84
85/* convert a string "src" containing escape sequences to UTF-8 */
86size_t u8_unescape(char *buf, size_t sz, const char *src);
87
88/* convert UTF-8 "src" to escape sequences.
89
90 sz is buf size in bytes. must be at least 12.
91
92 if escape_quotes is nonzero, quote characters will be escaped.
93
94 if ascii is nonzero, the output is 7-bit ASCII, no UTF-8 survives.
95
96 starts at src[*pi], updates *pi to point to the first unprocessed
97 byte of the input.
98
99 end is one more than the last allowable value of *pi.
100
101 returns number of bytes placed in buf, including a NUL terminator.
102*/
103size_t u8_escape(char *buf, size_t sz, const char *src, size_t *pi, size_t end,
104 int escape_quotes, int ascii);
105
106/* return a pointer to the first occurrence of ch in s, or NULL if not
107 found. character index of found character returned in *charn. */
108char *u8_strchr(const char *s, uint32_t ch, size_t *charn);
109
110/* same as the above, but searches a buffer of a given size instead of
111 a NUL-terminated string. */
112char *u8_memchr(const char *s, uint32_t ch, size_t sz, size_t *charn);
113
114char *u8_memrchr(const char *s, uint32_t ch, size_t sz);
115
116/* count the number of characters in a UTF-8 string */
117size_t u8_strlen(const char *s);
118
119/* number of columns occupied by a string */
120size_t u8_strwidth(const char *s);
121
122/* Only works on Linux.
123 * TODO: Update this function to pickup Windows Code-page 65001, which is
124 * a UTF-8 implementation
125 */
126int u8_is_locale_utf8(const char *locale);
127
128/* printf where the format string and arguments may be in UTF-8.
129 you can avoid this function and just use ordinary printf() if the current
130 locale is UTF-8. */
131size_t u8_vprintf(const char *fmt, va_list ap);
132size_t u8_printf(const char *fmt, ...);
133
134/* determine whether a sequence of bytes is valid UTF-8. length is in bytes */
135int u8_isvalid(const char *str, size_t length);
136
137/* reverse a UTF-8 string. len is length in bytes. dest and src must both
138 be allocated to at least len+1 bytes. returns 1 for error, 0 otherwise */
139int u8_reverse(char *dest, char *src, size_t len);
140
147char* u8_strncpy(char* dest, const char* src, size_t len);
148
149#ifdef __cplusplus
150}
151#endif
152
153
154#endif