|
40 | 40 | - Boolean (true or false) |
41 | 41 | - Integer (signed 64-bit integer) |
42 | 42 | - Float (64-bit floating point number) |
43 | | -- Text (UTF-8 encoded text data) |
| 43 | +- String (UTF-8 encoded string data) |
44 | 44 | - List (ordered collection of values) |
45 | 45 | - Map (keyed collection of values) |
46 | 46 | - Structure (composite set of values with a type signature) |
|
162 | 162 | +2 147 483 648 | +9 223 372 036 854 775 807 | INT_64 |
163 | 163 |
|
164 | 164 |
|
165 | | -Text |
| 165 | +String |
166 | 166 | ---- |
167 | 167 |
|
168 | | -Text data is represented as UTF-8 encoded binary data. Note that sizes used |
169 | | -for text are the byte counts of the UTF-8 encoded data, not the character count |
170 | | -of the original text. |
| 168 | +String data is represented as UTF-8 encoded binary data. Note that sizes used |
| 169 | +for string are the byte counts of the UTF-8 encoded data, not the character count |
| 170 | +of the original string. |
171 | 171 |
|
172 | 172 | Marker | Size | Maximum size |
173 | 173 | ========|=============================================|===================== |
|
176 | 176 | D1 | 16-bit big-endian unsigned integer | 65 535 bytes |
177 | 177 | D2 | 32-bit big-endian unsigned integer | 4 294 967 295 bytes |
178 | 178 |
|
179 | | -For encoded text containing fewer than 16 bytes, including empty strings, |
| 179 | +For encoded string containing fewer than 16 bytes, including empty strings, |
180 | 180 | the marker byte should contain the high-order nibble `1000` followed by a |
181 | 181 | low-order nibble containing the size. The encoded data then immediately |
182 | 182 | follows the marker. |
183 | 183 |
|
184 | | -For encoded text containing 16 bytes or more, the marker 0xD0, 0xD1 or 0xD2 |
| 184 | +For encoded string containing 16 bytes or more, the marker 0xD0, 0xD1 or 0xD2 |
185 | 185 | should be used, depending on scale. This marker is followed by the size and |
186 | 186 | the UTF-8 encoded data. Examples follow below: |
187 | 187 |
|
|
253 | 253 | the marker byte should contain the high-order nibble `1010` followed by a |
254 | 254 | low-order nibble containing the size. The items within the map are then |
255 | 255 | serialised in key-value-key-value order immediately after the marker. Keys |
256 | | -are typically text values. |
| 256 | +are typically string values. |
257 | 257 |
|
258 | 258 | For maps containing 16 pairs or more, the marker 0xD8, 0xD9 or 0xDA should be |
259 | 259 | used, depending on scale. This marker is followed by the size and map |
|
315 | 315 |
|
316 | 316 | if sys.version_info >= (3,): |
317 | 317 | INTEGER_TYPE = int |
318 | | - TEXT_TYPE = str |
| 318 | + STRING_TYPE = str |
319 | 319 | else: |
320 | 320 | INTEGER_TYPE = (int, long) |
321 | | - TEXT_TYPE = unicode |
| 321 | + STRING_TYPE = unicode |
322 | 322 |
|
323 | 323 | __all__ = ["Packer", "pack", "packb", "Unpacker", "unpack", "unpackb"] |
324 | 324 |
|
|
349 | 349 | INT_32_STRUCT = ">i" |
350 | 350 | INT_64_STRUCT = ">q" |
351 | 351 |
|
352 | | -TINY_TEXT = [bytes(bytearray([x])) for x in range(0x80, 0x90)] |
| 352 | +TINY_STRING = [bytes(bytearray([x])) for x in range(0x80, 0x90)] |
353 | 353 | TINY_LIST = [bytes(bytearray([x])) for x in range(0x90, 0xA0)] |
354 | 354 | TINY_MAP = [bytes(bytearray([x])) for x in range(0xA0, 0xB0)] |
355 | 355 | TINY_STRUCT = [bytes(bytearray([x])) for x in range(0xB0, 0xC0)] |
|
365 | 365 | BYTES_8 = b"\xCC" |
366 | 366 | BYTES_16 = b"\xCD" |
367 | 367 | BYTES_32 = b"\xCE" |
368 | | -TEXT_8 = b"\xD0" |
369 | | -TEXT_16 = b"\xD1" |
370 | | -TEXT_32 = b"\xD2" |
| 368 | +STRING_8 = b"\xD0" |
| 369 | +STRING_16 = b"\xD1" |
| 370 | +STRING_32 = b"\xD2" |
371 | 371 | LIST_8 = b"\xD4" |
372 | 372 | LIST_16 = b"\xD5" |
373 | 373 | LIST_32 = b"\xD6" |
@@ -498,10 +498,10 @@ def pack(self, value): |
498 | 498 | self.pack_bytes_header(len(value)) |
499 | 499 | self.pack_raw(value) |
500 | 500 |
|
501 | | - # Text |
502 | | - elif isinstance(value, TEXT_TYPE): |
| 501 | + # String |
| 502 | + elif isinstance(value, STRING_TYPE): |
503 | 503 | value_bytes = value.encode(ENCODING) |
504 | | - self.pack_text_header(len(value_bytes)) |
| 504 | + self.pack_string_header(len(value_bytes)) |
505 | 505 | self.pack_raw(value_bytes) |
506 | 506 |
|
507 | 507 | # List |
@@ -546,21 +546,21 @@ def pack_bytes_header(self, size): |
546 | 546 | else: |
547 | 547 | raise OverflowError("Bytes header size out of range") |
548 | 548 |
|
549 | | - def pack_text_header(self, size): |
| 549 | + def pack_string_header(self, size): |
550 | 550 | stream = self.stream |
551 | 551 | if size < PLUS_2_TO_THE_4: |
552 | | - stream.write(TINY_TEXT[size]) |
| 552 | + stream.write(TINY_STRING[size]) |
553 | 553 | elif size < PLUS_2_TO_THE_8: |
554 | | - stream.write(TEXT_8) |
| 554 | + stream.write(STRING_8) |
555 | 555 | stream.write(PACKED_UINT_8[size]) |
556 | 556 | elif size < PLUS_2_TO_THE_16: |
557 | | - stream.write(TEXT_16) |
| 557 | + stream.write(STRING_16) |
558 | 558 | stream.write(PACKED_UINT_16[size]) |
559 | 559 | elif size < PLUS_2_TO_THE_32: |
560 | | - stream.write(TEXT_32) |
| 560 | + stream.write(STRING_32) |
561 | 561 | stream.write(struct_pack(UINT_32_STRUCT, size)) |
562 | 562 | else: |
563 | | - raise OverflowError("Text header size out of range") |
| 563 | + raise OverflowError("String header size out of range") |
564 | 564 |
|
565 | 565 | def pack_list_header(self, size): |
566 | 566 | stream = self.stream |
@@ -690,16 +690,16 @@ def unpack(self): |
690 | 690 | byte_size = struct_unpack(UINT_32_STRUCT, stream_read(4))[0] |
691 | 691 | value = stream_read(byte_size) |
692 | 692 |
|
693 | | - # Text |
| 693 | + # String |
694 | 694 | elif marker_high == 0x80: |
695 | 695 | value = stream_read(marker & 0x0F).decode(ENCODING) |
696 | | - elif marker_byte == TEXT_8: |
| 696 | + elif marker_byte == STRING_8: |
697 | 697 | byte_size = UNPACKED_UINT_8[stream_read(1)] |
698 | 698 | value = stream_read(byte_size).decode(ENCODING) |
699 | | - elif marker_byte == TEXT_16: |
| 699 | + elif marker_byte == STRING_16: |
700 | 700 | byte_size = UNPACKED_UINT_16[stream_read(2)] |
701 | 701 | value = stream_read(byte_size).decode(ENCODING) |
702 | | - elif marker_byte == TEXT_32: |
| 702 | + elif marker_byte == STRING_32: |
703 | 703 | byte_size = struct_unpack(UINT_32_STRUCT, stream_read(4))[0] |
704 | 704 | value = stream_read(byte_size).decode(ENCODING) |
705 | 705 |
|
|
0 commit comments