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

Add TinyAsciiStr::concat #5772

Merged
merged 1 commit into from
Nov 5, 2024
Merged

Conversation

sffc
Copy link
Member

@sffc sffc commented Nov 5, 2024

@sffc sffc requested a review from zbraniecki as a code owner November 5, 2024 02:07
@sffc sffc requested a review from Manishearth November 5, 2024 02:08
@sffc
Copy link
Member Author

sffc commented Nov 5, 2024

I considered a few other signatures. I picked this one because:

  1. If the input were a &str, we'd need to check each character again. If you want to append a &str, you can first convert it to a TinyAsciiStr of the same size as the output.
  2. concat is more general than some of the alternatives. For example, to push a single character, you can make a TinyAsciiStr<1> and then call concat.
  3. To modify the receiver, you can save the result back, as in *x = x.concat(y). Note that &mut self is not const.
  4. I saw no good reason to enforce that Q == M + N because resize already is documented to do the truncation thing, and it saves the client from having to call resize themselves.

pub const fn concat<const M: usize, const Q: usize>(
self,
other: TinyAsciiStr<M>,
) -> TinyAsciiStr<Q> {
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think you should debug assert that Q = N+M.

Copy link
Member Author

Choose a reason for hiding this comment

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

Did you see my comment above about why I didn't add the assertion? It's fine if you disagree and want me to add the assertion, but I wanted to verify that you read my reasoning and are making this comment despite it

Copy link
Member Author

@sffc sffc Nov 5, 2024

Choose a reason for hiding this comment

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

Please also note that the only use case for this function (merging two subtags) will involve .concat().resize() if I force Q to be N + M. Seems better to do it in a single function call. There's not a good reason not to do so.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think I understand your logic. My interpretation of your code is that I can now do:

let t: TinyAsciiStr<12> = tinystr!(4, "abcd").concat(tinystr!(4, "efgh"));

@sffc sffc requested a review from zbraniecki November 5, 2024 04:30
@sffc
Copy link
Member Author

sffc commented Nov 5, 2024

@zbraniecki You can submit this (to unblock your PR) if you agree with my logic about not adding the assertion.

@zbraniecki
Copy link
Member

I'm fine merging it as is. No blockers.

@Manishearth Manishearth merged commit 8d54ba3 into unicode-org:main Nov 5, 2024
28 checks passed
@sffc sffc deleted the tinystr-concat branch November 5, 2024 18:21
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.

4 participants