-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snake string utils #20
base: master
Are you sure you want to change the base?
Conversation
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 data Usage: my_text = "foobar" * 40
builder = begin_cell()
SnakeData.write(builder, bytes(text, encoding="utf8"), prefixed=False) Please, put it in the |
from tonsdk.boc import begin_cell, Builder, Slice | ||
|
||
|
||
class SnakeData: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
"""
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we delete this?
@@ -2,11 +2,13 @@ | |||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixme: Remove
No description provided.