You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: BinaryFormat.md
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,7 @@ This is useful if the integer values are often large, e.g. for random numbers.
87
87
### Strings
88
88
89
89
Swift `String` values are encoded using their `UTF-8` representations.
90
-
If a string can't be encoded this way, then encoding fails.
90
+
If a string can't be encoded this way, then encoding fails.s
91
91
92
92
## Containers
93
93
@@ -148,6 +148,33 @@ which translates to:
148
148
0x00 // Fourth element is not nil, length 0
149
149
```
150
150
151
+
### Packed sequences
152
+
153
+
Some of these basic types can be decoded from a continuous stream, either because they have a fixed length (like `Double`), or because their encoding can detect when the type ends (like variable-length encoded types).
154
+
Since these types don't require a length, basic sequences (`Array` and `Set`) of these types are encoded in a "packed" format, where no additional length indicator is added for each element.
155
+
156
+
For example, encoding a series of `Bool` values in an unkeyed container would result in the following encoded data:
157
+
158
+
```
159
+
// True, false, false
160
+
02 01 02 00 02 00
161
+
```
162
+
163
+
The `02` bytes indicate the length of each `Bool`, which is unnecessary, since a `Bool` is always exactly one byte.
164
+
165
+
When encoding a type of `[Bool]`, the encoded data is shortened to:
166
+
167
+
```
168
+
// True, false, false
169
+
01 00 00
170
+
```
171
+
172
+
This encoding is only used for the following types:
0 commit comments