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
16#ifndef UTF8_H
17#define UTF8_H
18
19#include <stdio.h>
20#include <stdarg.h>
21#include <wchar.h>
22#include <wctype.h>
23#include <stdarg.h>
24
25#include <das3/defs.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31extern int locale_is_utf8;
32
33/* is c the start of a utf8 sequence? */
34#define isutf(c) (((c)&0xC0)!=0x80)
35
36#define UEOF ((uint32_t)-1)
37
38/* convert UTF-8 data to wide character */
39DAS_API size_t u8_toucs(uint32_t *dest, size_t sz, const char *src, size_t srcsz);
40
41/* the opposite conversion */
42DAS_API size_t u8_toutf8(char *dest, size_t sz, const uint32_t *src, size_t srcsz);
43
44/* single character to UTF-8, returns # bytes written */
45DAS_API size_t u8_wc_toutf8(char *dest, uint32_t ch);
46
47/* character number to byte offset */
48DAS_API size_t u8_offset(const char *str, size_t charnum);
49
50/* byte offset to character number */
51DAS_API size_t u8_charnum(const char *s, size_t offset);
52
53/* return next character, updating an index variable */
54DAS_API uint32_t u8_nextchar(const char *s, size_t *i);
55
56/* next character without NUL character terminator */
57DAS_API uint32_t u8_nextmemchar(const char *s, size_t *i);
58
59/* move to next character */
60DAS_API void u8_inc(const char *s, size_t *i);
61
62/* move to previous character */
63DAS_API void u8_dec(const char *s, size_t *i);
64
65/* returns length of next utf-8 sequence */
66DAS_API size_t u8_seqlen(const char *s);
67
68/* returns the # of bytes needed to encode a certain character */
69DAS_API size_t u8_charlen(uint32_t ch);
70
71/* computes the # of bytes needed to encode a WC string as UTF-8 */
72DAS_API size_t u8_codingsize(uint32_t *wcstr, size_t n);
73
74DAS_API char read_escape_control_char(char c);
75
76/* assuming src points to the character after a backslash, read an
77 escape sequence, storing the result in dest and returning the number of
78 input characters processed */
79DAS_API size_t u8_read_escape_sequence(const char *src, size_t ssz, uint32_t *dest);
80
81/* given a wide character, convert it to an ASCII escape sequence stored in
82 buf, where buf is "sz" bytes. returns the number of characters output.
83 sz must be at least 3. */
84DAS_API int u8_escape_wchar(char *buf, size_t sz, uint32_t ch);
85
86/* convert a string "src" containing escape sequences to UTF-8 */
87DAS_API size_t u8_unescape(char *buf, size_t sz, const char *src);
88
89/* convert UTF-8 "src" to escape sequences.
90
91 sz is buf size in bytes. must be at least 12.
92
93 if escape_quotes is nonzero, quote characters will be escaped.
94
95 if ascii is nonzero, the output is 7-bit ASCII, no UTF-8 survives.
96
97 starts at src[*pi], updates *pi to point to the first unprocessed
98 byte of the input.
99
100 end is one more than the last allowable value of *pi.
101
102 returns number of bytes placed in buf, including a NUL terminator.
103*/
104DAS_API size_t u8_escape(char *buf, size_t sz, const char *src, size_t *pi, size_t end,
105 int escape_quotes, int ascii);
106
107/* utility predicates used by the above */
108int octal_digit(char c);
109int hex_digit(char c);
110
111/* return a pointer to the first occurrence of ch in s, or NULL if not
112 found. character index of found character returned in *charn. */
113DAS_API char *u8_strchr(const char *s, uint32_t ch, size_t *charn);
114
115/* same as the above, but searches a buffer of a given size instead of
116 a NUL-terminated string. */
117DAS_API char *u8_memchr(const char *s, uint32_t ch, size_t sz, size_t *charn);
118
119DAS_API char *u8_memrchr(const char *s, uint32_t ch, size_t sz);
120
121/* count the number of characters in a UTF-8 string */
122DAS_API size_t u8_strlen(const char *s);
123
124/* number of columns occupied by a string */
125DAS_API size_t u8_strwidth(const char *s);
126
127/* Only works on Linux.
128 * TODO: Update this function to pickup Windows Code-page 65001, which is
129 * a UTF-8 implementation
130 */
131DAS_API int u8_is_locale_utf8(const char *locale);
132
133/* printf where the format string and arguments may be in UTF-8.
134 you can avoid this function and just use ordinary printf() if the current
135 locale is UTF-8. */
136DAS_API size_t u8_vprintf(const char *fmt, va_list ap);
137DAS_API size_t u8_printf(const char *fmt, ...);
138
139/* determine whether a sequence of bytes is valid UTF-8. length is in bytes */
140DAS_API int u8_isvalid(const char *str, size_t length);
141
142/* reverse a UTF-8 string. len is length in bytes. dest and src must both
143 be allocated to at least len+1 bytes. returns 1 for error, 0 otherwise */
144DAS_API int u8_reverse(char *dest, char *src, size_t len);
145
152DAS_API char* u8_strncpy(char* dest, const char* src, size_t len);
153
154#ifdef __cplusplus
155}
156#endif
157
158
159#endif
Minimal definitions for das2 utilities that can safely be run without calling das_init().