]> mj.ucw.cz Git - libucw.git/blob - charset/utf8.c
Audited usage of MAX_WORD_LEN, fixed several bugs and documented
[libucw.git] / charset / utf8.c
1 /*
2  *      The UniCode Library -- UTF-8 Functions
3  *
4  *      (c) 1997 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "charset/unicode.h"
12
13 uns
14 ucs2_to_utf8(byte *d, word *s)
15 {
16   byte *d0 = d;
17
18   while (*s)
19     {
20       uns u = *s++;
21       PUT_UTF8(d,u);
22     }
23   *d = 0;
24   return d - d0;
25 }
26
27 uns
28 utf8_to_ucs2(word *d, byte *s)
29 {
30   word *d0 = d;
31
32   while (*s)
33     if (IS_UTF8(*s))
34       {
35         uns u;
36         GET_UTF8_CHAR(s,u);
37         *d++ = u;
38       }
39     else if (*s >= 0x80)
40       *d++ = UNI_REPLACEMENT;
41     else
42       *d++ = *s++;
43   *d = 0;
44   return d - d0;
45 }