OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of swarmlib; | 5 part of swarmlib; |
6 | 6 |
7 // TODO(jimhug): Fill out methods, add encoder, move to shared lib. | 7 // TODO(jimhug): Fill out methods, add encoder, move to shared lib. |
8 class Decoder { | 8 class Decoder { |
9 int index; | 9 int index; |
10 String data; | 10 String data; |
11 | 11 |
12 Decoder(this.data) { | 12 Decoder(this.data) { |
13 this.index = 0; | 13 this.index = 0; |
14 } | 14 } |
15 | 15 |
16 // Reads numbers in variable-length 7-bit encoding. This matches the | 16 // Reads numbers in variable-length 7-bit encoding. This matches the |
17 // varint encoding used by protobufs except that it only uses 7 | 17 // varint encoding used by protobufs except that it only uses 7 |
18 // bits per byte so it can be efficiently passed as UTF8. | 18 // bits per byte so it can be efficiently passed as UTF8. |
19 // For more info, see appengine/encoder.py. | 19 // For more info, see appengine/encoder.py. |
20 int readInt() { | 20 int readInt() { |
21 var r = 0; | 21 var r = 0; |
22 for (var i=0; ; i++) { | 22 for (var i=0; ; i++) { |
23 var v = data.charCodeAt(index++); | 23 var v = data.charCodeAt(index++); |
24 r |= (v & 0x3F) << (6 * i); | 24 r |= (v & 0x3F) << (6 * i); |
25 if ((v & 0x40) == 0) break; | 25 if ((v & 0x40) == 0) break; |
26 } | 26 } |
27 return r.toInt(); | 27 return r.truncate(); |
28 } | 28 } |
29 | 29 |
30 bool readBool() { | 30 bool readBool() { |
31 final ch = data[index++]; | 31 final ch = data[index++]; |
32 assert (ch == 'T' || ch == 'F'); | 32 assert (ch == 'T' || ch == 'F'); |
33 return ch == 'T'; | 33 return ch == 'T'; |
34 } | 34 } |
35 | 35 |
36 String readString() { | 36 String readString() { |
37 int len = readInt(); | 37 int len = readInt(); |
38 String s = data.substring(index, index+len); | 38 String s = data.substring(index, index+len); |
39 index += len; | 39 index += len; |
40 return s; | 40 return s; |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
OLD | NEW |