Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/cargo/util/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ impl<'s> ScriptSource<'s> {
.push_visible_span(open_start..open_end));
};
let info = input.next_slice(info_nl.start);
let info = info.trim_matches(is_whitespace);
let info = info.strip_suffix('\r').unwrap_or(info); // already excludes `\n`
let info = info.trim_matches(is_horizontal_whitespace);
if !info.is_empty() {
let info_start = info.offset_from(&raw);
let info_end = info_start + info.len();
Expand Down Expand Up @@ -147,7 +148,8 @@ impl<'s> ScriptSource<'s> {
)
.push_visible_span(open_start..open_end));
} else {
let after_closing_fence = after_closing_fence.trim_matches(is_whitespace);
let after_closing_fence = strip_newline(after_closing_fence);
let after_closing_fence = after_closing_fence.trim_matches(is_horizontal_whitespace);
if !after_closing_fence.is_empty() {
// extra characters beyond the original fence pattern
let after_start = after_closing_fence.offset_from(&raw);
Expand Down Expand Up @@ -261,8 +263,6 @@ pub fn strip_ws_lines(input: &str) -> Option<usize> {
/// True if `c` is considered a whitespace according to Rust language definition.
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
/// for definitions of these classes.
///
/// See rust-lang/rust's compiler/rustc_lexer/src/lib.rs `is_whitespace`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we remove this line?

But anyway the link to Rust reference it good enough.

fn is_whitespace(c: char) -> bool {
// This is Pattern_White_Space.
//
Expand All @@ -271,27 +271,46 @@ fn is_whitespace(c: char) -> bool {

matches!(
c,
// Usual ASCII suspects
'\u{0009}' // \t
| '\u{000A}' // \n
// End-of-line characters
| '\u{000A}' // line feed (\n)
| '\u{000B}' // vertical tab
| '\u{000C}' // form feed
| '\u{000D}' // \r
| '\u{0020}' // space

// NEXT LINE from latin1
| '\u{0085}'
| '\u{000D}' // carriage return (\r)
| '\u{0085}' // next line (from latin1)
| '\u{2028}' // LINE SEPARATOR
| '\u{2029}' // PARAGRAPH SEPARATOR

// Bidi markers
// `Default_Ignorable_Code_Point` characters
| '\u{200E}' // LEFT-TO-RIGHT MARK
| '\u{200F}' // RIGHT-TO-LEFT MARK

// Dedicated whitespace characters from Unicode
| '\u{2028}' // LINE SEPARATOR
| '\u{2029}' // PARAGRAPH SEPARATOR
// Horizontal space characters
| '\u{0009}' // tab (\t)
| '\u{0020}' // space
)
}

/// True if `c` is considered horizontal whitespace according to Rust language definition.
fn is_horizontal_whitespace(c: char) -> bool {
// This is Pattern_White_Space.
//
// Note that this set is stable (ie, it doesn't change with different
// Unicode versions), so it's ok to just hard-code the values.

matches!(
c,
// Horizontal space characters
'\u{0009}' // tab (\t)
| '\u{0020}' // space
)
}

fn strip_newline(text: &str) -> &str {
text.strip_suffix("\r\n")
.or_else(|| text.strip_suffix('\n'))
.unwrap_or(text)
}

#[derive(Debug)]
pub struct FrontmatterError {
message: String,
Expand Down
Loading