FreeBASIC  0.91.0
utf_core.c
Go to the documentation of this file.
1 /* wstring to UTF conversion
2  * (based on ConvertUTF.c free implementation from Unicode, Inc)
3  */
4 
5 #include "fb.h"
6 
8  {
9  0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
10  };
11 
12 const char __fb_utf8_trailingTb[256] =
13  {
14  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
15  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
16  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
17  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
18  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
19  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
21  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
22  };
23 
25  {
26  0x00000000UL, 0x00003080UL, 0x000E2080UL, 0x03C82080UL, 0xFA082080UL, 0x82082080UL
27  };
28 
29 void fb_hCharToUTF8( const char *src, ssize_t chars, char *dst, ssize_t *total_bytes )
30 {
31  UTF_8 c;
32 
33  *total_bytes = 0;
34  while( chars > 0 )
35  {
36  c = *src++;
37  if( c < 0x80 )
38  {
39  *dst++ = c;
40  *total_bytes += 1;
41  }
42  else
43  {
44  *dst++ = 0xC0 | (c >> 6);
45  *dst++ = ((c | UTF8_BYTEMARK) & UTF8_BYTEMASK);
46  *total_bytes += 2;
47  }
48 
49  --chars;
50  }
51 }