about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
AgeCommit message (Collapse)AuthorLines
2023-10-11Rollup merge of #95967 - CAD97:from-utf16, r=dtolnayAli MJ Al-Nasrawy-0/+150
Add explicit-endian String::from_utf16 variants This adds the following APIs under `feature(str_from_utf16_endian)`: ```rust impl String { pub fn from_utf16le(v: &[u8]) -> Result<String, FromUtf16Error>; pub fn from_utf16le_lossy(v: &[u8]) -> String; pub fn from_utf16be(v: &[u8]) -> Result<String, FromUtf16Error>; pub fn from_utf16be_lossy(v: &[u8]) -> String; } ``` These are versions of `String::from_utf16` that explicitly take [UTF-16LE and UTF-16BE](https://unicode.org/faq/utf_bom.html#gen7). Notably, we can do better than just the obvious `decode_utf16(v.array_chunks::<2>().copied().map(u16::from_le_bytes)).collect()` in that: - We handle the case where the byte slice is not an even number of bytes, and - In the case that the UTF-16 is native endian and the slice is aligned, we can forward to `String::from_utf16`. If the Unicode Consortium actively defines how to handle character replacement when decoding a UTF-16 bytestream with a trailing odd byte, I was unable to find reference. However, the behavior implemented here is fairly self-evidently correct: replace the single errant byte with the replacement character.
2023-10-05Add more diagnostic items for clippyJason Newcomb-0/+1
2023-09-29fix char importsChristopher Durham-6/+6
2023-09-28add str_from_utf16_endian tracking issueChristopher Durham-4/+4
2023-09-28style nitsChristopher Durham-6/+6
Co-authored-by: David Tolnay <dtolnay@gmail.com>
2023-09-28Add feature(str_from_utf16_endian)CAD97-0/+150
2023-08-02string.rs: remove "Basic usage" textTshepang Mbambo-72/+1
Only a single example is given
2023-07-24Hide `ToString` implementations that specialize the default onesFrank Steffahn-0/+9
The status quo is highly confusing, since the overlap is not apparent, and specialization is not a feature of Rust. This addresses #87545; I'm not certain if it closes it, since that issue might also be trackign a *general* solution for hiding specializing impls automatically.
2023-07-21Demonstrate multibyte character removal in String::pop and String::remove ↵Andrew Tribick-8/+8
doctests
2023-07-12Replace version placeholder to 1.72Mark Rousskov-1/+1
2023-06-14Rollup merge of #109814 - est31:stabilize_string_leak, r=AmanieuMatthias Krüger-7/+8
Stabilize String::leak Stabilizes the following API: ```Rust impl String { pub fn leak(self) -> &'static mut str; } ``` closes #102929 blocked by having an FCP for stabilization.
2023-05-30Swap out CURRENT_RUSTC_VERSION to 1.71.0Mark Rousskov-1/+1
2023-05-28Stabilize String::leakest31-7/+8
2023-05-27Rollup merge of #111656 - finnbear:string_leak_unbounded_lifetime, r=AmanieuMatthias Krüger-2/+2
Use an unbounded lifetime in `String::leak`. Using `'a` instead of `'static` is predicted to make the process of making `String` generic over an allocator easier/less of a breaking change. See: - https://github.com/rust-lang/rust/pull/109814#issuecomment-1550164195 - https://github.com/rust-lang/rust/pull/109814#issuecomment-1550250163 ACP: https://github.com/rust-lang/libs-team/issues/109
2023-05-18Rollup merge of #111168 - DaniPopes:arguments-to-string, r=Mark-SimulacrumDylan DPC-0/+9
Specialize ToString implementation for fmt::Arguments Generates far fewer instructions by formatting into a String with `fmt::format` directly instead of going through the `fmt::Display` impl. This change is insta-stable.
2023-05-16Use an unbounded lifetime in String::leak.Finn Bear-2/+2
2023-05-08Auto merge of #106621 - ozkanonur:enable-elided-lifetimes-for-doctests, ↵bors-1/+1
r=Mark-Simulacrum enable `rust_2018_idioms` lint group for doctests With this change, `rust_2018_idioms` lint group will be enabled for compiler/libstd doctests. Resolves #106086 Resolves #99144 Signed-off-by: ozkanonur <work@onurozkan.dev>
2023-05-07enable `rust_2018_idioms` for doctestsozkanonur-1/+1
Signed-off-by: ozkanonur <work@onurozkan.dev>
2023-05-03Add the basic `ascii::Char` typeScott McMurray-0/+9
2023-05-04Specialize ToString implementation for fmt::ArgumentsDaniPopes-0/+9
2023-04-16rm const traits in libcoreDeadbeef-2/+1
2023-04-12remove some unneeded importsKaDiWa-2/+2
2023-04-05Derive String's PartialEq implementationKonrad Borowski-9/+1
2023-03-02Remove manual implementation of String::neKonrad Borowski-4/+0
2023-02-12Auto merge of #105671 - lukas-code:depreciate-char, r=scottmcmbors-4/+4
Use associated items of `char` instead of freestanding items in `core::char` The associated functions and constants on `char` have been stable since 1.52 and the freestanding items have soft-deprecated since 1.62 (https://github.com/rust-lang/rust/pull/95566). This PR ~~marks them as "deprecated in future", similar to the integer and floating point modules (`core::{i32, f32}` etc)~~ replaces all uses of `core::char::*` with `char::*` to prepare for future deprecation of `core::char::*`.
2023-02-07Docs: Fix format of headings in String::reserveDanilo Bargen-2/+2
2023-01-25Set version placeholders to 1.68Mark Rousskov-1/+1
2023-01-14Use associated items of `char` instead of freestanding items in `core::char`Lukas Markeffsky-4/+4
2023-01-10impl: specialize impl of `ToString` on `bool`Ezra Shaw-0/+9
2022-12-30Replace libstd, libcore, liballoc in line comments.jonathanCogan-1/+1
2022-12-28Update bootstrap cfgPietro Albini-1/+1
2022-11-18Rm diagnostic item, use lang itemDeadbeef-2/+1
2022-11-17Minimal implementation of implicit deref patternsDeadbeef-0/+1
2022-10-31Add tracking issue for `string_extend_from_within`Sky-1/+1
2022-10-22Fix typo in docs of `String::leak`.Finn Bear-1/+1
2022-10-19Put fn in the right place.Finn Bear-29/+29
2022-10-19Copy of #102941.Finn Bear-1/+30
2022-09-26remove cfg(bootstrap)Pietro Albini-3/+0
2022-08-23Auto merge of #99917 - yaahc:error-in-core-move, r=thomccbors-0/+20
Move Error trait into core This PR moves the error trait from the standard library into a new unstable `error` module within the core library. The goal of this PR is to help unify error reporting across the std and no_std ecosystems, as well as open the door to integrating the error trait into the panic reporting system when reporting panics whose source is an errors (such as via `expect`). This PR is a rewrite of https://github.com/rust-lang/rust/pull/90328 using new compiler features that have been added to support error in core.
2022-08-22Move error trait into coreJane Losare-Lusby-0/+20
2022-08-22Rollup merge of #100331 - lo48576:try-reserve-preserve-on-failure, r=thomccDylan DPC-1/+2
Guarantee `try_reserve` preserves the contents on error Update doc comments to make the guarantee explicit. However, some implementations does not have the statement though. * `HashMap`, `HashSet`: require guarantees on hashbrown side. * `PathBuf`: simply redirecting to `OsString`. Fixes #99606.
2022-08-20Rollup merge of #99544 - dylni:expose-utf8lossy, r=Mark-SimulacrumMatthias Krüger-8/+8
Expose `Utf8Lossy` as `Utf8Chunks` This PR changes the feature for `Utf8Lossy` from `str_internals` to `utf8_lossy` and improves the API. This is done to eventually expose the API as stable. Proposal: rust-lang/libs-team#54 Tracking Issue: #99543
2022-08-20Expose `Utf8Lossy` as `Utf8Chunks`dylni-8/+8
2022-08-10Guarantee `try_reserve` preserves the contents on errorYOSHIOKA Takuma-1/+2
Update doc comments to make the guarantee explicit. However, some implementations does not have the statement though. * `HashMap`, `HashSet`: require guarantees on hashbrown side. * `PathBuf`: simply redirecting to `OsString`. Fixes #99606.
2022-07-26Rollup merge of #98710 - mojave2:string, r=JohnTitorYuki Okushi-5/+5
correct the output of a `capacity` method example The output of this example in std::alloc is different from which shown in the comment. I have tested it on both Linux and Windows.
2022-06-30correct the output of a `capacity` method examplemojave2-5/+5
2022-06-29alloc: fix `no_global_oom_handling` warningsMiguel Ojeda-2/+2
Rust 1.62.0 introduced a couple new `unused_imports` warnings in `no_global_oom_handling` builds, making a total of 5 warnings: ```txt warning: unused import: `Unsize` --> library/alloc/src/boxed/thin.rs:6:33 | 6 | use core::marker::{PhantomData, Unsize}; | ^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `from_fn` --> library/alloc/src/string.rs:51:18 | 51 | use core::iter::{from_fn, FusedIterator}; | ^^^^^^^ warning: unused import: `core::ops::Deref` --> library/alloc/src/vec/into_iter.rs:12:5 | 12 | use core::ops::Deref; | ^^^^^^^^^^^^^^^^ warning: associated function `shrink` is never used --> library/alloc/src/raw_vec.rs:424:8 | 424 | fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> { | ^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: associated function `forget_remaining_elements` is never used --> library/alloc/src/vec/into_iter.rs:126:19 | 126 | pub(crate) fn forget_remaining_elements(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This patch cleans them so that projects compiling `alloc` without infallible allocations do not see the warnings. It also enables the use of `-Dwarnings`. The couple `dead_code` ones may be reverted when some fallible allocation support starts using them. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2022-06-19Fix documentation for with_capacity and reserve families of methodsjmaargh-35/+35
Documentation for the following methods with_capacity with_capacity_in with_capacity_and_hasher reserve reserve_exact try_reserve try_reserve_exact was inconsistent and often not entirely correct where they existed on the following types Vec VecDeque String OsString PathBuf BinaryHeap HashSet HashMap BufWriter LineWriter since the allocator is allowed to allocate more than the requested capacity in all such cases, and will frequently "allocate" much more in the case of zero-sized types (I also checked BufReader, but there the docs appear to be accurate as it appears to actually allocate the exact capacity). Some effort was made to make the documentation more consistent between types as well. Fix with_capacity* methods for Vec Fix *reserve* methods for Vec Fix docs for *reserve* methods of VecDeque Fix docs for String::with_capacity Fix docs for *reserve* methods of String Fix docs for OsString::with_capacity Fix docs for *reserve* methods on OsString Fix docs for with_capacity* methods on HashSet Fix docs for *reserve methods of HashSet Fix docs for with_capacity* methods of HashMap Fix docs for *reserve methods on HashMap Fix expect messages about OOM in doctests Fix docs for BinaryHeap::with_capacity Fix docs for *reserve* methods of BinaryHeap Fix typos Fix docs for with_capacity on BufWriter and LineWriter Fix consistent use of `hasher` between `HashMap` and `HashSet` Fix warning in doc test Add test for capacity of vec with ZST Fix doc test error
2022-05-21Auto merge of #96605 - Urgau:string-retain-codegen, r=thomccbors-8/+17
Improve codegen of String::retain method This pull-request improve the codegen of the `String::retain` method. Using `unwrap_unchecked` helps the optimizer to not generate a panicking path that will never be taken for valid UTF-8 like string. Using `encode_utf8` saves us from an expensive call to `memcpy`, as the optimizer is unable to realize that `ch_len <= 4` and so can generate much better assembly code. https://rust.godbolt.org/z/z73ohenfc
2022-05-01Improve codegen of String::retain method.Loïc BRANSTETT-8/+17
Using unwrap_unchecked helps the optimizer to not generate panicking path, that will never be taken for valid UTF-8 like string. Using encode_utf8 saves us a call to a memcpy, as the optimizer is unable to realize that ch_len <= 4 and so can generate much better assembly code. https://rust.godbolt.org/z/z73ohenfc