Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

arterialist
Copy link

No description provided.

@sasha1618
Copy link
Contributor

sasha1618 commented Jun 12, 2023

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 utils/_data.py folder

from tonsdk.boc import begin_cell, Builder, Slice


class SnakeData:
Copy link
Contributor

@sasha1618 sasha1618 Jun 12, 2023

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)
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixme: Remove

@spookyahell
Copy link

This is the only real snake, everything else, including cpython are fake snakes
image

(I'm very sorry, I have a humor problem.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants