Skip to content

Commit d254fc8

Browse files
committed
Implemented early return instead of indented if statement for deserialize_str
1 parent c505081 commit d254fc8

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

Diff for: src/de/mod.rs

+27-29
Original file line numberDiff line numberDiff line change
@@ -495,42 +495,40 @@ impl<'a, 'de, 's> de::Deserializer<'de> for &'a mut Deserializer<'de, 's> {
495495
{
496496
let escaped_string = self.parse_str()?;
497497

498-
if let Some(string_unescape_buffer) = self.string_unescape_buffer.as_deref_mut() {
499-
if escaped_string.as_bytes().contains(&b'\\') {
500-
let mut string_unescape_buffer_write_position = 0;
498+
// If the unescape buffer is not provided, skip unescaping strings
499+
let Some(string_unescape_buffer) = self.string_unescape_buffer.as_deref_mut() else {
500+
return visitor.visit_borrowed_str(escaped_string);
501+
};
501502

502-
for fragment in crate::str::EscapedStr(escaped_string).fragments() {
503-
let char_encode_buffer = &mut [0; 4];
503+
// If the escaped string doesn't contain '\\', it' can't have any escaped characters
504+
if !escaped_string.as_bytes().contains(&b'\\') {
505+
return visitor.visit_borrowed_str(escaped_string);
506+
}
504507

505-
let unescaped_bytes = match fragment? {
506-
crate::str::EscapedStringFragment::NotEscaped(fragment) => {
507-
fragment.as_bytes()
508-
}
509-
crate::str::EscapedStringFragment::Escaped(c) => {
510-
c.encode_utf8(char_encode_buffer).as_bytes()
511-
}
512-
};
508+
let mut string_unescape_buffer_write_position = 0;
513509

514-
string_unescape_buffer[string_unescape_buffer_write_position..]
515-
.get_mut(..unescaped_bytes.len())
516-
.ok_or(Error::EscapedStringIsTooLong)?
517-
.copy_from_slice(unescaped_bytes);
510+
for fragment in crate::str::EscapedStr(escaped_string).fragments() {
511+
let char_encode_buffer = &mut [0; 4];
518512

519-
string_unescape_buffer_write_position += unescaped_bytes.len();
513+
let unescaped_bytes = match fragment? {
514+
crate::str::EscapedStringFragment::NotEscaped(fragment) => fragment.as_bytes(),
515+
crate::str::EscapedStringFragment::Escaped(c) => {
516+
c.encode_utf8(char_encode_buffer).as_bytes()
520517
}
518+
};
521519

522-
visitor.visit_str(
523-
str::from_utf8(
524-
&string_unescape_buffer[..string_unescape_buffer_write_position],
525-
)
526-
.map_err(|_| Error::InvalidUnicodeCodePoint)?,
527-
)
528-
} else {
529-
visitor.visit_borrowed_str(escaped_string)
530-
}
531-
} else {
532-
visitor.visit_borrowed_str(escaped_string)
520+
string_unescape_buffer[string_unescape_buffer_write_position..]
521+
.get_mut(..unescaped_bytes.len())
522+
.ok_or(Error::EscapedStringIsTooLong)?
523+
.copy_from_slice(unescaped_bytes);
524+
525+
string_unescape_buffer_write_position += unescaped_bytes.len();
533526
}
527+
528+
visitor.visit_str(
529+
str::from_utf8(&string_unescape_buffer[..string_unescape_buffer_write_position])
530+
.map_err(|_| Error::InvalidUnicodeCodePoint)?,
531+
)
534532
}
535533

536534
/// Unsupported. String is not available in no-std.

0 commit comments

Comments
 (0)