summary refs log tree commit diff
path: root/library/alloc/src/string.rs
AgeCommit message (Collapse)AuthorLines
2025-03-31replace version placeholderBoxy-10/+10
2025-03-08Stabilize `const_vec_string_slice`Martin Habovstiak-8/+9
This feature was approved for stabilization in https://github.com/rust-lang/rust/issues/129041#issuecomment-2508940661 so this change stabilizes it.
2025-03-07Fully test the alloc crate through alloctestsbjorn3-21/+9
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2025-03-07Rollup merge of #138034 - thaliaarchi:use-prelude-size-of, r=tgross35Matthias Krüger-4/+2
library: Use `size_of` from the prelude instead of imported Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80. try-job: test-various try-job: x86_64-gnu try-job: x86_64-msvc-1
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-4/+2
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-03-05Rollup merge of #137569 - aDotInTheVoid:for-iurii, r=ibraheemdev许杰友 Jieyou Xu (Joe)-2/+1
Stabilize `string_extend_from_within` FCP'd here: https://github.com/rust-lang/rust/issues/103806#issuecomment-2674989531. Closes #103806.
2025-02-24Rollup merge of #136775 - robertbastian:patch-2, r=AmanieuTrevor Gross-5/+2
Update `String::from_raw_parts` safety requirements These have become out of sync with `Vec::from_raw_part`'s safety requirements, and are likely to diverge again. I think it's safest to just point at `Vec`'s requirements. https://github.com/rust-lang/rust/issues/119206#issuecomment-2180116680
2025-02-24Stablize `string_extend_from_within`Alona Enraght-Moony-2/+1
2025-02-24Update string.rsRobert Bastian-2/+2
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2025-02-19Rollup merge of #132268 - elichai:string_try_from_vec, r=AmanieuMatthias Krüger-0/+18
Impl TryFrom<Vec<u8>> for String I think this is useful enough to have :) As a general question, is there any policy around adding "missing" trait implementations? (like adding `AsRef<T> for T` for std types), I mostly stumble upon them when using a lot of "impl Trait in argument position" like (`foo: impl Into<String>`)
2025-02-19Rollup merge of #120580 - HTGAzureX1212:HTGAzureX1212/issue-45795, r=m-ou-seMatthias Krüger-3/+5
Add `MAX_LEN_UTF8` and `MAX_LEN_UTF16` Constants This pull request adds the `MAX_LEN_UTF8` and `MAX_LEN_UTF16` constants as per #45795, gated behind the `char_max_len` feature. The constants are currently applied in the `alloc`, `core` and `std` libraries.
2025-02-16add MAX_LEN_UTF8 and MAX_LEN_UTF16 constantsHTGAzureX1212-3/+5
2025-02-09Update string.rsRobert Bastian-5/+2
2025-02-08Implement Extend<AsciiChar> for StringMatthew Zeitlin-0/+26
2025-01-31Update encode_utf16 to mention it is native endianMarijn Schouten-8/+8
2024-12-22Impl String::into_charstison-2/+185
Signed-off-by: tison <wander4096@gmail.com>
2024-12-13Remove support for specializing ToString outside the standard librarybjorn3-37/+32
This is the only trait specializable outside of the standard library. Before stabilizing specialization we will probably want to remove support for this. It was originally made specializable to allow a more efficient ToString in libproc_macro back when this way the only way to get any data out of a TokenStream. We now support getting individual tokens, so proc macros no longer need to call it as often.
2024-12-05Fixed another broken testElias Holzmann-2/+2
2024-12-05Added struct `fmt::FormattingOptions`Elias Holzmann-1/+2
This allows to build custom `std::Formatter`s at runtime. Also added some related enums and two related methods on `std::Formatter`.
2024-11-17alloc: fix `String`'s docYutaro Ohno-1/+1
2024-10-28Impl TryFrom<Vec<u8>> for StringElichai Turkel-0/+18
2024-10-17Auto merge of #130223 - LaihoE:faster_str_replace, r=thomccbors-1/+6
optimize str.replace Adds a fast path for str.replace for the ascii to ascii case. This allows for autovectorizing the code. Also should this instead be done with specialization? This way we could remove one branch. I think it is the kind of branch that is easy to predict though. Benchmark for the fast path (replace all "a" with "b" in the rust wikipedia article, using criterion) : | N | Speedup | Time New (ns) | Time Old (ns) | |----------|---------|---------------|---------------| | 2 | 2.03 | 13.567 | 27.576 | | 8 | 1.73 | 17.478 | 30.259 | | 11 | 2.46 | 18.296 | 45.055 | | 16 | 2.71 | 17.181 | 46.526 | | 37 | 4.43 | 18.526 | 81.997 | | 64 | 8.54 | 18.670 | 159.470 | | 200 | 9.82 | 29.634 | 291.010 | | 2000 | 24.34 | 81.114 | 1974.300 | | 20000 | 30.61 | 598.520 | 18318.000 | | 1000000 | 29.31 | 33458.000 | 980540.000 |
2024-10-07Rollup merge of #128399 - mammothbane:master, r=Amanieu,tgross35Stuart Cook-13/+25
liballoc: introduce String, Vec const-slicing This change `const`-qualifies many methods on `Vec` and `String`, notably `as_slice`, `as_str`, `len`. These changes are made behind the unstable feature flag `const_vec_string_slice`. ## Motivation This is to support simultaneous variance over ownership and constness. I have an enum type that may contain either `String` or `&str`, and I want to produce a `&str` from it in a possibly-`const` context. ```rust enum StrOrString<'s> { Str(&'s str), String(String), } impl<'s> StrOrString<'s> { const fn as_str(&self) -> &str { match self { // In a const-context, I really only expect to see this variant, but I can't switch the implementation // in some mode like #[cfg(const)] -- there has to be a single body Self::Str(s) => s, // so this is a problem, since it's not `const` Self::String(s) => s.as_str(), } } } ``` Currently `String` and `Vec` don't support this, but can without functional changes. Similar logic applies for `len`, `capacity`, `is_empty`. ## Changes The essential thing enabling this change is that `Unique::as_ptr` is `const`. This lets us convert `RawVec::ptr` -> `Vec::as_ptr` -> `Vec::as_slice` -> `String::as_str`. I had to move the `Deref` implementations into `as_{str,slice}` because `Deref` isn't `#[const_trait]`, but I would expect this change to be invisible up to inlining. I moved the `DerefMut` implementations as well for uniformity.
2024-10-06liballoc: introduce String, Vec const-slicingNathan Perry-13/+25
This change `const`-qualifies many methods on Vec and String, notably `as_slice`, `as_str`, `len`. These changes are made behind the unstable feature flag `const_vec_string_slice` with the following tracking issue: https://github.com/rust-lang/rust/issues/129041
2024-09-23Add fast path for ascii to ascii in str::replaceLaiho-1/+6
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-3/+3
2024-09-21Rollup merge of #130408 - okaneco:into_lossy_refactor, r=NoratriebMichael Goulet-1/+25
Avoid re-validating UTF-8 in `FromUtf8Error::into_utf8_lossy` Part of the unstable feature `string_from_utf8_lossy_owned` - #129436 Refactor `FromUtf8Error::into_utf8_lossy` to copy valid UTF-8 bytes into the buffer, avoiding double validation of bytes. Add tests that mirror the `String::from_utf8_lossy` tests.
2024-09-20Avoid re-validating UTF-8 in `FromUtf8Error::into_utf8_lossy`okaneco-1/+25
Refactor `into_utf8_lossy` to copy valid UTF-8 bytes into the buffer, avoiding double validation of bytes. Add tests that mirror the `String::from_utf8_lossy` tests
2024-09-19[Clippy] Swap `unnecessary_owned_empty_strings` to use diagnostic item ↵GnomedDev-0/+1
instead of path
2024-09-19[Clippy] Swap `unnecessary_to_owned` to use diagnostic item instead of pathGnomedDev-0/+1
2024-09-19[Clippy] Swap `single_char_add_str`/`format_push_string` to use diagnostic ↵GnomedDev-0/+2
items instead of paths
2024-09-19[Clippy] Swap `option_as_ref_deref` to use diagnostic items instead of pathsGnomedDev-0/+2
2024-09-15Rollup merge of #129439 - okaneco:vec_string_lossy, r=NoratriebMatthias Krüger-0/+74
Implement feature `string_from_utf8_lossy_owned` for lossy conversion from `Vec<u8>` to `String` methods Accepted ACP: https://github.com/rust-lang/libs-team/issues/116 Tracking issue: #129436 Implement feature for lossily converting from `Vec<u8>` to `String` - Add `String::from_utf8_lossy_owned` - Add `FromUtf8Error::into_utf8_lossy` --- Related to #64727, but unsure whether to mark it "fixed" by this PR. That issue partly asks for in-place replacement of the original allocation. We fulfill the other half of that request with these functions. closes #64727
2024-09-09Add missing `#[allow(missing_docs)]` on hack functions in allocUrgau-0/+1
2024-08-31Fix `elided_named_lifetimes` in codePavel Grigorenko-1/+1
2024-08-22Implement feature `string_from_utf8_lossy_owned`okaneco-0/+74
Implement feature for lossily converting from `Vec<u8>` to `String` - Add `String::from_utf8_lossy_owned` - Add `FromUtf8Error::into_utf8_lossy`
2024-08-07alloc: make `to_string_str!` a bit less complexMichael Howell-22/+35
2024-08-06alloc: add ToString specialization for `&&str`Michael Howell-8/+35
Fixes #128690
2024-08-03Rollup merge of #127586 - zachs18:more-must-use, r=cuviperMatthias Krüger-1/+1
Add `#[must_use]` to some `into_raw*` functions. cc #121287 r? ``@cuviper`` Adds `#[must_use = "losing the pointer will leak memory"]`[^1] to `Box::into_raw(_with_allocator)`, `Vec::into_raw_parts(_with_alloc)`, `String::into_raw_parts`[^2], and `rc::{Rc, Weak}::into_raw_with_allocator` (Rc's normal `into_raw` and all of `Arc`'s `into_raw*`s are already `must_use`). Adds `#[must_use = "losing the raw <resource name may leak resources"]` to `IntoRawFd::into_raw_fd`, `IntoRawSocket::into_raw_socket`, and `IntoRawHandle::into_raw_handle`. [^1]: "*will* leak memory" may be too-strong wording (since `Box`/`Vec`/`String`/`rc::Weak` might not have a backing allocation), but I left it as-is for simplicity and consistency. [^2]: `String::into_raw_parts`'s `must_use` message is changed from the previous (possibly misleading) "`self` will be dropped if the result is not used".
2024-07-29Reformat `use` declarations.Nicholas Nethercote-4/+1
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-15Remove generic lifetime parameter of trait `Pattern`Benoît du Garreau-12/+15
Use a GAT for `Searcher` associated type because this trait is always implemented for every lifetime anyway.
2024-07-10Clarify/add `must_use` messages for more `into_raw*` functions of `alloc` types.Zachary S-1/+1
2024-07-06Mitigate focused memory leaks in `alloc` doctests for Miri.Zachary S-0/+3
If/when `-Zmiri-disable-leak-check` is able to be used at test-granularity, it should applied to these tests instead of unleaking.
2024-05-19Auto merge of #99969 - calebsander:feature/collect-box-str, r=dtolnaybors-4/+6
alloc: implement FromIterator for Box<str> `Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`. Add analogous `FromIterator` implementations for `Box<str>` matching the current implementations for `String`. Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too. ACP: https://github.com/rust-lang/libs-team/issues/196
2024-05-18Clarify how String::leak and into_boxed_str differJon Gjengset-5/+7
2024-05-05alloc: implement FromIterator for Box<str>Caleb Sander-4/+6
Box<[T]> implements FromIterator<T> using Vec<T> + into_boxed_slice(). Add analogous FromIterator implementations for Box<str> matching the current implementations for String. Remove the Global allocator requirement for FromIterator<Box<str>> too.
2024-04-27WS fix.JirCep-1/+1
2024-04-27String.truncate calls Vec.truncate, in turn, and that statesJirCep-1/+1
"is greater or equal to". Beside common sense.
2024-04-26Auto merge of #123909 - dtolnay:utf8chunks, r=joboetbors-3/+1
Stabilize `Utf8Chunks` Pending FCP in https://github.com/rust-lang/rust/issues/99543. This PR includes the proposed modification in https://github.com/rust-lang/libs-team/issues/190 as agreed in https://github.com/rust-lang/rust/issues/99543#issuecomment-2050406568.
2024-04-24Stabilize Utf8ChunksDavid Tolnay-3/+1