root/trunk/thune/encoding.c

Revision 550, 5.0 kB (checked in by krobillard, 7 weeks ago)

Thune:

  • 8-bit string encoding is now Latin-1.
  • Now using WELL512a generator for random numbers.
  • Added hash-map datatype. List datatype can now be disabled in config.
  • Added project-point, remap.
  • Unique & fill now handle vector!.
  • File port 'read now retuns none when end of file reached.

Thune-GL:

  • Added draw-prog! & vertex-buffer! datatypes.
  • Display now accepts /fullscreen option.
  • Added particle-sim dialect.
Line 
1/*============================================================================
2    Urlan Interpreter
3    Copyright (C) 2005-2007  Karl Robillard
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18===========================================================================*/
19
20
21#include <urlan.h>
22/*
23#include <urlan_atoms.h>
24*/
25
26
27int ur_encodingCharSize[ UR_ENC_COUNT ] = { 1, 1, 2 };
28
29
30/*
31   Returns number of characters copied.
32*/
33int copyLatin1ToUtf8( uint8_t* dest, const uint8_t* src, int len )
34{
35    // TODO: Prevent overflow of dest.
36    const uint8_t* start;
37    const uint8_t* end;
38    uint16_t c;
39
40    start = dest;
41    end = src + len;
42
43    while( src != end )
44    {
45        c = *src++;
46        if( c > 127 )
47        {
48            *dest++ = 0xC0 | (c >> 6);
49            c = 0x80 | (c & 0x3f);
50        }
51        *dest++ = c;
52    }
53    return dest - start;
54}
55
56
57/*
58   Returns number of characters copied.
59*/
60int copyUcs2ToUtf8( uint8_t* dest, const uint16_t* src, int len )
61{
62    // TODO: Prevent overflow of dest.
63    const uint8_t* start;
64    const uint16_t* end;
65    uint16_t c;
66
67    start = dest;
68    end = src + len;
69
70    while( src != end )
71    {
72        c = *src++;
73        if( c > 127 )
74        {
75            if( c > 0x07ff )
76            {
77                *dest++ = 0xE0 | (c >> 12);
78                *dest++ = 0x80 | ((c >> 6) & 0x3f);
79                c = 0x80 | (c & 0x3f);
80            }
81            else
82            {
83                *dest++ = 0xC0 | (c >> 6);
84                c = 0x80 | (c & 0x3f);
85            }
86        }
87        *dest++ = c;
88    }
89    return dest - start;
90}
91
92
93#if 0
94int copyUcs2ToAscii( char* dest, const uint16_t* src, int len )
95{
96    const uint16_t* end;
97    uint16_t c;
98
99    end = src + len;
100
101    while( src != end )
102    {
103        c = *src++;
104        if( c > 127 )
105            c = 0;
106        *dest++ = (char) c;
107    }
108    return len;
109}
110#endif
111
112
113/*
114   Returns number of characters copied.
115*/
116int copyAsciiToUtf16( uint16_t* dest, const uint8_t* src, int len )
117{
118    const uint8_t* end = src + len;
119    while( src != end )
120        *dest++ = *src++;
121    return len;
122}
123
124
125// (str enc -- new-str)
126UR_CALL( uc_encode )
127{
128    UIndex strN;
129    UCell* res;
130    uint8_t* cpA;
131    uint8_t* cpB;
132    UBinary* bin;
133    int count;
134    int enc;
135
136    UR_S_DROP;
137    res = UR_TOS;
138
139    if( ur_isAWord(tos) && ur_stringSlice(ut, res, &cpA, &cpB) )
140    {
141        count = cpB - cpA;
142
143        switch( ur_atom(tos) )
144        {
145            case UR_ATOM_LATIN1:
146                switch( ur_encoding(res) )
147                {
148                    case UR_ENC_UCS2:
149                        count >>= 1;
150                        strN = ur_makeBinary( count * 2, &bin );
151                        bin->used = copyUcs2ToUtf8( bin->ptr.b,
152                                                    (uint16_t*) cpA, count );
153                        enc = UR_ENC_LATIN1;
154                        goto set_result;
155                }
156                break;
157
158            case UR_ATOM_UTF8:
159                /*
160                enc = UR_ENC_UTF8;
161                switch( ur_encoding(res) )
162                {
163                    case UR_ENC_LATIN1:
164                        goto set_result;
165
166                    case UR_ENC_UCS2:
167                        goto set_result;
168                }
169                */
170                break;
171
172            case UR_ATOM_UCS2:
173                switch( ur_encoding(res) )
174                {
175                    case UR_ENC_LATIN1:
176                        strN = ur_makeBinary( count * 2, &bin );
177                        bin->used = copyAsciiToUtf16( bin->ptr.u16,
178                                                      cpA, count );
179                        enc = UR_ENC_UCS2;
180                        goto set_result;
181                }
182                break;
183        }
184    }
185    return;
186
187set_result:
188
189    ur_initType(res, UT_STRING);
190    ur_setEncoding(res, enc);
191    ur_setSeries(res, strN, 0);
192}
193
194
195// (str -- enc)
196UR_CALL( uc_encodingQ )
197{
198    UR_CALL_UNUSED_TH
199
200    if( ur_is(tos, UT_STRING) )
201    {
202        UAtom atom = UR_ATOM_LATIN1;
203
204        switch( ur_encoding(tos) )
205        {
206            //case UR_ENC_LATIN1: atom = UR_ATOM_LATIN1; break;
207            case UR_ENC_UTF8:  atom = UR_ATOM_UTF8;  break;
208            case UR_ENC_UCS2:  atom = UR_ATOM_UCS2;  break;
209        }
210
211        ur_initType(tos, UT_WORD);
212        ur_setUnbound(tos, atom);
213    }
214    else
215    {
216        ur_initType(tos, UT_NONE);
217    }
218}
219
220
221/* EOF */
Note: See TracBrowser for help on using the browser.