FreeSWITCH API Documentation  1.7.0
switch_utf8.h
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Seven Du <dujinfang@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  * Anthony Minessale II <anthm@freeswitch.org>
26  *
27  *
28  * switch_utf8.h UTf8
29  *
30  */
31 
32 
33 /*
34  Basic UTF-8 manipulation routines
35  by Jeff Bezanson
36  placed in the public domain Fall 2005
37 
38  This code is designed to provide the utilities you need to manipulate
39  UTF-8 as an internal string encoding. These functions do not perform the
40  error checking normally needed when handling UTF-8 data, so if you happen
41  to be from the Unicode Consortium you will want to flay me alive.
42  I do this because error checking can be performed at the boundaries (I/O),
43  with these routines reserved for higher performance on data known to be
44  valid.
45 */
46 
47 #include <switch.h>
48 
49 /* is c the start of a utf8 sequence? */
50 #define isutf(c) (((c)&0xC0)!=0x80)
51 
52 /* convert UTF-8 data to wide character */
53 SWITCH_DECLARE(int) switch_u8_toucs(uint32_t *dest, int sz, char *src, int srcsz);
54 
55 /* the opposite conversion */
56 SWITCH_DECLARE(int) switch_u8_toutf8(char *dest, int sz, uint32_t *src, int srcsz);
57 
58 /* single character to UTF-8 */
59 SWITCH_DECLARE(int) switch_u8_wc_toutf8(char *dest, uint32_t ch);
60 
61 /* character number to byte offset */
62 SWITCH_DECLARE(int) switch_u8_offset(char *str, int charnum);
63 
64 /* byte offset to character number */
65 SWITCH_DECLARE(int) switch_u8_charnum(char *s, int offset);
66 
67 /* return next character, updating an index variable */
68 SWITCH_DECLARE(uint32_t) switch_u8_nextchar(char *s, int *i);
69 
70 /* move to next character */
71 SWITCH_DECLARE(void) switch_u8_inc(char *s, int *i);
72 
73 /* move to previous character */
74 SWITCH_DECLARE(void) switch_u8_dec(char *s, int *i);
75 
76 /* returns length of next utf-8 sequence */
77 SWITCH_DECLARE(int) switch_u8_seqlen(char *s);
78 
79 /* assuming src points to the character after a backslash, read an
80  escape sequence, storing the result in dest and returning the number of
81  input characters processed */
82 SWITCH_DECLARE(int) switch_u8_read_escape_sequence(char *src, uint32_t *dest);
83 
84 /* given a wide character, convert it to an ASCII escape sequence stored in
85  buf, where buf is "sz" bytes. returns the number of characters output.*/
86 SWITCH_DECLARE(int) switch_u8_escape_wchar(char *buf, int sz, uint32_t ch);
87 
88 /* convert a string "src" containing escape sequences to UTF-8 */
89 SWITCH_DECLARE(int) switch_u8_unescape(char *buf, int sz, char *src);
90 
91 /* convert UTF-8 "src" to ASCII with escape sequences.
92  if escape_quotes is nonzero, quote characters will be preceded by
93  backslashes as well. */
94 SWITCH_DECLARE(int) switch_u8_escape(char *buf, int sz, char *src, int escape_quotes);
95 
96 /* utility predicates used by the above */
97 int octal_digit(char c);
98 int hex_digit(char c);
99 
100 /* return a pointer to the first occurrence of ch in s, or NULL if not
101  found. character index of found character returned in *charn. */
102 SWITCH_DECLARE(char *) switch_u8_strchr(char *s, uint32_t ch, int *charn);
103 
104 /* same as the above, but searches a buffer of a given size instead of
105  a NUL-terminated string. */
106 SWITCH_DECLARE(char *) switch_u8_memchr(char *s, uint32_t ch, size_t sz, int *charn);
107 
108 /* count the number of characters in a UTF-8 string */
109 SWITCH_DECLARE(int) switch_u8_strlen(char *s);
110 
111 SWITCH_DECLARE(int) switch_u8_is_locale_utf8(char *locale);
112 
113 SWITCH_DECLARE(uint32_t) switch_u8_get_char(char *s, int *i);
int switch_u8_escape_wchar(char *buf, int sz, uint32_t ch)
Definition: switch_utf8.c:357
void switch_u8_inc(char *s, int *i)
Definition: switch_utf8.c:247
int hex_digit(char c)
Definition: switch_utf8.c:264
int switch_u8_charnum(char *s, int offset)
Definition: switch_utf8.c:207
int switch_u8_read_escape_sequence(char *src, uint32_t *dest)
Definition: switch_utf8.c:273
int switch_u8_offset(char *str, int charnum)
Definition: switch_utf8.c:194
int switch_u8_unescape(char *buf, int sz, char *src)
Definition: switch_utf8.c:330
int switch_u8_seqlen(char *s)
Definition: switch_utf8.c:65
int switch_u8_escape(char *buf, int sz, char *src, int escape_quotes)
Definition: switch_utf8.c:385
uint32_t switch_u8_nextchar(char *s, int *i)
Definition: switch_utf8.c:232
switch_byte_t switch_byte_t * buf
char * switch_u8_memchr(char *s, uint32_t ch, size_t sz, int *charn)
Definition: switch_utf8.c:422
void switch_u8_dec(char *s, int *i)
Definition: switch_utf8.c:253
Main Library Header.
int switch_u8_toutf8(char *dest, int sz, uint32_t *src, int srcsz)
Definition: switch_utf8.c:125
#define SWITCH_DECLARE(type)
char * switch_u8_strchr(char *s, uint32_t ch, int *charn)
Definition: switch_utf8.c:405
int switch_u8_is_locale_utf8(char *locale)
Definition: switch_utf8.c:447
uint32_t switch_u8_get_char(char *s, int *i)
Definition: switch_utf8.c:469
int switch_u8_toucs(uint32_t *dest, int sz, char *src, int srcsz)
Definition: switch_utf8.c:80
int switch_u8_strlen(char *s)
Definition: switch_utf8.c:220
int octal_digit(char c)
Definition: switch_utf8.c:259
int switch_u8_wc_toutf8(char *dest, uint32_t ch)
Definition: switch_utf8.c:166