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

Slices and Strings comparisons #105

Merged
merged 6 commits into from
Feb 16, 2024

Conversation

Gusarich
Copy link
Member

This PR introduces the ability to compare both slices and strings via == operation.

For slice comparison, their hashes (calculated using HASHSU) can be compared directly.
As for strings, they are essentially slices and can be compared similarly. However, there's a consideration with strings due to their potential for various serializations (e.g., a string can be split in the middle and continued in a subsequent reference, still constituting a valid string). Upon reviewing the implementation of the String type in Tact, it was determined to be deterministic: all slices in a 'snake' representation are completely filled, except for the last one. Therefore, the same slice comparison method is applicable to strings as well.

@0kenx
Copy link
Contributor

0kenx commented Jan 12, 2024

Hash is not the most gas efficient way to do this. There's a native opcode for this:

int equal_slices(slice a, slice b) asm "SDEQ";

@Skydev0h
Copy link

Hash is not the most gas efficient way to do this. There's a native opcode for this:

int equal_slices(slice a, slice b) asm "SDEQ";

SDEQ is, essentially, an abbreviation for Slice Data Equal. It does not account for refs at all. Use of hash covers both long (multi-cell) and short (single-cell) strings correctly.

@anton-trunov anton-trunov self-assigned this Feb 14, 2024
@anton-trunov anton-trunov added the kind: language feature Intent to add a language feature label Feb 14, 2024
Copy link
Member

@anton-trunov anton-trunov left a comment

Choose a reason for hiding this comment

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

Thanks for contributing more features 👍.
Would be great to

  • have some runtime tests with == and != both for strings and (some other kinds of) slices;
  • add an entry to the changelog (to the Changed subsection).

Update: done

src/generator/writers/writeExpression.ts Outdated Show resolved Hide resolved
@anton-trunov anton-trunov added this to the v1.2.0 milestone Feb 15, 2024
@anton-trunov anton-trunov linked an issue Feb 15, 2024 that may be closed by this pull request
@anton-trunov anton-trunov merged commit ef7165e into tact-lang:main Feb 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: language feature Intent to add a language feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String comparison with == and !=
4 participants