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

Ensure shuffled bits get cleared out in "modifying" operations #33

Merged
merged 3 commits into from
Jun 27, 2022

Conversation

quinnj
Copy link
Member

@quinnj quinnj commented Jun 27, 2022

Fixes #32. The core issue here is we're taking a few shortcuts in some operations
like chop, chomp, first, last where we just shuffle the bits around and OR the
new length. The problem is there can be "extra bits" in the inline string that
can then affect operations like ==. So we need to ensure in these optimized
"modifying" operations, these extra bits get zeroed out to ensure a consistent
bit representation.

Fixes #32. The core issue here is we're taking a few shortcuts in some operations
like chop, chomp, first, last where we just shuffle the bits around and OR the
new length. The problem is there can be "extra bits" in the inline string that
can then affect operations like `==`. So we need to ensure in these optimized
"modifying" operations, these extra bits get zeroed out to ensure a consistent
bit representation.
Copy link
Collaborator

@nickrobinson251 nickrobinson251 left a comment

Choose a reason for hiding this comment

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

Let's add tests?

some things which were failing before:

abc = InlineString3("abc")
@test first(abc, 2) == InlineString3("ab")
@test last(abc, 2) == InlineString3("bc")
@test chop(abc; head=0, tail=1) == InlineString3("ab")
@test chomp(InlineString3("ab\n")) == InlineString3("ab")

newlen = nextind(s, min(lastindex(s), nextind(s, 0, n))) - 1
i = sizeof(T) - newlen
# clear out any bits we're not keeping
s = Base.shl_int(Base.lshr_int(s, 8 * i), 8 * i)
Copy link
Collaborator

@nickrobinson251 nickrobinson251 Jun 27, 2022

Choose a reason for hiding this comment

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

is it worth putting this in a little helper function for clarity, rather than repeating it with comment?

@inline clear_last(s, n) = Base.shl_int(Base.lshr_int(s, 8 * n), 8 * n)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good call.

@quinnj quinnj merged commit f048f24 into main Jun 27, 2022
@quinnj quinnj deleted the jq/extrabits branch June 27, 2022 21:43
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.

Possible bug in unique for partial InlineString
2 participants