]> mj.ucw.cz Git - libucw.git/blob - lib/unicode-utf8.c
Rewritten ff-binary.
[libucw.git] / lib / unicode-utf8.c
1 /*
2  *      UCW Library -- UTF-8 Functions
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2003 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/unicode.h"
13
14 uns
15 utf8_strlen(byte *str)
16 {
17   uns len = 0;
18   while (*str)
19     {
20       UTF8_SKIP(str);
21       len++;
22     }
23   return len;
24 }
25
26 uns
27 utf8_strnlen(byte *str, uns n)
28 {
29   uns len = 0;
30   byte *end = str + n;
31   while (str < end)
32     {
33       UTF8_SKIP(str);
34       len++;
35     }
36   return len;
37 }
38
39 #ifdef TEST
40 #include <string.h>
41 #include <stdio.h>
42 int main(int argc, char **argv)
43 {
44   byte buf[256];
45   if (argc > 1 && !strncmp(argv[1], "get", 3))
46     {
47       int f32 = !strcmp(argv[1], "get32");
48       byte *p = buf;
49       uns u;
50       while (scanf("%x", &u) == 1)
51         *p++ = u;
52       *p = 0;
53       p = buf;
54       while (*p)
55         {
56           if (p != buf)
57             putchar(' ');
58           if (f32)
59             GET_UTF8_32(p, u);
60           else
61             GET_UTF8(p, u);
62           printf("%04x", u);
63         }
64       putchar('\n');
65     }
66   else if (argc > 1 && !strncmp(argv[1], "put", 3))
67     {
68       uns u, i=0;
69       int f32 = !strcmp(argv[1], "put32");
70       while (scanf("%x", &u) == 1)
71         {
72           byte *p = buf;
73           if (f32)
74             PUT_UTF8_32(p, u);
75           else
76             PUT_UTF8(p, u);
77           *p = 0;
78           for (p=buf; *p; p++)
79             {
80               if (i++)
81                 putchar(' ');
82               printf("%02x", *p);
83             }
84         }
85       putchar('\n');
86     }
87   else
88     puts("?");
89   return 0;
90 }
91 #endif