about summary refs log tree commit diff
path: root/src/libcore/str
AgeCommit message (Collapse)AuthorLines
2020-04-22Rollup merge of #71366 - faern:use-assoc-int-consts3, r=dtolnayYuki Okushi-1/+0
Use assoc int consts3 Define module level int consts with associated constants instead of `min_value()` and `max_value()`. So the code become consistent with what the docs recommend etc. Seems natural. Also remove the last usages of the int module constants from this repo (except src/test/ directory which I have still not really done anything in). Some places were missed in the previous PRs because the code uses `crate::<IntTy>` to reach the constants. This is a continuation of #70857 r? @dtolnay
2020-04-20Stop accessing module level int consts via crate::<Ty>Linus Färnstrand-1/+0
2020-04-19Update pattern docs.Eric Huss-37/+150
2020-04-13Add examples to Pattern docsIvan Tham-0/+26
2020-04-13Add period to Pattern docsIvan Tham-5/+5
2020-03-31Rollup merge of #70588 - Coder-256:str-split-at-docs, r=Dylan-DPCMazdak Farrokhzad-2/+2
Fix incorrect documentation for `str::{split_at, split_at_mut}` The documentation for each method currently states: > Panics if `mid` is not on a UTF-8 code point boundary, or if it is beyond the last code point of the string slice. However, this is not consistent with the real behavior, or that of the corresponding methods for `[T]` slices. A comment inside each of the `str` methods states: > is_char_boundary checks that the index is in [0, .len()] That is what I would expect the behavior to be, and in fact this seems to be the real behavior. For example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8e03dcc209d4dd176df2297523f9fee1)): ```rust fn main() { // Prints ("abc", "") and doesn't panic println!("{:?}", "abc".split_at(3)); } ``` In this case, I would interpret "the last code point of the string slice" to mean the byte at index 2 in UTF-8. However, it is possible to pass an index of 3, which is definitely "beyond the last code point of the string slice". I think that this is much clearer, but feel free to bikeshed.
2020-03-30Fix incorrect documentation for `str::{split_at, split_at_mut}`Jacob Greenfield-2/+2
2020-03-30Optimize strip_prefix and strip_suffix with str patternsNikhil Benesch-30/+92
Constructing a Searcher in strip_prefix and strip_suffix is unnecessarily slow when the pattern is a fixed-length string. Add strip_prefix and strip_suffix methods to the Pattern trait, and add optimized implementations of these methods in the str implementation. The old implementation is retained as the default for these methods.
2020-03-23#[track_caller] on core::ops::{Index, IndexMut}.Adam Perry-0/+2
2020-03-07Use ?-operator in more places (clippy::question_mark, had some false ↵Matthias Krüger-8/+5
negatives fixed recently)
2020-03-05Make link to `std::str` activeLeSeulArtichaut-1/+3
2020-02-26don't take redundant references to operandsMatthias Krüger-1/+1
2020-02-22Relax str::get_unchecked precondition to permit empty slicingridiculousfish-4/+4
Prior to this commit, `str` documented that `get_unchecked` had the precondition that "`begin` must come before `end`". This would appear to prohibit empty slices (i.e. begin == end). In practice, get_unchecked is called often with empty slices. Let's relax the precondition so as to allow them.
2020-02-22Auto merge of #67330 - golddranks:split_inclusive, r=kodrausbors-0/+150
Implement split_inclusive for slice and str # Overview * Implement `split_inclusive` for `slice` and `str` and `split_inclusive_mut` for `slice` * `split_inclusive` is a substring/subslice splitting iterator that includes the matched part in the iterated substrings as a terminator. * EDIT: The behaviour has now changed, as per @KodrAus 's input, to the same semantics with the `split_terminator` function. I updated the examples below. * Two examples below: ```Rust let data = "\nMäry häd ä little lämb\nLittle lämb\n"; let split: Vec<&str> = data.split_inclusive('\n').collect(); assert_eq!(split, ["\n", "Märy häd ä little lämb\n", "Little lämb\n"]); ``` ```Rust let uppercase_separated = "SheePSharKTurtlECaT"; let mut first_char = true; let split: Vec<&str> = uppercase_separated.split_inclusive(|c: char| { let split = !first_char && c.is_uppercase(); first_char = split; split }).collect(); assert_eq!(split, ["SheeP", "SharK", "TurtlE", "CaT"]); ``` # Justification for the API * I was surprised to find that stdlib currently only has splitting iterators that leave out the matched part. In my experience, wanting to leave a substring terminator as a part of the substring is a pretty common usecase. * This API is strictly more expressive than the standard `split` API: it's easy to get the behaviour of `split` by mapping a subslicing operation that drops the terminator. On the other hand it's impossible to derive this behaviour from `split` without using hacky and brittle `unsafe` code. The normal way to achieve this functionality would be implementing the iterator yourself. * Especially when dealing with mutable slices, the only way currently is to use `split_at_mut`. This API provides an ergonomic alternative that plays to the strengths of the iterating capabilities of Rust. (Using `split_at_mut` iteratively used to be a real pain before NLL, fortunately the situation is a bit better now.) # Discussion items * <s>Does it make sense to mimic `split_terminator` in that the final empty slice would be left off in case of the string/slice ending with a terminator? It might do, as this use case is naturally geared towards considering the matching part as a terminator instead of a separator.</s> * EDIT: The behaviour was changed to mimic `split_terminator`. * Does it make sense to have `split_inclusive_mut` for `&mut str`?
2020-02-20Auto merge of #69256 - nnethercote:misc-inlining, r=Centrilbors-1/+1
Miscellaneous inlining improvements These commits inline some hot functions that aren't currently inlined, for some speed wins. r? @Centril
2020-02-18Always inline `run_utf8_validation`.Nicholas Nethercote-1/+1
It only has two call sites, and the one within `from_utf8` is hot within rustc itself.
2020-02-17Rollup merge of #68495 - sdegutis:patch-1, r=Mark-SimulacrumYuki Okushi-1/+2
Updating str.chars docs to mention crates.io. This might spare someone else a little time searching the stdlib for unicode/grapheme support.
2020-02-09Don't return empty slice on last iteration with matched terminator. Test ↵Pyry Kontio-18/+45
reverse iteration.
2020-02-09Implement split_inclusive for slice and str, an splitting iterator that ↵Pyry Kontio-0/+123
includes the matched part in the iterated substrings as a terminator.
2020-02-01Remove some unsound specializationsMatthew Jasper-5/+5
2020-01-23Updating str.chars docs to mention crates.io.Steven Degutis-1/+2
This might spare someone else a little time searching the stdlib for unicode/grapheme support.
2020-01-16Fix formatting: ./x.py fmtPhoebe Bell-15/+5
2020-01-16Move comments for tidyPhoebe Bell-5/+5
2020-01-16Elaborate on SAFETY commentsPhoebe Bell-37/+39
2020-01-16Apply suggestions from code reviewPhoebe Bell-1/+1
Co-Authored-By: Ralf Jung <post@ralfj.de>
2020-01-16Document unsafe blocks in core::{cell, str, sync}Phoebe Bell-13/+50
2020-01-08Use matches macro in libcore and libstdIgor Aleksanov-8/+2
2019-12-22Format the worldMark Rousskov-302/+396
2019-12-22Rollup merge of #67480 - rossmacarthur:fix-41260-avoid-issue-0-part-2, r=CentrilMazdak Farrokhzad-6/+6
Require issue = "none" over issue = "0" in unstable attributes These changes make the use of `issue = "none"` required in unstable attributes throughout the compiler. Notes: - #66299 is now in beta so `issue = "none"` is accepted. - The `tidy` tool now fails on `issue = "0"`. - Tests that used `issue = "0"` were changed to use `issue = "none"`, except for _one_ that asserts `issue = "0"` can still be used. - The compiler still allows `issue = "0"` because some submodules require it, this could be disallowed once these are updated. Resolves #41260 r? @varkor
2019-12-21Require issue = "none" over issue = "0" in unstable attributesRoss MacArthur-6/+6
2019-12-21Fix src/libcore/str/mod.rs doc commentsBroono Lu-1/+1
2019-12-18Propagate cfg bootstrapMark Rousskov-7/+4
2019-12-16Rollup merge of #67249 - ranma42:improve-starts-with-literal-char, r=BurntSushiMazdak Farrokhzad-15/+4
Improve code generated for `starts_with(<literal char>)` This PR includes two minor improvements to the code generated when checking for string prefix/suffix. The first commit simplifies the str/str operation, by taking advantage of the raw UTF-8 representation. The second commit replaces the current str/char matching logic with a char->str encoding and then the previous method. The resulting code should be equivalent in the generic case (one char is being encoded versus one char being decoded), but it becomes easy to optimize in the case of a literal char, which in most cases a developer might expect to be at least as simple as that of a literal string. This PR should fix #41993
2019-12-16Rollup merge of #66735 - SOF3:feature/str_strip, r=KodrAusMazdak Farrokhzad-1/+72
Add str::strip_prefix and str::strip_suffix Introduces a counterpart for `Path::strip_prefix` on `str`. This was also discussed in https://internals.rust-lang.org/t/pre-pr-path-strip-prefix-counterpart-in-str/11364/.
2019-12-15Set tracking issue for str_stripSOFe-2/+2
2019-12-13Require stable/unstable annotations for the constness of all stable ↵Oliver Scherer-0/+7
functions with a `const` modifier
2019-12-12Minor cleanup in `Pattern::{is_prefix_of,is_suffix_of}` for `char`Andrea Canciani-4/+2
2019-12-12Prefer encoding the char when checking for string prefix/suffixAndrea Canciani-10/+4
This enables constant folding when matching a literal char. Fixes #41993.
2019-12-11Improve `str` prefix/suffix comparisonAndrea Canciani-5/+2
The comparison can be performed on the raw bytes, as the chars can only match if their UTF8 encoding matches. This avoids the `is_char_boundary` checks and translates to a straight `u8` slice comparison which is optimized to a memcmp or inline comparison where appropriate.
2019-12-11Some small readability improvementsAndre Bogus-6/+1
2019-12-03Fix documentation of pattern for str::matches()Sen Jiang-2/+2
Made it the same as rmatches()
2019-11-26Fixed formatting issuesSOFe-2/+4
2019-11-26Improved comments to clarify sasumptions in str::strip_prefixSOFe-5/+7
2019-11-25Add str::strip_prefix and str::strip_suffixSOFe-1/+68
2019-11-06Have tidy ensure that we document all `unsafe` blocks in libcoreOliver Scherer-0/+5
2019-11-01doc(str): show example of chars().count() under len()Jeff Dickey-2/+2
the docs are great at explaining that .len() isn't like in other languages but stops short of explaining how to get the character length. r? @steveklabnik
2019-10-20Remove leading :: from paths in doc examplesMikko Rantanen-2/+2
2019-10-04Allow unused attributes to avoid incremental bugMark Rousskov-0/+1
2019-09-25Snap cfgs to new betaMark Rousskov-4/+1
2019-09-24Stabilize `str::len`, `[T]::len`, `is_empty` and `str::as_bytes` as const fnOliver Scherer-3/+5