Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Resolve "xs:list deserialization does not split on all whitespace" #843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions src/de/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::encoding::Decoder;
use crate::errors::serialize::DeError;
use crate::escape::unescape;
use crate::utils::CowRef;
use memchr::memchr;
use serde::de::value::UnitDeserializer;
use serde::de::{
DeserializeSeed, Deserializer, EnumAccess, IntoDeserializer, SeqAccess, VariantAccess, Visitor,
Expand Down Expand Up @@ -361,14 +360,17 @@ impl<'de, 'a> SeqAccess<'de> for ListIter<'de, 'a> {
T: DeserializeSeed<'de>,
{
if let Some(mut content) = self.content.take() {
const DELIMITER: u8 = b' ';
const DELIMETERS: [u8; 4] = [b' ', b'\t', b'\r', b'\n'];

loop {
let string = content.as_str();
if string.is_empty() {
return Ok(None);
}
return match memchr(DELIMITER, string.as_bytes()) {

let first_delimiter = string.as_bytes().iter().position(|c| DELIMETERS.contains(c));
Copy link
Contributor

Choose a reason for hiding this comment

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

str::find will accept an array of characters and give you the first matching position which is likely faster than iterating bytes.

Alternatively, two calls to memchr2 might be an alternative that might be a bit faster yet.


return match first_delimiter {
// No delimiters in the `content`, deserialize it as a whole atomic
None => match content {
Content::Input(s) => seed.deserialize(AtomicDeserializer {
Expand All @@ -391,7 +393,7 @@ impl<'de, 'a> SeqAccess<'de> for ListIter<'de, 'a> {
// `content` started with a space, skip them all
Some(0) => {
// Skip all spaces
let start = string.as_bytes().iter().position(|ch| *ch != DELIMITER);
let start = string.as_bytes().iter().position(|c| !DELIMETERS.contains(c));
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to the above, str::find also accepts a closure checking whether a character matches.

content = match (start, content) {
// We cannot find any non-space character, so string contains only spaces
(None, _) => return Ok(None),
Expand Down