Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 15.5k
Make slice->str conversion and related functions const#90607
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -72,9 +72,10 @@ impl Utf8Error { | ||
| /// assert_eq!(1, error.valid_up_to()); | ||
| /// ``` | ||
| #[stable(feature = "utf8_error", since = "1.5.0")] | ||
| #[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")] | ||
| #[must_use] | ||
| #[inline] | ||
| pub fn valid_up_to(&self) -> usize { | ||
| pub const fn valid_up_to(&self) -> usize { | ||
| self.valid_up_to | ||
| } | ||
| @@ -94,10 +95,15 @@ impl Utf8Error { | ||
| /// | ||
| /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html | ||
| #[stable(feature = "utf8_error_error_len", since = "1.20.0")] | ||
| #[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")] | ||
| #[must_use] | ||
| #[inline] | ||
| pub fn error_len(&self) -> Option<usize> { | ||
| self.error_len.map(|len| len as usize) | ||
| pub const fn error_len(&self) -> Option<usize> { | ||
| // This should become `map` again, once it's `const` | ||
| match self.error_len { | ||
WaffleLapkin marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| Some(len) => Some(len as usize), | ||
| None => None, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -8,25 +8,25 @@ use super::Utf8Error; | ||
| /// The first byte is special, only want bottom 5 bits for width 2, 4 bits | ||
| /// for width 3, and 3 bits for width 4. | ||
| #[inline] | ||
| fn utf8_first_byte(byte: u8, width: u32) -> u32 { | ||
| const fn utf8_first_byte(byte: u8, width: u32) -> u32 { | ||
| (byte & (0x7F >> width)) as u32 | ||
| } | ||
| /// Returns the value of `ch` updated with continuation byte `byte`. | ||
| #[inline] | ||
| fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { | ||
| const fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { | ||
| (ch << 6) | (byte & CONT_MASK) as u32 | ||
| } | ||
| /// Checks whether the byte is a UTF-8 continuation byte (i.e., starts with the | ||
| /// bits `10`). | ||
| #[inline] | ||
| pub(super) fn utf8_is_cont_byte(byte: u8) -> bool { | ||
| pub(super) const fn utf8_is_cont_byte(byte: u8) -> bool { | ||
| (byte as i8) < -64 | ||
| } | ||
| #[inline] | ||
| fn unwrap_or_0(opt: Option<&u8>) -> u8 { | ||
| const fn unwrap_or_0(opt: Option<&u8>) -> u8 { | ||
| match opt { | ||
| Some(&byte) => byte, | ||
| None => 0, | ||
| @@ -105,14 +105,15 @@ const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize; | ||
| /// Returns `true` if any byte in the word `x` is nonascii (>= 128). | ||
| #[inline] | ||
| fn contains_nonascii(x: usize) -> bool { | ||
| const fn contains_nonascii(x: usize) -> bool { | ||
| (x & NONASCII_MASK) != 0 | ||
| } | ||
| /// Walks through `v` checking that it's a valid UTF-8 sequence, | ||
| /// returning `Ok(())` in that case, or, if it is invalid, `Err(err)`. | ||
| #[inline(always)] | ||
| pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { | ||
| #[rustc_const_unstable(feature = "str_internals", issue = "none")] | ||
| pub(super) const fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { | ||
| let mut index = 0; | ||
| let len = v.len(); | ||
| @@ -142,7 +143,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { | ||
| let first = v[index]; | ||
| if first >= 128 { | ||
| let w = UTF8_CHAR_WIDTH[first as usize]; | ||
| let w = utf8_char_width(first); | ||
| // 2-byte encoding is for codepoints \u{0080} to \u{07ff} | ||
| // first C2 80 last DF BF | ||
| // 3-byte encoding is for codepoints \u{0800} to \u{ffff} | ||
| @@ -230,7 +231,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> { | ||
| } | ||
| // https://tools.ietf.org/html/rfc3629 | ||
WaffleLapkin marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| static UTF8_CHAR_WIDTH: [u8; 256] = [ | ||
| const UTF8_CHAR_WIDTH: &[u8; 256] = &[ | ||
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| 1, // 0x1F | ||
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
| @@ -253,7 +254,7 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [ | ||
| #[unstable(feature = "str_internals", issue = "none")] | ||
| #[must_use] | ||
| #[inline] | ||
| pub fn utf8_char_width(b: u8) -> usize { | ||
| pub const fn utf8_char_width(b: u8) -> usize { | ||
| UTF8_CHAR_WIDTH[b as usize] as usize | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.