-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add padLeftCodec and padRightCodec helpers to…
… @solana/codecs-core (#2314) This PR adds new `padLeftCodec` and `padRightCodec` helper functions. See copy/pasted documentation below: --- ## Padding codecs The `padLeftCodec` and `padRightCodec` helpers can be used to add padding to the left or right of a given codec. They accept an `offset` number that tells us how big the padding should be. ```ts const getLeftPaddedCodec = () => padLeftCodec(getU16Codec(), 4); getLeftPaddedCodec().encode(0xffff); // 0x00000000ffff // | └-- Our encoded u16 number. // └-- Our 4-byte padding. const getRightPaddedCodec = () => padRightCodec(getU16Codec(), 4); getRightPaddedCodec().encode(0xffff); // 0xffff00000000 // | └-- Our 4-byte padding. // └-- Our encoded u16 number. ``` Note that both the `padLeftCodec` and `padRightCodec` functions are simple wrappers around the `offsetCodec` and `resizeCodec` functions. For more complex padding strategies, you may want to use the `offsetCodec` and `resizeCodec` functions directly instead. As usual, encoder-only and decoder-only helpers are available for these padding functions. Namely, `padLeftEncoder`, `padRightEncoder`, `padLeftDecoder` and `padRightDecoder`. ```ts const getMyPaddedEncoder = () => padLeftEncoder(getU16Encoder()); const getMyPaddedDecoder = () => padLeftDecoder(getU16Decoder()); const getMyPaddedCodec = () => combineCodec(getMyPaddedEncoder(), getMyPaddedDecoder()); ```
- Loading branch information
1 parent
09d8cc8
commit f9509c7
Showing
7 changed files
with
283 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.