~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/haxe/Unserializer.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2009-03-18 23:09:50 UTC
  • mfrom: (3.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090318230950-pgfuxg2ucolps74t
Tags: 1:2.2-2
* Use ocamlfind to locate and use the libraries xml-light and extlib
  which already exist in Debian as separate packages.
  (Closes: #519630)
* Fixed compile error with camlp4 3.11, thanks to Stéphane Glondu.
  (Closes: #519627)
* Use quilt instead of dpatch for patches, and describe how to use
  quilt in Debian.source (thanks to Russ Allbery).
* Added a Vcs-Hg control filed to indicate the location of the public
  repository.
* Bumped Standards-Version to 3.8.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 * DAMAGE.
24
24
 */
25
25
package haxe;
26
 
import Type.Enum;
27
26
 
28
27
typedef TypeResolver = {
29
28
        function resolveClass( name : String ) : Class<Dynamic>;
34
33
 
35
34
        public static var DEFAULT_RESOLVER : TypeResolver = Type;
36
35
 
 
36
        static var BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:";
 
37
 
 
38
        #if !neko
 
39
        static var CODES = null;
 
40
 
 
41
        static function initCodes() {
 
42
                var codes =
 
43
                        #if flash9
 
44
                                new flash.utils.ByteArray();
 
45
                        #else
 
46
                                new Array();
 
47
                        #end
 
48
                for( i in 0...BASE64.length )
 
49
                        codes[untyped BASE64.cca(i)] = i;
 
50
                return codes;
 
51
        }
 
52
        #end
 
53
 
37
54
        var buf : String;
38
55
        var pos : Int;
39
56
        var length : Int;
66
83
                        resolver = r;
67
84
        }
68
85
 
 
86
        inline function get(p) : Int {
 
87
                #if (flash || js)
 
88
                return untyped buf.cca(p);
 
89
                #elseif neko
 
90
                return untyped __dollar__sget(buf.__s,p);
 
91
                #else
 
92
                return buf.charCodeAt(p);
 
93
                #end
 
94
        }
 
95
 
69
96
        function readDigits() {
70
97
                var k = 0;
71
98
                var s = false;
72
99
                var fpos = pos;
73
100
                while( true ) {
74
 
                        var c = buf.charCodeAt(pos);
 
101
                        var c = get(pos);
 
102
                        #if flash9
 
103
                        // if flash9, it returns 0 so we will break later
 
104
                        #elseif (flash || js)
 
105
                        if( Math.isNaN(c) )
 
106
                                break;
 
107
                        #else
75
108
                        if( c == null )
76
109
                                break;
 
110
                        #end
77
111
                        if( c == 45 ) { // negative sign
78
112
                                if( pos != fpos )
79
113
                                        break;
96
130
                while( true ) {
97
131
                        if( pos >= length )
98
132
                                throw "Invalid object";
99
 
                        if( buf.charCodeAt(pos) == 103 ) /*g*/
 
133
                        if( get(pos) == 103 ) /*g*/
100
134
                                break;
101
135
                        var k = unserialize();
102
136
                        if( !Std.is(k,String) )
111
145
                var constr = Reflect.field(edecl,tag);
112
146
                if( constr == null )
113
147
                        throw "Unknown enum tag "+Type.getEnumName(edecl)+"."+tag;
114
 
                if( buf.charCodeAt(pos++) != 58 ) // ':'
 
148
                if( get(pos++) != 58 ) // ':'
115
149
                        throw "Invalid enum format";
116
150
                var nargs = readDigits();
117
151
                if( nargs == 0 ) {
129
163
        }
130
164
 
131
165
        public function unserialize() : Dynamic {
132
 
                switch( buf.charCodeAt(pos++) ) {
 
166
                switch( get(pos++) ) {
133
167
                case 110: // n
134
168
                        return null;
135
169
                case 116: // t
143
177
                case 100: // d
144
178
                        var p1 = pos;
145
179
                        while( true ) {
146
 
                                var c = buf.charCodeAt(pos);
 
180
                                var c = get(pos);
147
181
                                // + - . , 0-9
148
182
                                if( (c >= 43 && c < 58) || c == 101 /*e*/ || c == 69 /*E*/ )
149
183
                                        pos++;
167
201
                case 112: // p
168
202
                        return Math.POSITIVE_INFINITY;
169
203
                case 97: // a
 
204
                        var buf = buf;
170
205
                        var a = new Array<Dynamic>();
171
206
                        cache.push(a);
172
207
                        while( true ) {
173
 
                                var c = buf.charCodeAt(pos);
 
208
                                var c = get(pos);
174
209
                                if( c == 104 ) { /*h*/
175
210
                                        pos++;
176
211
                                        break;
184
219
                        }
185
220
                        return a;
186
221
                case 111: // o
187
 
                        var o = Reflect.empty();
 
222
                        var o = {};
188
223
                        cache.push(o);
189
224
                        unserializeObject(o);
190
225
                        return o;
228
263
                        return unserializeEnum(edecl,tag);
229
264
                case 108: // l
230
265
                        var l = new List();
231
 
                        while( buf.charCodeAt(pos) != 104 /*h*/ )
 
266
                        var buf = buf;
 
267
                        while( get(pos) != 104 /*h*/ )
232
268
                                l.add(unserialize());
233
269
                        pos++;
234
270
                        return l;
235
271
                case 98: // b
236
272
                        var h = new Hash();
237
 
                        while( buf.charCodeAt(pos) != 104 /*h*/ ) {
 
273
                        var buf = buf;
 
274
                        while( get(pos) != 104 /*h*/ ) {
238
275
                                var s = unserialize();
239
276
                                h.set(s,unserialize());
240
277
                        }
242
279
                        return h;
243
280
                case 113: // q
244
281
                        var h = new IntHash();
245
 
                        var c = buf.charCodeAt(pos++);
 
282
                        var buf = buf;
 
283
                        var c = get(pos++);
246
284
                        while( c == 58 ) { /*:*/
247
285
                                var i = readDigits();
248
286
                                h.set(i,unserialize());
249
 
                                c = buf.charCodeAt(pos++);
 
287
                                c = get(pos++);
250
288
                        }
251
289
                        if( c != 104 )
252
290
                                throw "Invalid IntHash format";
255
293
                        var d = Date.fromString(buf.substr(pos,19));
256
294
                        pos += 19;
257
295
                        return d;
258
 
                // DEPRECATED
259
296
                case 115: // s
260
297
                        var len = readDigits();
 
298
                        var buf = buf;
261
299
                        if( buf.charAt(pos++) != ":" || length - pos < len )
262
 
                                throw "Invalid string length";
 
300
                                throw "Invalid bytes length";
263
301
                        #if neko
264
 
                        var s = neko.Utf8.sub(buf,pos-upos,len);
265
 
                        pos += s.length;
266
 
                        upos += s.length - len;
267
 
                        #else true
268
 
                        var s = buf.substr(pos,len);
269
 
                        pos += len;
 
302
                        var bytes = haxe.io.Bytes.ofData( base_decode(untyped buf.substr(pos,len).__s,untyped BASE64.__s) );
 
303
                        #else
 
304
                        var codes = CODES;
 
305
                        if( codes == null ) {
 
306
                                codes = initCodes();
 
307
                                CODES = codes;
 
308
                        }
 
309
                        var b = new haxe.io.BytesBuffer();
 
310
                        var i = pos;
 
311
                        var rest = len & 3;
 
312
                        var max = i + (len - rest);
 
313
                        while( i < max ) {
 
314
                                var c1 = codes[untyped buf.cca(i++)];
 
315
                                var c2 = codes[untyped buf.cca(i++)];
 
316
                                b.addByte((c1 << 2) | (c2 >> 4));
 
317
                                var c3 = codes[untyped buf.cca(i++)];
 
318
                                b.addByte(((c2 << 4) | (c3 >> 2)) #if !flash9 & 0xFF #end );
 
319
                                var c4 = codes[untyped buf.cca(i++)];
 
320
                                b.addByte(((c3 << 6) | c4) #if !flash9 & 0xFF #end );
 
321
                        }
 
322
                        if( rest >= 2 ) {
 
323
                                var c1 = codes[untyped buf.cca(i++)];
 
324
                                var c2 = codes[untyped buf.cca(i++)];
 
325
                                b.addByte((c1 << 2) | (c2 >> 4));
 
326
                                if( rest == 3 ) {
 
327
                                        var c3 = codes[untyped buf.cca(i++)];
 
328
                                        b.addByte(((c2 << 4) | (c3 >> 2)) #if !flash9 & 0xFF #end );
 
329
                                }
 
330
                        }
 
331
                        var bytes = b.getBytes();
270
332
                        #end
271
 
                        scache.push(s);
272
 
                        return s;
 
333
                        pos += len;
 
334
                        cache.push(bytes);
 
335
                        return bytes;
273
336
                default:
274
337
                }
275
338
                pos--;
283
346
                return new Unserializer(v).unserialize();
284
347
        }
285
348
 
 
349
        #if neko
 
350
        static var base_decode = neko.Lib.load("std","base_decode",2);
 
351
        #end
 
352
 
286
353
}