mirror of
https://github.com/apple/pkl.git
synced 2026-03-31 06:03:11 +02:00
Introduces Bytes class (#1019)
This introduces a new `Bytes` standard library class, for working with binary data. * Add Bytes class to the standard library * Change CLI to eval `output.bytes` * Change code generators to map Bytes to respective underlying type * Add subscript and concat operator support * Add binary encoding for Bytes * Add PCF and Plist rendering for Bytes Co-authored-by: Kushal Pisavadia <kushi.p@gmail.com>
This commit is contained in:
@@ -138,6 +138,7 @@ endif::[]
|
||||
:uri-stdlib-Function3: {uri-stdlib-baseModule}/Function3
|
||||
:uri-stdlib-Function4: {uri-stdlib-baseModule}/Function4
|
||||
:uri-stdlib-Function5: {uri-stdlib-baseModule}/Function5
|
||||
:uri-stdlib-Bytes: {uri-stdlib-baseModule}/Bytes
|
||||
:uri-stdlib-Resource: {uri-stdlib-baseModule}/Resource
|
||||
:uri-stdlib-outputFiles: {uri-stdlib-baseModule}/ModuleOutput#files
|
||||
|
||||
|
||||
@@ -161,6 +161,24 @@ The array's length is the number of slots that are filled. For example, xref:{ur
|
||||
|
|
||||
|
|
||||
|
|
||||
|
||||
|link:{uri-stdlib-Function}[Function]
|
||||
|`0xE`
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
||||
|link:{uri-stdlib-Bytes}[Bytes]
|
||||
|`0xF`
|
||||
|link:{uri-messagepack-bin}[bin]
|
||||
|Binary contents
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|===
|
||||
|
||||
[[object-members]]
|
||||
|
||||
@@ -3684,6 +3684,41 @@ res5 = map.getOrNull("Falcon") // <5>
|
||||
<4> result: `2`
|
||||
<5> result: `null`
|
||||
|
||||
[[bytes]]
|
||||
=== Bytes
|
||||
|
||||
A value of type `Bytes` is a sequence of `UInt8` elements.
|
||||
|
||||
`Bytes` can be constructed by passing byte values into the constructor.
|
||||
|
||||
[source,pkl]
|
||||
----
|
||||
bytes1 = Bytes(0xff, 0x00, 0x3f) // <1>
|
||||
bytes2 = Bytes() // <2>
|
||||
----
|
||||
<1> Result: a `Bytes` with 3 elements
|
||||
<2> Result: an empty `Bytes`
|
||||
|
||||
`Bytes` can also be constructed from a base64-encoded string, via `base64DecodedBytes`:
|
||||
|
||||
[source,pkl]
|
||||
----
|
||||
bytes3 = "cGFycm90".base64DecodedBytes // <1>
|
||||
----
|
||||
<1> Result: `Bytes(112, 97, 114, 114, 111, 116)`
|
||||
|
||||
==== `Bytes` vs `List<UInt8>`
|
||||
|
||||
`Bytes` is similar to `List<UInt8>` in that they are both sequences of `UInt8` elements.
|
||||
However, they are semantically distinct.
|
||||
`Bytes` represent binary data, and is typically rendered differently.
|
||||
For example, they are rendered as `<data>` tags when using `PListRenderer`.
|
||||
|
||||
`Bytes` also have different performance characteristics; a value of type `Bytes` tends to be managed as a contiguous memory block.
|
||||
Thus, they are more compact and consume less memory.
|
||||
However, they are not optimized for transformations.
|
||||
For example, given two values of size `M` and `N`, concatenating two `Bytes` values allocates O(M + N) space, whereas concatenating two `List` values allocates O(1) space.
|
||||
|
||||
[[regular-expressions]]
|
||||
=== Regular Expressions
|
||||
|
||||
@@ -4671,6 +4706,10 @@ The following types are iterable:
|
||||
|entry key (`Key`)
|
||||
|entry value (`Value`)
|
||||
|
||||
|`Bytes`
|
||||
|element index (`Int`)
|
||||
|element value (`UInt8`)
|
||||
|
||||
|`Listing<Element>`
|
||||
|element index (`Int`)
|
||||
|element value (`Element`)
|
||||
@@ -4751,6 +4790,9 @@ The following table describes how different iterables turn into object members:
|
||||
|
||||
| `IntSeq`
|
||||
| Element
|
||||
|
||||
| `Bytes`
|
||||
| Element
|
||||
|===
|
||||
|
||||
These types can only be spread into enclosing objects that support that member type.
|
||||
|
||||
Reference in New Issue
Block a user