Open
Conversation
Contributor
|
Let's make a more flexible class: # https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md#data-serialization
class SnakeData:
prefix = 0x00
prefix_len = 8
@classmethod
def write(cls, builder: 'Builder', data: Union[bytes, bytearray], prefixed=False):
if prefixed:
builder.store_bits(cls.prefix)
# todo: implement data serialization logic to a given builder (now data is a bytes sequence)
# implementation example
builders = []
while len(data) > 0:
max_bytes = builder.builder_rembits >> 3
bits, data = data[:max_bytes], data[max_bytes:]
builder.store_bytes(bits)
builders.append(builder)
builder = begin_cell()
if len(builders) > 1:
last_builder = builders[-1]
for builder in reversed(builders[:-1]):
builder.store_ref(last_builder.end_cell())
last_builder = builder
return builders[0]
@classmethod
def read(cls, cs, prefixed=False):
data = bytearray()
if prefixed:
assert cs.load_bits(cls.prefix_len) == cls.prefix
while True:
data.extend(cs.load_bytes(cs.slice_bits >> 3))
if cs.slice_refs > 0:
cs = cs.load_ref().begin_parse()
continue
break
return dataUsage: my_text = "foobar" * 40
builder = begin_cell()
SnakeData.write(builder, bytes(text, encoding="utf8"), prefixed=False)Please, put it in the |
sasha1618
reviewed
Jun 12, 2023
| from tonsdk.boc import begin_cell, Builder, Slice | ||
|
|
||
|
|
||
| class SnakeData: |
Contributor
There was a problem hiding this comment.
We may write
class SnakeData:
"""
Implementation of the TON data standard: https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md#data-serialization
"""
sasha1618
reviewed
Jun 12, 2023
| if prefixed: | ||
| builder.store_uint(cls.prefix, cls.prefix_len) | ||
|
|
||
| # todo: implement data serialization logic to a given builder (now data is a bytes sequence) |
sasha1618
reviewed
Jun 12, 2023
| from ._builder import Builder, begin_cell | ||
| from ._dict_builder import DictBuilder, begin_dict | ||
| from ._slice import Slice | ||
| from ._string_utils import string_to_cell, cell_to_string, read_string_tail, write_string_tail |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

No description provided.