-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Avoid redundant WTF-8 checks in PathBuf
#140159
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,19 +216,26 @@ impl Buf { | |
self.as_slice().into_rc() | ||
} | ||
|
||
/// Provides plumbing to core `Vec::truncate`. | ||
/// More well behaving alternative to allowing outer types | ||
/// full mutable access to the core `Vec`. | ||
#[inline] | ||
pub(crate) fn truncate(&mut self, len: usize) { | ||
/// Provides plumbing to `Vec::truncate` without giving full mutable access | ||
/// to the `Vec`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The length must be at an `OsStr` boundary, according to | ||
/// `Slice::check_public_boundary`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am having increasingly negative opinions about having an internal type named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then what would a better name be? I might submit a PR for that. But maybe not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure, honestly! it just is starting to bug me because this looked like a typo at first. |
||
#[inline] | ||
pub unsafe fn truncate_unchecked(&mut self, len: usize) { | ||
self.inner.truncate(len); | ||
} | ||
|
||
/// Provides plumbing to core `Vec::extend_from_slice`. | ||
/// More well behaving alternative to allowing outer types | ||
/// full mutable access to the core `Vec`. | ||
/// Provides plumbing to `Vec::extend_from_slice` without giving full | ||
/// mutable access to the `Vec`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This encoding has no safety requirements. | ||
#[inline] | ||
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) { | ||
pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) { | ||
self.inner.extend_from_slice(other); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you to @thaliaarchi for helping explain to/remind me that the reason this is sufficient as a safety condition is that joined surrogate pairs get distinct encodings that are not identical to "a lone surrogate followed by a lone surrogate" because that's why UTF-8 is, er, UTF-8 instead of... yeah.
To reiterate for my own benefit, because I have an annoying "I understand this from the bottom up or it doesn't click" problem, this required understanding:
Utf16::Paired
variant when it isn't actually paired, but a lot of "WTF-16" encodings actually allow lone surrogates in because everyone was still tacitly assuming handling things code-unit-by-code-unit would work fine and that's not how any multi-code-unit encoding works.boyUnicode code point". Thus, putting two surrogates "one after the other" is not always "simple" appending for WTF-8, when it can actually mean decoding the pair of code units into a Unicode code point and then encoding that into UTF-8.( There's a detail for "trailing and leading" surrogates about the actual encoding for surrogates but I am deliberately eliding it because everyone spends five hours of explanation on exactly how surrogates are encoded which doesn't matter when thinking about the design at a higher level, such that everything that actually matters oozes out of my brain before we get to the conclusion. )
( I guess that's me not being completely bottom-up but whatever. )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...this is almost completely irrelevant to this actual review obviously I'm just trying to write this down again to try to get it to stick to my brain this time, as I have tried to learn/understand this at least 3 times.