Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
third_party
dart-lang
sdk
pkg
compiler
test
codesize
swarm
appengine
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
'''
5
This Encoder shares a lot in common with protobufs. It uses variable length
6
ints and size-encoded strings and binary values. Other than being hugely
7
stripped down, the major conceptual difference is that this encoding
8
is UTF8 "safe". This means that it generates a form that should be passed
9
on the wire as UTF8 and then can be very efficiently decoded by JS in the
10
browser which natively handles these kinds of strings. To stay efficient in
11
this range, all numeric data is encoded in only 7 bits.
12
'''
13
14
import
base64
15
16
17
class
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
)
encoder.Encoder
Definition
encoder.py:17
encoder.Encoder.getRaw
getRaw(self)
Definition
encoder.py:60
encoder.Encoder.writeList
writeList(self, l)
Definition
encoder.py:48
encoder.Encoder.writeInt
writeInt(self, value)
Definition
encoder.py:22
encoder.Encoder.writeString
writeString(self, s)
Definition
encoder.py:35
encoder.Encoder.data
data
Definition
encoder.py:20
encoder.Encoder.writeBool
writeBool(self, b)
Definition
encoder.py:32
encoder.Encoder.writeRaw
writeRaw(self, s)
Definition
encoder.py:53
encoder.Encoder.writeBinary
writeBinary(self, s)
Definition
encoder.py:40
encoder.Encoder.__init__
__init__(self)
Definition
encoder.py:19
encoder.Encoder.finish
finish(self)
Definition
encoder.py:56
append
static void append(char **dst, size_t *count, const char *src, size_t n)
Definition
editor.cpp:211
Generated on Fri Apr 26 2024 06:14:40 for Flutter Engine by
1.9.8