Merge pull request #238 from Kijewski/pr-is_ascii_whitespace

parser: use built-in whitespace trimming
This commit is contained in:
Guillaume Gomez 2024-11-14 14:43:14 +01:00 committed by GitHub
commit 8dd3fc9628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 33 additions and 32 deletions

View File

@ -97,7 +97,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.71.1"
toolchain: "1.80.0"
- run: cargo check --lib -p rinja --all-features
Audit:

View File

@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "../README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
features = ["full"]

View File

@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
all-features = true

View File

@ -5,7 +5,7 @@ members = ["."]
name = "rinja_axum"
version = "0.3.5"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
description = "Axum integration for Rinja templates"
keywords = ["markup", "template", "jinja2", "html", "axum"]
categories = ["template-engine"]

View File

@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
all-features = true

View File

@ -10,7 +10,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
publish = false
[package.metadata.docs.rs]

View File

@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
all-features = true

View File

@ -8,11 +8,10 @@ use winnow::combinator::{
terminated,
};
use winnow::error::{ErrorKind, ParserError as _};
use winnow::token::{take_till0, take_till1};
use crate::{
CharLit, ErrorContext, Level, Num, ParseResult, PathOrIdentifier, StrLit, WithSpan, char_lit,
filter, identifier, keyword, not_ws, num_lit, path_or_identifier, str_lit, ws,
filter, identifier, keyword, num_lit, path_or_identifier, skip_ws0, skip_ws1, str_lit, ws,
};
macro_rules! expr_prec_layer {
@ -181,7 +180,7 @@ impl<'a> Expr<'a> {
fn concat(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
fn concat_expr(i: &str, level: Level) -> ParseResult<'_, Option<WithSpan<'_, Expr<'_>>>> {
let ws1 = |i| opt(take_till1(not_ws)).parse_next(i);
let ws1 = |i| opt(skip_ws1).parse_next(i);
let (j, data) = opt((ws1, '~', ws1, |i| Expr::muldivmod(i, level))).parse_next(i)?;
if let Some((t1, _, t2, expr)) = data {
if t1.is_none() || t2.is_none() {
@ -591,8 +590,6 @@ impl<'a> Suffix<'a> {
}
fn r#try(i: &'a str) -> ParseResult<'a, Self> {
preceded(take_till0(not_ws), '?')
.map(|_| Self::Try)
.parse_next(i)
preceded(skip_ws0, '?').map(|_| Self::Try).parse_next(i)
}
}

View File

@ -15,7 +15,7 @@ use winnow::ascii::escaped;
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, peek, preceded, repeat};
use winnow::error::{ErrorKind, FromExternalError};
use winnow::stream::AsChar;
use winnow::token::{any, one_of, take_till0, take_till1, take_while};
use winnow::token::{any, one_of, take_till1, take_while};
pub mod expr;
pub use expr::{Expr, Filter};
@ -274,18 +274,25 @@ impl<'a> From<ErrorContext<'a>> for winnow::error::ErrMode<ErrorContext<'a>> {
}
}
fn is_ws(c: char) -> bool {
matches!(c, ' ' | '\t' | '\r' | '\n')
#[inline]
fn skip_ws0(i: &str) -> ParseResult<'_, ()> {
Ok((i.trim_ascii_start(), ()))
}
fn not_ws(c: char) -> bool {
!is_ws(c)
#[inline]
fn skip_ws1(i: &str) -> ParseResult<'_, ()> {
let j = i.trim_ascii_start();
if i.len() != j.len() {
Ok((j, ()))
} else {
fail.parse_next(i)
}
}
fn ws<'a, O>(
inner: impl Parser<&'a str, O, ErrorContext<'a>>,
) -> impl Parser<&'a str, O, ErrorContext<'a>> {
delimited(take_till0(not_ws), inner, take_till0(not_ws))
delimited(skip_ws0, inner, skip_ws0)
}
/// Skips input until `end` was found, but does not consume it.
@ -892,7 +899,7 @@ fn filter<'a>(
i: &'a str,
level: &mut Level,
) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> {
let (j, _) = take_till0(not_ws).parse_next(i)?;
let (j, _) = skip_ws0.parse_next(i)?;
let had_spaces = i.len() != j.len();
let (j, _) = ('|', not('|')).parse_next(j)?;

View File

@ -5,12 +5,12 @@ use winnow::Parser;
use winnow::combinator::{
alt, cut_err, delimited, eof, fail, not, opt, peek, preceded, repeat, separated1, terminated,
};
use winnow::token::{any, tag, take_till0};
use winnow::token::{any, tag};
use crate::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{
ErrorContext, Expr, Filter, ParseResult, State, Target, WithSpan, filter, identifier, is_ws,
keyword, not_ws, skip_till, str_lit_without_prefix, ws,
ErrorContext, Expr, Filter, ParseResult, State, Target, WithSpan, filter, identifier, keyword,
skip_till, skip_ws0, str_lit_without_prefix, ws,
};
#[derive(Debug, PartialEq)]
@ -88,10 +88,7 @@ impl<'a> Node<'a> {
let start = i;
let (i, tag) = preceded(
|i| s.tag_block_start(i),
peek(preceded(
(opt(Whitespace::parse), take_till0(not_ws)),
identifier,
)),
peek(preceded((opt(Whitespace::parse), skip_ws0), identifier)),
)
.parse_next(i)?;
@ -1076,9 +1073,9 @@ impl<'a> Lit<'a> {
}
pub(crate) fn split_ws_parts(s: &'a str) -> Self {
let trimmed_start = s.trim_start_matches(is_ws);
let trimmed_start = s.trim_ascii_start();
let len_start = s.len() - trimmed_start.len();
let trimmed = trimmed_start.trim_end_matches(is_ws);
let trimmed = trimmed_start.trim_ascii_end();
Self {
lws: &s[..len_start],
val: trimmed,

View File

@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
all-features = true

View File

@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
[package.metadata.docs.rs]
all-features = true

View File

@ -4,7 +4,7 @@ version = "0.3.5"
authors = ["rinja-rs developers"]
workspace = ".."
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
publish = false
[features]