-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add .insert() and .insert_char() methods to ~str. #10826
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
Conversation
One of the design decisions made by the C++ standard is to avoid exposing inefficient convenience methods like this. I would expect these on a rope type but I'm not really sure they make sense on a string. There's also the issue of extending the usage of fairly meaningless byte indices. |
As to byte indices, this can be changed to what ever |
A program needing this should probably be using a rope though. The motivation behind that C++ design choice is to encourage using the right type, by making inefficient code inconvenient. Inserting text into a Unicode code string is not trivial to do correctly, and we're not really helping by pretending it's as easy as giving a byte or code point index. A code point doesn't correspond to the semantic characters most programmers expect them to, and we're not helping them. |
I thought I needed this for punycode decoding, but it turns out I really needed I still disagree with your argument, though. Yes, proper text handling is hard. That doesn’t mean we should not provide any and leave everyone to implement it themselves, possibly even more wrong than what we could have provided. In any case, if the answer is "use a rope instead" it would be nice to have a rope type in libextra. (Although that’s not really related to this bug.) |
|
@SimonSapin There used to be a rope type in extra (until #7629), if this can be of any help. |
A rewrite is necessary for a correct, useful rope. |
This only reinforce my point that "just use a rope" is not the answer to insert on ~str. Anyway, I finished implementing punycode, using insert on ~[char]: https://github.com/SimonSapin/rust-url/blob/master/punycode.rs |
I don't particularly have a problem with this, though the docs could indicate the performance pitfalls as well as the behavior when the position isn't on a codepoint boundary. Any other opinions? |
@SimonSapin: I don't understand why the poor state of the standard library would be an argument against ropes. |
@brson Yes. I will update the pull request to do so. (Though this applies to the rest of the standard library as well.) @thestinger I mean that #7629 is a sign that ropes are complex and not obvious to get right, and thus are not always a good answer to "I need to insert". |
The effort required to implement a type like a hash table, rope or balanced binary search tree says nothing about their ease of use or suitability for a certain purpose. Vectors obviously aren't easy to get right either because Rust's vectors are full of overflow bugs and are slow. |
+1 |
I do notice that |
@brson Do you mean the order of the parameters? I can change that. |
@SimonSapin yeah, the argument order is different for the two. |
@brson Rebased, and fixed the argument order. |
I could not run the tests because of unrelated building issue, sorry about that.
Add lints for disallowing usage of `to_xx_bytes` and `from_xx_bytes` Adds `host_endian_bytes`, `little_endian_bytes` and `big_endian_bytes` Closes rust-lang#10765 v - not sure what to put here since this adds 3 lints changelog: Add `host_endian_bytes`, `little_endian_bytes` and `big_endian_bytes` lints
I could not run the tests because of unrelated building issue, sorry about that.