about summary refs log tree commit diff
path: root/library/core/src/str
AgeCommit message (Collapse)AuthorLines
2025-09-26Update CURRENT_RUSTC_VERSION post-bumpMark Rousskov-4/+4
2025-09-21Change the cfg to a dashBen Kimock-2/+2
2025-09-21Add panic=immediate-abortBen Kimock-2/+2
2025-09-12Constify Eq, Ord, PartialOrdEvgenii Zheltonozhskii-1/+2
2025-09-10Rollup merge of #144765 - Qelxiros:range-inclusive-last, r=jhprattMatthias Krüger-4/+4
inclusive `Range`s: change `end` to `last` Tracking issue: rust-lang/rust#125687 ACP: rust-lang/libs-team#511
2025-09-08change end to lastJeremy Smart-4/+4
2025-09-06clean up some old const trait impl syntaxNathaniel McCallum-2/+1
2025-09-03Rollup merge of #145279 - clarfonthey:const-convert-initial, r=tgross35Stuart Cook-2/+3
Constify conversion traits (part 1) This is the first part of rust-lang/rust#144289 being split into smaller pieces. It adds/moves constness of several traits under the `const_convert` feature: * `From` * `Into` * `TryFrom` * `TryInto` * `FromStr` * `AsRef` * `AsMut` * `Borrow` * `BorrowMut` * `Deref` * `DerefMut` There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over `AsRef`: * `ByteStr::new` (unstable under `bstr` feature) * `OsStr::new` * `Path::new` Those which directly use `Into`: * `Result::into_ok` * `Result::into_err` And those which use `Deref` and `DerefMut`: * `Pin::as_ref` * `Pin::as_mut` * `Pin::as_deref_mut` * `Option::as_deref` * `Option::as_deref_mut` * `Result::as_deref` * `Result::as_deref_mut` (note: the `Option` and `Result` methods were suggested by ``@npmccallum`` initially as rust-lang/rust#146101) The parts which are missing from this PR are: * Anything that involves heap-allocated types * Making any method const than the ones listed above * Anything that could rely on the above, *or* could rely on system-specific code for `OsStr` or `Path` (note: this mostly makes these methods useless since `str` doesn't implement `AsRef<OsStr>` yet, but it's better to track the method for now and add impls later, IMHO) r? ``@tgross35`` (who mostly already reviewed this)
2025-09-01Constify conversion traitsltdk-2/+3
2025-08-29Rollup merge of #145756 - okaneco:stabilize_char_boundary, r=scottmcmTrevor Gross-4/+4
str: Stabilize `round_char_boundary` feature Closes https://github.com/rust-lang/rust/issues/93743 FCP completed https://github.com/rust-lang/rust/issues/93743#issuecomment-3168382171
2025-08-27`const`ify (the unstable) `str::as_str`Pavel Grigorenko-1/+1
2025-08-26remove deprecated Error::description in implsMarijn Schouten-12/+2
2025-08-22Stabilize `round_char_boundary` featureokaneco-4/+4
2025-08-06tidyBoxy-4/+4
2025-07-29Auto merge of #144393 - heiher:str-contains-lsx, r=tgross35bors-3/+16
LoongArch64 LSX fast-path for `str.contains(&str)` Benchmark results with LLVM 21 on LA664: ``` OLD: test bench_is_contained_in ... bench: 43.63 ns/iter (+/- 0.04) NEW: test bench_is_contained_in ... bench: 12.81 ns/iter (+/- 0.01) ```
2025-07-29LoongArch64 LSX fast-path for `str.contains(&str)`WANG Rui-3/+16
Benchmark results with LLVM 21 on LA664: ``` OLD: test bench_is_contained_in ... bench: 43.63 ns/iter (+/- 0.04) NEW: test bench_is_contained_in ... bench: 12.81 ns/iter (+/- 0.01) ```
2025-07-29Auto merge of #143289 - scottmcm:remove-array-chunks, r=jhprattbors-1/+1
Remove `[T]::array_chunks(_mut)` Since libs-api is proposing as much in https://github.com/rust-lang/rust/issues/74985#issuecomment-3024465102 Closes rust-lang/rust#74985 Closes rust-lang/rust#76354 try-job: dist-various-1 try-job: dist-various-2
2025-07-27Remove `[T]::array_chunks(_mut)`Scott McMurray-1/+1
2025-07-25str: Mark unstable `round_char_boundary` feature functions as constokaneco-14/+26
Mark `floor_char_boundary`, `ceil_char_boundary` const Simplify the implementations, reducing the number of arithmetic operations
2025-07-22Rollup merge of #143768 - Randl:const-try, r=oli-obk许杰友 Jieyou Xu (Joe)-0/+2
Constify Try, From, TryFrom and relevant traits
2025-07-21Constify Try, From, TryFromEvgenii Zheltonozhskii-0/+2
2025-07-20Rollup merge of #143423 - hkBst:clippy-fix-1, r=workingjubileeGuillaume Gomez-1/+0
address clippy formatting nits - int_log10.rs: change top level doc comments to outer - collect.rs: remove empty line after doc comment - clippy fix: markdown indentation for indented items after line break: a markdown list item continued over multiples lines, but those following lines which are part of the same item are not indented - clippy fix: bound in one place: when there is a bound in angle brackets and another bound on the same variable in a where clause
2025-07-17Make slices `[const] PartialEq`Oli Scherer-1/+2
2025-07-16Rollup merge of #143921 - oli-obk:const-index, r=fee1-deadSamuel Tardieu-15/+28
Constify `Index` traits tracking issue: rust-lang/rust#143775 the `SliceIndex` trait cannot be implemented by users as it is sealed. While it would be useful for the `get` method on slices, it seems weird to have a feature gate for that that isn't also gating index syntax at the same time, so I put them under the same feature gate. r? ```````@fee1-dead```````
2025-07-15Rollup merge of #143907 - Kijewski:pr-inline-split_at_unchecked, ↵Samuel Tardieu-0/+1
r=Mark-Simulacrum core: make `str::split_at_unchecked()` inline This PR adds `#[inline]` to the method `str::split_at_unchecked()`. This is done for two reasons: 1. The method is tiny, e.g. on AMD-64 (<https://godbolt.org/z/ba68fdfxn>): ```asm movq %rdi, %rax subq %rcx, %rdx movq %rsi, (%rdi) addq %rcx, %rsi movq %rcx, 8(%rdi) movq %rsi, 16(%rdi) movq %rdx, 24(%rdi) retq ``` 2. More importantly, inlining the method enables further automatic optimizations. E.g. if you split at index 3, then in the compiler (rustc, llvm or both) knows that this code cannot fail, and the panicking path is omitted in the generated code: ```rust pub fn punctuation(i: &str) -> Result<(), ()> { const THREE_CHARS: &[[u8; 3]] = &[*b"<<=", *b">>=", *b"...", *b"..="]; if let Some((head, _)) = i.split_at_checked(3) && THREE_CHARS.contains(&head.as_bytes().try_into().unwrap()) { Ok(()) } else { Err(()) } } ``` <details> <summary>Without PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=0234de8158f467eebd73286f20d6e27a> ```asm playground::punctuation: subq $40, %rsp movq %rsi, %rdx movq %rdi, %rsi movb $1, %al cmpq $3, %rdx ja .LBB2_2 je .LBB2_3 .LBB2_11: addq $40, %rsp retq .LBB2_2: cmpb $-64, 3(%rsi) jl .LBB2_11 .LBB2_3: leaq 8(%rsp), %rdi movl $3, %ecx callq *core::str::<impl str>::split_at_unchecked@GOTPCREL(%rip) movq 8(%rsp), %rcx movb $1, %al testq %rcx, %rcx je .LBB2_11 cmpq $3, 16(%rsp) jne .LBB2_12 movzwl (%rcx), %edx movzbl 2(%rcx), %ecx shll $16, %ecx orl %edx, %ecx cmpl $4013115, %ecx jg .LBB2_8 cmpl $3026478, %ecx je .LBB2_10 cmpl $4009518, %ecx je .LBB2_10 jmp .LBB2_11 .LBB2_8: cmpl $4013630, %ecx je .LBB2_10 cmpl $4013116, %ecx jne .LBB2_11 .LBB2_10: xorl %eax, %eax addq $40, %rsp retq .LBB2_12: leaq .Lanon.d98a7fbb86d10a97c24516e267466134.2(%rip), %rdi leaq .Lanon.d98a7fbb86d10a97c24516e267466134.1(%rip), %rcx leaq .Lanon.d98a7fbb86d10a97c24516e267466134.6(%rip), %r8 leaq 7(%rsp), %rdx movl $43, %esi callq *core::result::unwrap_failed@GOTPCREL(%rip) ``` </details> <details> <summary>With PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=5d4058c79ce0f6cb1a434190427d2055> ```asm playground::punctuation: movb $1, %al cmpq $3, %rsi ja .LBB0_2 je .LBB0_3 .LBB0_9: retq .LBB0_2: cmpb $-64, 3(%rdi) jl .LBB0_9 .LBB0_3: movzwl (%rdi), %eax movzbl 2(%rdi), %ecx shll $16, %ecx orl %eax, %ecx movb $1, %al cmpl $4013115, %ecx jg .LBB0_6 cmpl $3026478, %ecx je .LBB0_8 cmpl $4009518, %ecx je .LBB0_8 jmp .LBB0_9 .LBB0_6: cmpl $4013630, %ecx je .LBB0_8 cmpl $4013116, %ecx jne .LBB0_9 .LBB0_8: xorl %eax, %eax retq ``` </details>
2025-07-15constify `Index` trait and its slice implsOli Scherer-4/+6
2025-07-15constify some methods using `SliceIndex`Oli Scherer-2/+4
2025-07-15constify `SliceIndex` traitOli Scherer-9/+18
2025-07-13core: make `str::split_at_unchecked()` inlineRené Kijewski-0/+1
This PR adds `#[inline]` to the method `str::split_at_unchecked()`. This is done for two reasons: 1. The method is tiny, e.g. on AMD-64 (<https://godbolt.org/z/ba68fdfxn>): ```asm movq %rdi, %rax subq %rcx, %rdx movq %rsi, (%rdi) addq %rcx, %rsi movq %rcx, 8(%rdi) movq %rsi, 16(%rdi) movq %rdx, 24(%rdi) retq ``` 2. More importantly, inlining the method enables further automatic optimizations. E.g. if you split at index 3, then in the compiler (rustc, llvm or both) knows that this code cannot fail, and the panicking path is omitted in the generated code: ```rust pub fn punctuation(i: &str) -> Result<(), ()> { const THREE_CHARS: &[[u8; 3]] = &[*b"<<=", *b">>=", *b"...", *b"..="]; if let Some((head, _)) = i.split_at_checked(3) && THREE_CHARS.contains(&head.as_bytes().try_into().unwrap()) { Ok(()) } else { Err(()) } } ``` <details> <summary>Without PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=0234de8158f467eebd73286f20d6e27a> ```asm playground::punctuation: subq $40, %rsp movq %rsi, %rdx movq %rdi, %rsi movb $1, %al cmpq $3, %rdx ja .LBB2_2 je .LBB2_3 .LBB2_11: addq $40, %rsp retq .LBB2_2: cmpb $-64, 3(%rsi) jl .LBB2_11 .LBB2_3: leaq 8(%rsp), %rdi movl $3, %ecx callq *core::str::<impl str>::split_at_unchecked@GOTPCREL(%rip) movq 8(%rsp), %rcx movb $1, %al testq %rcx, %rcx je .LBB2_11 cmpq $3, 16(%rsp) jne .LBB2_12 movzwl (%rcx), %edx movzbl 2(%rcx), %ecx shll $16, %ecx orl %edx, %ecx cmpl $4013115, %ecx jg .LBB2_8 cmpl $3026478, %ecx je .LBB2_10 cmpl $4009518, %ecx je .LBB2_10 jmp .LBB2_11 .LBB2_8: cmpl $4013630, %ecx je .LBB2_10 cmpl $4013116, %ecx jne .LBB2_11 .LBB2_10: xorl %eax, %eax addq $40, %rsp retq .LBB2_12: leaq .Lanon.d98a7fbb86d10a97c24516e267466134.2(%rip), %rdi leaq .Lanon.d98a7fbb86d10a97c24516e267466134.1(%rip), %rcx leaq .Lanon.d98a7fbb86d10a97c24516e267466134.6(%rip), %r8 leaq 7(%rsp), %rdx movl $43, %esi callq *core::result::unwrap_failed@GOTPCREL(%rip) ``` </details> <details> <summary>With PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=5d4058c79ce0f6cb1a434190427d2055> ```asm playground::punctuation: movb $1, %al cmpq $3, %rsi ja .LBB0_2 je .LBB0_3 .LBB0_9: retq .LBB0_2: cmpb $-64, 3(%rdi) jl .LBB0_9 .LBB0_3: movzwl (%rdi), %eax movzbl 2(%rdi), %ecx shll $16, %ecx orl %eax, %ecx movb $1, %al cmpl $4013115, %ecx jg .LBB0_6 cmpl $3026478, %ecx je .LBB0_8 cmpl $4009518, %ecx je .LBB0_8 jmp .LBB0_9 .LBB0_6: cmpl $4013630, %ecx je .LBB0_8 cmpl $4013116, %ecx jne .LBB0_9 .LBB0_8: xorl %eax, %eax retq ``` </details>
2025-07-13update issue number for `const_trait_impl`Deadbeef-2/+2
2025-07-08collect.rs: remove empty line after doc commentMarijn Schouten-1/+0
2025-07-07Make `Default` const and add some `const Default` implsEsteban Küber-2/+4
Full list of `impl const Default` types: - () - bool - char - Cell - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
2025-07-07lib: more eagerly return `self.len()` from `ceil_char_boundary`Simonas Kazlauskas-1/+1
There is no reason to go through the complicated branch as it would always return `self.len()` in this case. Also helps debug code somewhat and I guess might make optimizations easier (although I haven't really a sample to demonstrate this.) ref. #93743 Suggested by @chrisduerr
2025-07-05use `div_ceil` instead of manual logicFolkert de Vries-3/+3
2025-07-05use `is_multiple_of` instead of manual moduloFolkert de Vries-2/+2
2025-07-01Update version placeholdersJosh Stone-1/+1
2025-06-24Rollup merge of #142779 - Erk-:fix/core/142734, r=jhprattGuillaume Gomez-0/+6
Add note about `str::split` handling of no matches. Adds small note and example to the test for a non matching pattern resolves rust-lang/rust#142734
2025-06-20Add note about `str::split` handling of no matches.Valdemar Erk-0/+6
2025-06-11Add trim_prefix and trim_suffix for slice and str.Deven T. Corzine-0/+77
Implements `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types which remove at most one occurrence of a prefix/suffix while always returning a string/slice (rather than Option), enabling easy method chaining.
2025-06-05Stabilize `const_eq_ignore_ascii_case`Paolo Barbolini-1/+1
2025-06-03`FromStr`: Rework explanation of `FromStr`/`Display` round-trippingJosh Triplett-6/+5
- Drop the phrasing "usually a mistake". - Mention that `Display` may not be lossless. - Drop a misplaced parenthetical about round-tripping that didn't fit the paragraph it was in.
2025-06-03Add some more description of interactions between `Display` and `FromStr`Josh Triplett-0/+6
2025-06-03Improve the documentation of `Display` and `FromStr`, and their interactionsJosh Triplett-0/+9
In particular: - `Display` is not necessarily lossless - The output of `Display` might not be parseable by `FromStr`, and might not produce the same value if it is. - Calling `.parse()` on the output of `Display` is usually a mistake unless a type's documented output and input formats match. - The input formats accepted by `FromStr` depend on the type.
2025-05-31Rollup merge of #141112 - xizheyin:issue-141079, r=Mark-SimulacrumMatthias Krüger-0/+8
std: note that `std::str::from_utf8*` functions are aliases to `<str>::from_utf8*` methods Closes #141079 r? libs
2025-05-31std: note that `std::str::from_utf8*` functions are aliases to ↵xizheyin-0/+8
`std::<str>::from_utf8*` methods Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-05-28Revert "increase perf of charsearcher for single ascii characters"Jakub Beránek-32/+2
This reverts commit 245bf503e2a948ac98170516d11df632e85a948b.
2025-05-27Auto merge of #129658 - saethlin:spare-a-crumb, r=jhprattbors-0/+4
Add some track_caller info to precondition panics Currently, when you encounter a precondition check, you'll always get the caller location of the implementation of the precondition checks. But with this PR, you'll be told the location of the invalid call. Which is useful. I thought of this while looking at https://github.com/rust-lang/rust/pull/129642#issuecomment-2311703898. The changes to `tests/ui/const*` happen because the const-eval interpreter skips `#[track_caller]` frames in its backtraces. The perf implications of this are: * Increased debug binary sizes. The caller_location implementation requires that the additional data we want to display here be stored in const allocations, which are deduplicated but not across crates. There is no impact on optimized build sizes. The panic path and the caller location data get optimized out. * The compile time hit to opt-incr-patched bitmaps happens because the patch changes the line number of some function calls with precondition checks, causing us to go from 0 dirty CGUs to 1 dirty CGU. * The other compile time hits are marginal but real, and due to doing a handful of new queries. Adding more useful data isn't completely free.
2025-05-27Rollup merge of #141516 - bend-n:okay, r=workingjubilee许杰友 Jieyou Xu (Joe)-2/+32
speed up charsearcher for ascii chars attempt at fixing rust-lang/rust#82471 this implementation should be valid because ascii characters are always one byte and there are no continuation bytes that overlap with ascii characters im not completely sure that this is _always_ an improvement but it seems to be an improvement for this case and i dont think it can significantly regress any cases
2025-05-26Rollup merge of #140952 - SimonSapin:ascii_whitespace_definition, r=dtolnayJacob Pratt-1/+3
Specify that split_ascii_whitespace uses the same definition as is_ascii_whitespace
2025-05-26increase perf of charsearcher for single ascii charactersbendn-2/+32