From bb24c73a06683c116eba3ad17aebdc87a5302529 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 30 Nov 2021 17:44:17 +0100 Subject: spans: introduce PosTracker --- src/spans.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/spans.rs b/src/spans.rs index 663bd59..1fc17c1 100644 --- a/src/spans.rs +++ b/src/spans.rs @@ -4,7 +4,7 @@ use std::{ mem, }; -use crate::{Doctype, Emitter, EndTag, Error, StartTag, Token}; +use crate::{Doctype, Emitter, EndTag, Error, Reader, StartTag, Token}; type Span = std::ops::Range; @@ -12,6 +12,41 @@ pub trait GetPos { fn get_pos(&self) -> usize; } +struct PosTracker { + reader: R, + position: usize, +} + +impl GetPos for PosTracker { + fn get_pos(&self) -> usize { + self.position + } +} + +impl Reader for PosTracker { + type Error = R::Error; + + fn read_char(&mut self) -> Result, Self::Error> { + match self.reader.read_char()? { + Some(char) => { + self.position += char.len_utf8(); + Ok(Some(char)) + } + None => Ok(None), + } + } + + fn try_read_string(&mut self, s: &str, case_sensitive: bool) -> Result { + match self.reader.try_read_string(s, case_sensitive)? { + true => { + self.position += s.len(); + Ok(true) + } + false => Ok(false), + } + } +} + /// The default implementation of [`crate::Emitter`], used to produce ("emit") tokens. pub struct SpanEmitter { current_characters: String, -- cgit v1.2.3