"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "utf8.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 UTF-8 wrapper for teapot
3 Copyright (C) 2010 Joerg Walter
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "utf8.h"
20
21 #include <string.h>
22
23 #ifdef ENABLE_UTF8
24
25 size_t mbslen(const char *s)
26 {
27 const unsigned char *str = (unsigned char *)s;
28 size_t chars = 0;
29
30 while (*str) {
31 chars++;
32 if (is_mbchar(*str++)) while (is_mbcont(*str)) str++;
33 }
34
35 return chars;
36 }
37
38 char *mbspos(const char *s, int ch)
39 {
40 const unsigned char *str = (unsigned char *)s;
41 int chars = 0;
42
43 if (ch < 0) {
44 while (chars > ch) {
45 chars--;
46 while (is_mbcont(*--str));
47 }
48 } else {
49 while (chars < ch && *str) {
50 chars++;
51 if (is_mbchar(*str++)) while (is_mbcont(*str)) str++;
52 }
53 }
54
55 return (char *)(void *)str;
56 }
57
58 #else
59
60 size_t mbslen(const char *str)
61 {
62 return strlen(str);
63 }
64
65 char *mbspos(const char *str, int ch)
66 {
67 return (char *)(void *)(str+ch);
68 }
69
70 #endif