text stringlengths 0 828 |
|---|
The maximum length for the encoded string is 2**16-1 (65535) bytes. |
An assertion error will result if the encoded string is longer. |
Parameters |
---------- |
s: str |
String to be encoded. |
f: file |
File-like object. |
Returns |
------- |
int |
Number of bytes written to f. |
"""""" |
encode = codecs.getencoder('utf8') |
encoded_str_bytes, num_encoded_chars = encode(s) |
num_encoded_str_bytes = len(encoded_str_bytes) |
assert 0 <= num_encoded_str_bytes <= 2**16-1 |
num_encoded_bytes = num_encoded_str_bytes + 2 |
f.write(FIELD_U8.pack((num_encoded_str_bytes & 0xff00) >> 8)) |
f.write(FIELD_U8.pack(num_encoded_str_bytes & 0x00ff)) |
f.write(encoded_str_bytes) |
return num_encoded_bytes" |
635,"def decode_utf8(f): |
""""""Decode a utf-8 string encoded as described in MQTT Version |
3.1.1 section 1.5.3 line 177. This is a 16-bit unsigned length |
followed by a utf-8 encoded string. |
Parameters |
---------- |
f: file |
File-like object with read method. |
Raises |
------ |
UnderflowDecodeError |
Raised when a read failed to extract enough bytes from the |
underlying stream to decode the string. |
Utf8DecodeError |
When any code point in the utf-8 string is invalid. |
Returns |
------- |
int |
Number of bytes consumed. |
str |
A string utf-8 decoded from ``f``. |
"""""" |
decode = codecs.getdecoder('utf8') |
buf = f.read(FIELD_U16.size) |
if len(buf) < FIELD_U16.size: |
raise UnderflowDecodeError() |
(num_utf8_bytes,) = FIELD_U16.unpack_from(buf) |
num_bytes_consumed = FIELD_U16.size + num_utf8_bytes |
buf = f.read(num_utf8_bytes) |
if len(buf) < num_utf8_bytes: |
raise UnderflowDecodeError() |
try: |
s, num_chars = decode(buf, 'strict') |
except UnicodeError as e: |
raise Utf8DecodeError(e) |
return num_bytes_consumed, s" |
636,"def encode_varint(v, f): |
""""""Encode integer `v` to file `f`. |
Parameters |
---------- |
v: int |
Integer v >= 0. |
f: file |
Object containing a write method. |
Returns |
------- |
int |
Number of bytes written. |
"""""" |
assert v >= 0 |
num_bytes = 0 |
while True: |
b = v % 0x80 |
v = v // 0x80 |
if v > 0: |
b = b | 0x80 |
f.write(FIELD_U8.pack(b)) |
num_bytes += 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.