Skip to content

serializer: Use match_byte in the serializer. #361

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2023
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
17 changes: 11 additions & 6 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::match_byte;
use dtoa_short::{self, Notation};
use itoa;
use std::fmt::{self, Write};
Expand Down Expand Up @@ -226,11 +227,15 @@ where
{
let mut chunk_start = 0;
for (i, b) in value.bytes().enumerate() {
let escaped = match b {
let escaped = match_byte! { b,
b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' | b'_' | b'-' => continue,
_ if !b.is_ascii() => continue,
b'\0' => Some("\u{FFFD}"),
_ => None,
b => {
if !b.is_ascii() {
continue;
}
None
},
};
dest.write_str(&value[chunk_start..i])?;
if let Some(escaped) = escaped {
Expand All @@ -251,7 +256,7 @@ where
{
let mut chunk_start = 0;
for (i, b) in value.bytes().enumerate() {
let hex = match b {
let hex = match_byte! { b,
b'\0'..=b' ' | b'\x7F' => true,
b'(' | b')' | b'"' | b'\'' | b'\\' => false,
_ => continue,
Expand Down Expand Up @@ -304,7 +309,7 @@ where
{
/// Wrap a text writer to create a `CssStringWriter`.
pub fn new(inner: &'a mut W) -> CssStringWriter<'a, W> {
CssStringWriter { inner: inner }
CssStringWriter { inner }
}
}

Expand All @@ -315,7 +320,7 @@ where
fn write_str(&mut self, s: &str) -> fmt::Result {
let mut chunk_start = 0;
for (i, b) in s.bytes().enumerate() {
let escaped = match b {
let escaped = match_byte! { b,
b'"' => Some("\\\""),
b'\\' => Some("\\\\"),
b'\0' => Some("\u{FFFD}"),
Expand Down