]> mj.ucw.cz Git - libucw.git/blob - charset/utf8.c
Added macro for fetching of u32's aligned on 2-byte boundary.
[libucw.git] / charset / utf8.c
1 /*
2  *      The UniCode Library -- UTF-8 Functions
3  *
4  *      (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "charset/unicode.h"
9
10 uns
11 ucs2_to_utf8(byte *d, word *s)
12 {
13   byte *d0 = d;
14
15   while (*s)
16     {
17       uns u = *s++;
18       PUT_UTF8(d,u);
19     }
20   *d = 0;
21   return d - d0;
22 }
23
24 uns
25 utf8_to_ucs2(word *d, byte *s)
26 {
27   word *d0 = d;
28
29   while (*s)
30     if (IS_UTF8(*s))
31       {
32         uns u;
33         GET_UTF8_CHAR(s,u);
34         *d++ = u;
35       }
36     else if (*s >= 0x80)
37       *d++ = UNI_REPLACEMENT;
38     else
39       *d++ = *s++;
40   *d = 0;
41   return d0 - d;
42 }