From 6b4886857eb7ec3e1afa9c0aecb4d752f3f2d57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Fri, 9 Aug 2024 14:48:58 +0200 Subject: [PATCH] xso: offer `is_xml_whitespace` function --- xso-proc/src/compound.rs | 2 +- xso/src/lib.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/xso-proc/src/compound.rs b/xso-proc/src/compound.rs index 1ef6c842..adfe9890 100644 --- a/xso-proc/src/compound.rs +++ b/xso-proc/src/compound.rs @@ -279,7 +279,7 @@ impl Compound { None => quote! { // note: u8::is_ascii_whitespace includes U+000C, which is not // part of XML's white space definition.' - if #text.as_bytes().iter().any(|b| *b != b' ' && *b != b'\t' && *b != b'\r' && *b != b'\n') { + if !::xso::is_xml_whitespace(#text.as_bytes()) { ::core::result::Result::Err(::xso::error::Error::Other("Unexpected text content".into())) } else { ::core::result::Result::Ok(::core::ops::ControlFlow::Break( diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 9741b8d3..6bd1c04c 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -419,3 +419,13 @@ pub fn from_bytes(mut buf: &[u8]) -> Result { rxml::error::XmlError::InvalidEof("while parsing FromXml impl"), )) } + +/// Return true if the string contains exclusively XML whitespace. +/// +/// XML whitespace is defined as U+0020 (space), U+0009 (tab), U+000a +/// (newline) and U+000d (carriage return). +pub fn is_xml_whitespace>(s: T) -> bool { + s.as_ref() + .iter() + .all(|b| *b == b' ' || *b == b'\t' || *b == b'\r' || *b == b'\n') +}