Skip to content

Commit

Permalink
Rollup merge of #100311 - xfix:lines-fix-handling-of-bare-cr, r=Chris…
Browse files Browse the repository at this point in the history
…Denton

Fix handling of trailing bare CR in str::lines

Continuing from #91191.

Fixes #94435.
  • Loading branch information
Dylan-DPC committed Mar 22, 2023
2 parents 439292b + cef81dc commit d694f47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
26 changes: 19 additions & 7 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,13 +1499,25 @@ fn test_split_whitespace() {

#[test]
fn test_lines() {
let data = "\nMäry häd ä little lämb\n\r\nLittle lämb\n";
let lines: Vec<&str> = data.lines().collect();
assert_eq!(lines, ["", "Märy häd ä little lämb", "", "Little lämb"]);

let data = "\r\nMäry häd ä little lämb\n\nLittle lämb"; // no trailing \n
let lines: Vec<&str> = data.lines().collect();
assert_eq!(lines, ["", "Märy häd ä little lämb", "", "Little lämb"]);
fn t(data: &str, expected: &[&str]) {
let lines: Vec<&str> = data.lines().collect();
assert_eq!(lines, expected);
}
t("", &[]);
t("\n", &[""]);
t("\n2nd", &["", "2nd"]);
t("\r\n", &[""]);
t("bare\r", &["bare\r"]);
t("bare\rcr", &["bare\rcr"]);
t("Text\n\r", &["Text", "\r"]);
t(
"\nMäry häd ä little lämb\n\r\nLittle lämb\n",
&["", "Märy häd ä little lämb", "", "Little lämb"],
);
t(
"\r\nMäry häd ä little lämb\n\nLittle lämb",
&["", "Märy häd ä little lämb", "", "Little lämb"],
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/str/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::from_utf8_unchecked;
use super::pattern::Pattern;
use super::pattern::{DoubleEndedSearcher, ReverseSearcher, Searcher};
use super::validations::{next_code_point, next_code_point_reverse};
use super::LinesAnyMap;
use super::LinesMap;
use super::{BytesIsNotEmpty, UnsafeBytesToStr};
use super::{CharEscapeDebugContinue, CharEscapeDefault, CharEscapeUnicode};
use super::{IsAsciiWhitespace, IsNotEmpty, IsWhitespace};
Expand Down Expand Up @@ -1104,7 +1104,7 @@ generate_pattern_iterators! {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct Lines<'a>(pub(super) Map<SplitTerminator<'a, char>, LinesAnyMap>);
pub struct Lines<'a>(pub(super) Map<SplitInclusive<'a, char>, LinesMap>);

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Iterator for Lines<'a> {
Expand Down
10 changes: 5 additions & 5 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ impl str {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn lines(&self) -> Lines<'_> {
Lines(self.split_terminator('\n').map(LinesAnyMap))
Lines(self.split_inclusive('\n').map(LinesMap))
}

/// An iterator over the lines of a string.
Expand Down Expand Up @@ -2604,10 +2604,10 @@ impl Default for &mut str {
impl_fn_for_zst! {
/// A nameable, cloneable fn type
#[derive(Clone)]
struct LinesAnyMap impl<'a> Fn = |line: &'a str| -> &'a str {
let l = line.len();
if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
else { line }
struct LinesMap impl<'a> Fn = |line: &'a str| -> &'a str {
let Some(line) = line.strip_suffix('\n') else { return line };
let Some(line) = line.strip_suffix('\r') else { return line };
line
};

#[derive(Clone)]
Expand Down

0 comments on commit d694f47

Please sign in to comment.