-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Unsafetyify From<Vec<char>> #35098
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
Closed
Closed
Unsafetyify From<Vec<char>> #35098
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ const TAG_FOUR_B: u8 = 0b1111_0000; | |
const MAX_ONE_B: u32 = 0x80; | ||
const MAX_TWO_B: u32 = 0x800; | ||
const MAX_THREE_B: u32 = 0x10000; | ||
const UTF8_BUF_SIZE: usize = 4; | ||
|
||
/* | ||
Lu Uppercase_Letter an uppercase letter | ||
|
@@ -644,15 +645,27 @@ impl ExactSizeIterator for EscapeDebug { } | |
#[unstable(feature = "unicode", issue = "27784")] | ||
#[derive(Debug)] | ||
pub struct EncodeUtf8 { | ||
buf: [u8; 4], | ||
buf: [u8; UTF8_BUF_SIZE], | ||
pos: usize, | ||
} | ||
|
||
impl EncodeUtf8 { | ||
/// Returns the remaining bytes of this iterator as a slice. | ||
#[unstable(feature = "unicode", issue = "27784")] | ||
#[inline(always)] | ||
pub fn as_slice(&self) -> &[u8] { | ||
&self.buf[self.pos..] | ||
// We know for sure this method cannot slice out-of-bounds because: | ||
// * 0 ≤ self.pos ≤ 3 | ||
// * self.buf.len() = 4 | ||
// | ||
// This way the slicing will always succeed, but LLVM is incapable of figuring out both | ||
// these conditions hold, resulting in suboptimal code, especially after inlining. | ||
// Ideally there would be a `slice_unchecked` method for slices, but there isn’t any, | ||
// therefore we construct the slice manually. | ||
unsafe { | ||
::slice::from_raw_parts(self.buf.as_ptr().offset(self.pos as isize), | ||
UTF8_BUF_SIZE.wrapping_sub(self.pos)) | ||
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.
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. Avoids the unnecessary overflow check. |
||
} | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
wrapping_add
?Also would it be better to increase
bytes
outside this loop?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 variable is used to offset the pointer as well, so there must be some sort of counter here anyway.
EDIT: wrapping_add because
bytes
cannot overflow, saving overflow check. In debug code, that is.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.
Right, I misread what
bytes
was used for.Why does debug mode matter? IMO
+
is the operator to use if you know it can't overflow, the wrapping ops are for when you know it can overflow and that's what you want.