about summary refs log tree commit diff
path: root/library/alloc/src/str.rs
AgeCommit message (Collapse)AuthorLines
2025-06-06remove extraneous textTshepang Mbambo-4/+0
"Basic usage" implies there is an example that shows advanced usage
2025-05-21add doc alias `replace_first` for `str::replacen`waffle-0/+1
2025-05-20use Self alias in self types rather than manually substituting itMichael Goulet-2/+2
2025-04-27Rollup merge of #137714 - DiuDiu777:doc-fix, r=tgross35Matthias Krüger-0/+4
Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked` ## PR Description​ This PR addresses missing safety documentation for two APIs: ​**1. alloc::ffi::CStr::from_raw**​ - ​`Alias`: The pointer ​must not be aliased​ (accessed via other pointers) during the reconstructed CString's lifetime. - `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free. - `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation: ``` use std::ffi::CString; use std::os::raw::c_char; fn create_dangling() -> *mut c_char { let local_ptr: *mut c_char = { let valid_data = CString::new("valid").unwrap(); valid_data.into_raw() }; unsafe { let _x = CString::from_raw(local_ptr); } local_ptr } fn main() { let dangling = create_dangling(); unsafe {let _y = CString::from_raw(dangling);} // Cause UB! } ``` ​**2. alloc::str::from_boxed_utf8_unchecked**​ - `ValidStr`: Bytes must contain a ​valid UTF-8 sequence.
2025-04-27fix missing doc in CString::from_raw and str::from_boxed_utf8_uncheckedLemonJ-0/+4
2025-03-07Fully test the alloc crate through alloctestsbjorn3-3/+0
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2024-11-06Inline str::repeatKornel-0/+1
2024-10-22better default capacity for str::replaceLaiho-4/+8
2024-10-17Auto merge of #130223 - LaihoE:faster_str_replace, r=thomccbors-1/+24
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-09-23Add fast path for ascii to ascii in str::replaceLaiho-1/+24
2024-09-23Improve autovectorization of to_lowercase / to_uppercase functionsJörn Horstmann-52/+77
Refactor the code in the `convert_while_ascii` helper function to make it more suitable for auto-vectorization and also process the full ascii prefix of the string. The generic case conversion logic will only be invoked starting from the first non-ascii character. The runtime on microbenchmarks with ascii-only inputs improves between 1.5x for short and 4x for long inputs on x86_64 and aarch64. The new implementation also encapsulates all unsafe inside the `convert_while_ascii` function. Fixes #123712
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-7/+7
2024-07-29Reformat `use` declarations.Nicholas Nethercote-11/+9
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-2/+2
Use a GAT for `Searcher` associated type because this trait is always implemented for every lifetime anyway.
2024-06-25Simplify `str::clone_into`DaniPopes-3/+4
Removes an `unsafe` in favor of just using `String` methods.
2024-05-08fix #124714 str.to_lowercase sigma handlingMarcondiro-4/+6
2024-05-01Replace version placeholders for 1.79Mark Rousskov-1/+1
2024-04-24Stabilize Utf8ChunksDavid Tolnay-1/+1
2024-01-25Export core::str::from_raw_parts{,_mut} into alloc::str and std::strDavid Tolnay-0/+2
2023-08-15[nit] Fix a comment typo.Taras Tsugrii-1/+1
2023-08-02str.rs: remove "Basic usage" textTshepang Mbambo-6/+0
Only one example is given
2023-04-26Spelling library/Josh Soref-3/+3
* advance * aligned * borrowed * calculate * debugable * debuggable * declarations * desugaring * documentation * enclave * ignorable * initialized * iterator * kaboom * monomorphization * nonexistent * optimizer * panicking * process * reentrant * rustonomicon * the * uninitialized Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2023-03-25Improve documentation for str::replace() and str::replacen()Veera-2/+2
Currently, to know what the function will return when the pattern doesn't match, the docs require the reader to understand the implementation detail and mentally evaluate or run the example code. It is not immediately clear. This PR makes it more explicit so the reader can quickly find the information.
2023-01-16Avoid unsafe code in `to_ascii_[lower/upper]case()`Chayim Refael Friedman-8/+6
2022-08-22Rollup merge of #93162 - camsteffen:std-prim-docs, r=Mark-SimulacrumDylan DPC-21/+1
Std module docs improvements My primary goal is to create a cleaner separation between primitive types and primitive type helper modules (fixes #92777). I also changed a few header lines in other top-level std modules (seen at https://doc.rust-lang.org/std/) for consistency. Some conventions used/established: * "The \`Box\<T>` type for heap allocation." - if a module mainly provides a single type, name it and summarize its purpose in the module header * "Utilities for the _ primitive type." - this wording is used for the header of helper modules * Documentation for primitive types themselves are removed from helper modules * provided-by-core functionality of primitive types is documented in the primitive type instead of the helper module (such as the "Iteration" section in the slice docs) I wonder if some content in `std::ptr` should be in `pointer` but I did not address this.
2022-08-20Improve primitive/std docs separation and headersCameron Steffen-21/+1
2022-08-20Expose `Utf8Lossy` as `Utf8Chunks`dylni-0/+2
2022-06-10additional docs example for replace **all** of strbvanjoi-0/+1
2022-05-26improve case conversion happy pathConrad Ludgate-5/+69
2022-04-05trivial cfg(bootstrap) changesPietro Albini-10/+9
2022-03-30remove now unnecessary lang itemslcnr-1/+1
2022-03-30rework implementation for inherent impls for builtin typeslcnr-0/+9
2021-12-08Use spare_capacity_mut instead of invalid unchecked indexingBen Kimock-2/+10
2021-11-05Fix `str::SplitInclusive` stabilisation datembartlett21-1/+1
2021-10-15Add #[must_use] to remaining alloc functionsJohn Kugelman-1/+4
2021-10-12Fix uppercase/lowercase errorJohn Kugelman-1/+1
2021-10-10Add #[must_use] to from_value conversionsJohn Kugelman-0/+1
2021-10-09Add #[must_use] to string/char transformation methodsJohn Kugelman-0/+6
These methods could be misconstrued as modifying their arguments instead of returning new values. Where possible I made the note recommend a method that does mutate in place.
2021-06-23Use HTTPS links where possibleSmitty-1/+1
2021-05-05alloc: Add unstable Cfg feature `no-global_oom_handling`John Ericson-0/+13
For certain sorts of systems, programming, it's deemed essential that all allocation failures be explicitly handled where they occur. For example, see Linus Torvald's opinion in [1]. Merely not calling global panic handlers, or always `try_reserving` first (for vectors), is not deemed good enough, because the mere presence of the global OOM handlers is burdens static analysis. One option for these projects to use rust would just be to skip `alloc`, rolling their own allocation abstractions. But this would, in my opinion be a real shame. `alloc` has a few `try_*` methods already, and we could easily have more. Features like custom allocator support also demonstrate and existing to support diverse use-cases with the same abstractions. A natural way to add such a feature flag would a Cargo feature, but there are currently uncertainties around how std library crate's Cargo features may or not be stable, so to avoid any risk of stabilizing by mistake we are going with a more low-level "raw cfg" token, which cannot be interacted with via Cargo alone. Note also that since there is no notion of "default cfg tokens" outside of Cargo features, we have to invert the condition from `global_oom_handling` to to `not(no_global_oom_handling)`. This breaks the monotonicity that would be important for a Cargo feature (i.e. turning on more features should never break compatibility), but it doesn't matter for raw cfg tokens which are not intended to be "constraint solved" by Cargo or anything else. To support this use-case we create a new feature, "global-oom-handling", on by default, and put the global OOM handler infra and everything else it that depends on it behind it. By default, nothing is changed, but users concerned about global handling can make sure it is disabled, and be confident that all OOM handling is local and explicit. For this first iteration, non-flat collections are outright disabled. `Vec` and `String` don't yet have `try_*` allocation methods, but are kept anyways since they can be oom-safely created "from parts", and we hope to add those `try_` methods in the future. [1]: https://lore.kernel.org/lkml/CAHk-=wh_sNLoz84AUUzuqXEsYH35u=8HV3vK-jbRbJ_B-JjGrg@mail.gmail.com/
2021-03-28Auto merge of #81728 - Qwaz:fix-80335, r=joshtriplettbors-19/+25
Fixes API soundness issue in join() Fixes #80335
2021-03-23Bump alloc::str::SplitInclusive to 1.53.0 releaseDavid Tolnay-1/+1
2021-03-23Expose str::SplitInclusive in alloc and therefore in stdIan Jackson-0/+2
This seems to have been omitted from the beginning when this feature was first introduced in 86bf96291d82. Most users won't need to name this type which is probably why this wasn't noticed in the meantime. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-03-20Update the commentYechan Bae-4/+4
2021-02-25Convert primitives to use intra-doc linksJoshua Nelson-1/+1
2021-02-03Fixes #80335Yechan Bae-18/+24
2020-12-31Remove many unnecessary manual link resolves from libraryCamelid-2/+0
Now that #76934 has merged, we can remove a lot of these! E.g, this is no longer necessary: [`Vec<T>`]: Vec
2020-12-07Privatize some of libcore unicode_internalsAleksey Kladov-1/+1
My understanding is that these API are perma unstable, so it doesn't make sense to pollute docs & IDE completion[1] with them. [1]: https://github.com/rust-analyzer/rust-analyzer/issues/6738
2020-07-27mv std libs to library/mark-0/+576