Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
encoder.py
Go to the documentation of this 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
3# BSD-style license that can be found in the LICENSE file.
4'''
5This Encoder shares a lot in common with protobufs. It uses variable length
6ints and size-encoded strings and binary values. Other than being hugely
7stripped down, the major conceptual difference is that this encoding
8is UTF8 "safe". This means that it generates a form that should be passed
9on the wire as UTF8 and then can be very efficiently decoded by JS in the
10browser which natively handles these kinds of strings. To stay efficient in
11this range, all numeric data is encoded in only 7 bits.
12'''
13
14import base64
15
16
17class Encoder:
18
19 def __init__(self):
20 self.data = []
21
22 def writeInt(self, value):
23 '''Uses a 7-bit per byte encoding to stay UTF-8 "safe".'''
24 bits = value & 0x3f
25 value >>= 6
26 while value:
27 self.data.append(chr(0x40 | bits))
28 bits = value & 0x3f
29 value >>= 6
30 self.data.append(chr(bits))
31
32 def writeBool(self, b):
33 self.data.append(('F', 'T')[b])
34
35 def writeString(self, s):
36 if not s: s = ''
37 self.writeInt(len(s))
38 self.data.append(s)
39
40 def writeBinary(self, s):
41 '''Encode binary data using base64. This is less efficient than a 7-bit
42 encoding would be; however, it can be decoded much faster on most
43 browsers due to native support for the format.'''
44 v = base64.b64encode(s)
45 self.writeInt(len(v))
46 self.data.append(v)
47
48 def writeList(self, l):
49 self.writeInt(len(l))
50 for i in l:
51 i.encode(self)
52
53 def writeRaw(self, s):
54 self.data.append(s)
55
56 def finish(self):
57 d = ''.join(self.data)
58 return _encVarInt(len(d)) + d
59
60 def getRaw(self):
61 return ''.join(self.data)
writeList(self, l)
Definition encoder.py:48
writeInt(self, value)
Definition encoder.py:22
writeString(self, s)
Definition encoder.py:35
writeBool(self, b)
Definition encoder.py:32
writeRaw(self, s)
Definition encoder.py:53
writeBinary(self, s)
Definition encoder.py:40
__init__(self)
Definition encoder.py:19
static void append(char **dst, size_t *count, const char *src, size_t n)
Definition editor.cpp:211