Refactoring

This commit is contained in:
Wilson Lin 2020-07-30 19:51:43 +10:00
parent 6a56bcc89f
commit d024d21274
5 changed files with 27 additions and 27 deletions

View File

@ -2,11 +2,11 @@ use crate::proc::Processor;
use crate::proc::range::ProcessorRange;
#[derive(Copy, Clone)]
pub struct Checkpoint {
pub struct WriteCheckpoint {
write_next: usize,
}
impl Checkpoint {
impl WriteCheckpoint {
#[inline(always)]
pub fn get_written_range_since(&self, amount: usize) -> ProcessorRange {
ProcessorRange {
@ -16,8 +16,8 @@ impl Checkpoint {
}
#[inline(always)]
pub fn new(proc: &Processor) -> Checkpoint {
Checkpoint {
pub fn new(proc: &Processor) -> WriteCheckpoint {
WriteCheckpoint {
write_next: proc.write_next,
}
}

View File

@ -1,5 +1,5 @@
use crate::err::ProcessingResult;
use crate::proc::checkpoint::Checkpoint;
use crate::proc::checkpoint::WriteCheckpoint;
use crate::proc::MatchAction::*;
use crate::proc::MatchMode::*;
use crate::proc::Processor;
@ -31,7 +31,7 @@ pub fn process_attr(proc: &mut Processor, ns: Namespace, element: ProcessorRange
proc.make_lowercase(name);
let attr_cfg = ATTRS.get(ns, &proc[element], &proc[name]);
let is_boolean = attr_cfg.filter(|attr| attr.boolean).is_some();
let after_name = Checkpoint::new(proc);
let after_name = WriteCheckpoint::new(proc);
let should_collapse_and_trim_value_ws = attr_cfg.filter(|attr| attr.collapse_and_trim).is_some();
proc.m(WhileInLookup(WHITESPACE), Discard);

View File

@ -1,7 +1,7 @@
use lazy_static::lazy_static;
use std::collections::HashMap;
use crate::err::ProcessingResult;
use crate::proc::checkpoint::Checkpoint;
use crate::proc::checkpoint::WriteCheckpoint;
use crate::proc::MatchAction::*;
use crate::proc::MatchMode::*;
use crate::proc::Processor;
@ -189,7 +189,7 @@ fn handle_whitespace_char_type(c: u8, proc: &mut Processor, metrics: &mut Metric
// The resulting written value would have the minimum possible value length.
// Since the actual processed value would have a length equal or greater to it (e.g. it might be quoted, or some characters might get encoded), we can then read minimum value right to left and start writing from actual processed value length (which is calculated), quoting/encoding as necessary.
pub fn process_attr_value(proc: &mut Processor, should_collapse_and_trim_ws: bool) -> ProcessingResult<ProcessedAttrValue> {
let start = Checkpoint::new(proc);
let start = WriteCheckpoint::new(proc);
let src_delimiter = proc.m(IsInLookup(ATTR_QUOTE), Discard).first(proc);
let delim_lookup = match src_delimiter {
Some(b'"') => DOUBLE_QUOTE,

View File

@ -10,7 +10,7 @@ use {
std::sync::Arc,
esbuild_rs::{TransformOptionsBuilder, TransformOptions},
crate::proc::JsMinSection,
crate::proc::checkpoint::Checkpoint,
crate::proc::checkpoint::WriteCheckpoint,
};
#[cfg(feature = "js-esbuild")]
@ -31,7 +31,7 @@ lazy_static! {
#[inline(always)]
pub fn process_script(proc: &mut Processor, cfg: &Cfg, js: bool) -> ProcessingResult<()> {
#[cfg(feature = "js-esbuild")]
let start = Checkpoint::new(proc);
let start = WriteCheckpoint::new(proc);
proc.require_not_at_end()?;
proc.m(WhileNotSeq(&SCRIPT_END), Keep);
// `process_tag` will require closing tag.

View File

@ -1,7 +1,7 @@
use lazy_static::lazy_static;
use std::collections::HashSet;
use crate::err::{ErrorType, ProcessingResult};
use crate::proc::checkpoint::{Checkpoint, ReadCheckpoint};
use crate::proc::checkpoint::{WriteCheckpoint, ReadCheckpoint};
use crate::proc::MatchAction::*;
use crate::proc::MatchMode::*;
use crate::proc::Processor;
@ -130,7 +130,7 @@ pub fn process_tag(proc: &mut Processor, cfg: &Cfg, ns: Namespace, parent: Optio
}
// Mark attribute start in case we want to erase it completely.
let attr_checkpoint = Checkpoint::new(proc);
let attr_checkpoint = WriteCheckpoint::new(proc);
let mut erase_attr = false;
// Write space after tag name or unquoted/valueless attribute.
@ -213,25 +213,25 @@ pub fn process_tag(proc: &mut Processor, cfg: &Cfg, ns: Namespace, parent: Optio
return Ok(MaybeClosingTag(None));
};
// Require closing tag for non-void.
let closing_tag_checkpoint = ReadCheckpoint::new(proc);
proc.m(IsSeq(b"</"), Discard).require("closing tag")?;
let closing_tag = proc.m(WhileInLookup(TAG_NAME_CHAR), Discard).require("closing tag name")?;
proc.make_lowercase(closing_tag);
if parent.filter(|p| proc[*p] == proc[closing_tag]).is_some() && can_omit_closing_tag {
closing_tag_checkpoint.restore(proc);
return Ok(MaybeClosingTag(None));
};
// We need to check closing tag matches as otherwise when we later write closing tag, it might be longer than source closing tag and cause source to be overwritten.
if !proc[closing_tag].eq(&proc[tag_name]) {
return Err(ErrorType::ClosingTagMismatch {
expected: unsafe { String::from_utf8_unchecked(proc[tag_name].to_vec()) },
got: unsafe { String::from_utf8_unchecked(proc[closing_tag].to_vec()) },
});
};
proc.m(WhileInLookup(WHITESPACE), Discard);
proc.m(IsChar(b'>'), Discard).require("closing tag end")?;
Ok(MaybeClosingTag(Some(tag_name)))
if proc[closing_tag] != proc[tag_name] {
if can_omit_closing_tag {
closing_tag_checkpoint.restore(proc);
Ok(MaybeClosingTag(None))
} else {
Err(ErrorType::ClosingTagMismatch {
expected: unsafe { String::from_utf8_unchecked(proc[tag_name].to_vec()) },
got: unsafe { String::from_utf8_unchecked(proc[closing_tag].to_vec()) },
})
}
} else {
proc.m(WhileInLookup(WHITESPACE), Discard);
proc.m(IsChar(b'>'), Discard).require("closing tag end")?;
Ok(MaybeClosingTag(Some(tag_name)))
}
}