- 
                Notifications
    
You must be signed in to change notification settings  - Fork 266
 
New parser implementation #690
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
          
     Draft
      
      
            Mingun
  wants to merge
  22
  commits into
  tafia:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Mingun:new-parser
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            22 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ba035ac
              
                Implement DTD parsing in a new crate quick-dtd
              
              
                Mingun f3ccb26
              
                Implement new XML parser
              
              
                Mingun 327d46e
              
                Add support for encoding detection to a parser
              
              
                Mingun 8456be3
              
                Remove dependency from XmlSource from NsReader
              
              
                Mingun 9b66833
              
                Remove tests of internals that would be removed soon
              
              
                Mingun e53ce41
              
                Use new parser for async reads
              
              
                Mingun 60aa437
              
                Run `cargo fmt`
              
              
                Mingun d23c1d7
              
                Use new parser for buffered reader
              
              
                Mingun bf58185
              
                Use new parser for borrowed reader
              
              
                Mingun f063917
              
                Cleanup unused code
              
              
                Mingun 40c1cfc
              
                Inline ReaderState::close_expanded_empty
              
              
                Mingun 584406e
              
                Inline ReaderState::emit_bang with obvious dead code elimination (rem…
              
              
                Mingun 698f88c
              
                Remove BangType (review with with whitespace ignored mode)
              
              
                Mingun 9f117a3
              
                Remove dead code - these checks already performed by a parser
              
              
                Mingun d71f3f5
              
                Remove intermediate slices in Doctype and comment checks
              
              
                Mingun adccd21
              
                Simplify check in Doctype and start search of a name after first space
              
              
                Mingun 0302010
              
                Inline ReaderState::emit_question_mark
              
              
                Mingun d5d3a99
              
                Remove dead code - these checks already performed by a parser
              
              
                Mingun 8771d65
              
                Inline ReaderState::emit_start with obvious dead code elimination
              
              
                Mingun a199202
              
                Move name length calculation to BytesStart::wrap
              
              
                Mingun 45abd39
              
                Inline ReaderState::emit_end
              
              
                Mingun 273c8a0
              
                dbg! in xml
              
              
                Mingun File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [package] | ||
| name = "quick-dtd" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| 
     | 
||
| description = "High performance DTD reader for quick-xml" | ||
| 
     | 
||
| documentation = "https://docs.rs/quick-dtd" | ||
| repository = "https://github.com/tafia/quick-xml" | ||
| 
     | 
||
| keywords = ["dtd", "parser", "xml"] | ||
| categories = ["parsing", "parser-implementations", "no-std"] | ||
| license = "MIT" | ||
| rust-version = "1.56" | ||
| include = ["src/*", "LICENSE-MIT.md", "README.md"] | ||
| 
     | 
||
| [dependencies] | ||
| document-features = { version = "0.2", optional = true } | ||
| 
     | 
||
| [dev-dependencies] | ||
| pretty_assertions = "1.4" | ||
| 
     | 
||
| [features] | ||
| default = ["std"] | ||
| 
     | 
||
| ## Enables support of Rust standard library | ||
| std = [] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| The MIT License (MIT) | ||
| 
     | 
||
| Copyright (c) 2023 Mingun | ||
| 
     | 
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| 
     | 
||
| 
     | 
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| 
     | 
||
| 
     | 
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| //! Contains a parser for an XML comment. | ||
| 
     | 
||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| enum State { | ||
| /// The parser does not yet seen any dashes at the end of previous slice. | ||
| Seen0, | ||
| /// The parser already seen one dash on the end of previous slice. | ||
| Seen1, | ||
| /// The parser already seen two dashes on the end of previous slice. | ||
| Seen2, | ||
| } | ||
| 
     | 
||
| impl Default for State { | ||
| fn default() -> Self { | ||
| Self::Seen0 | ||
| } | ||
| } | ||
| 
     | 
||
| /// A parser that search a `-->` sequence in the slice. | ||
| /// | ||
| /// To use a parser create an instance of parser and [`feed`] data into it. | ||
| /// After successful search the parser will return [`Some`] with position where | ||
| /// comment is ended (the position after `-->`). If search was unsuccessful, | ||
| /// a [`None`] will be returned. You typically would expect positive result of | ||
| /// search, so that you should feed new data until yo'll get it. | ||
| /// | ||
| /// NOTE: after successful match the parser does not returned to the initial | ||
| /// state and should not be used anymore. Create a new parser if you want to perform | ||
| /// new search. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # use quick_dtd::CommentParser; | ||
| /// # use pretty_assertions::assert_eq; | ||
| /// let mut parser = CommentParser::default(); | ||
| /// | ||
| /// // Parse `<my-element with = 'some > inside'>and the text follow...` | ||
| /// // splitted into three chunks | ||
| /// assert_eq!(parser.feed(b"<!-- comment"), None); | ||
| /// // ...get new chunk of data | ||
| /// assert_eq!(parser.feed(b" with some -> and -"), None); | ||
| /// // ...get another chunk of data | ||
| /// assert_eq!(parser.feed(b"-- inside-->and the text follow..."), Some(12)); | ||
| /// // ^ ^ | ||
| /// // 0 11 | ||
| /// ``` | ||
| /// | ||
| /// [`feed`]: Self::feed() | ||
| #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] | ||
| pub struct CommentParser(State); | ||
| 
     | 
||
| impl CommentParser { | ||
| /// Determines the end position of an XML comment in the provided slice. | ||
| /// Comments is a pieces of text enclosed in `<!--` and `-->` braces. | ||
| /// Comment ends on the first occurrence of `-->` which cannot be escaped. | ||
| /// | ||
| /// # Parameters | ||
| /// - `bytes`: a slice to search end of comment. Should contain text in | ||
| /// ASCII-compatible encoding | ||
| pub fn feed(&mut self, bytes: &[u8]) -> Option<usize> { | ||
| let mut it = bytes.iter().enumerate(); | ||
| while let Some((i, _)) = it.find(|(_, &b)| b == b'>') { | ||
| // --|> | ||
| if i == 0 && self.0 == State::Seen2 { | ||
| // +1 for `>` which should be included in event | ||
| return Some(1); | ||
| } | ||
| // x-|-> | ||
| // --|-> | ||
| if i == 1 && bytes[0] == b'-' && matches!(self.0, State::Seen1 | State::Seen2) { | ||
| // +1 for `>` which should be included in event | ||
| return Some(2); | ||
| } | ||
| if bytes[..i].ends_with(b"--") { | ||
| // +1 for `>` which should be included in event | ||
| return Some(i + 1); | ||
| } | ||
| } | ||
| if bytes.ends_with(b"--") { | ||
| self.0 = State::Seen2; | ||
| } else { | ||
| self.next_state(bytes.last().copied()); | ||
| } | ||
| None | ||
| } | ||
| 
     | 
||
| #[inline] | ||
| fn next_state(&mut self, last: Option<u8>) { | ||
| match (self.0, last) { | ||
| (State::Seen0, Some(b'-')) => self.0 = State::Seen1, | ||
| 
     | 
||
| (State::Seen1, Some(b'-')) => self.0 = State::Seen2, | ||
| (State::Seen1, Some(_)) => self.0 = State::Seen0, | ||
| 
     | 
||
| (State::Seen2, Some(b'-')) => {} | ||
| (State::Seen2, Some(_)) => self.0 = State::Seen0, | ||
| 
     | 
||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| #[test] | ||
| fn test() { | ||
| use pretty_assertions::assert_eq; | ||
| use State::*; | ||
| 
     | 
||
| fn parse_comment(bytes: &[u8], initial: State) -> Result<usize, State> { | ||
| let mut parser = CommentParser(initial); | ||
| match parser.feed(bytes) { | ||
| Some(i) => Ok(i), | ||
| None => Err(parser.0), | ||
| } | ||
| } | ||
| 
     | 
||
| assert_eq!(parse_comment(b"", Seen0), Err(Seen0)); // xx| | ||
| assert_eq!(parse_comment(b"", Seen1), Err(Seen1)); // x-| | ||
| assert_eq!(parse_comment(b"", Seen2), Err(Seen2)); // --| | ||
| 
     | 
||
| assert_eq!(parse_comment(b"-", Seen0), Err(Seen1)); // xx|- | ||
| assert_eq!(parse_comment(b"-", Seen1), Err(Seen2)); // x-|- | ||
| assert_eq!(parse_comment(b"-", Seen2), Err(Seen2)); // --|- | ||
| 
     | 
||
| assert_eq!(parse_comment(b">", Seen0), Err(Seen0)); // xx|> | ||
| assert_eq!(parse_comment(b">", Seen1), Err(Seen0)); // x-|> | ||
| assert_eq!(parse_comment(b">", Seen2), Ok(1)); // --|> | ||
| 
     | 
||
| assert_eq!(parse_comment(b"--", Seen0), Err(Seen2)); // xx|-- | ||
| assert_eq!(parse_comment(b"--", Seen1), Err(Seen2)); // x-|-- | ||
| assert_eq!(parse_comment(b"--", Seen2), Err(Seen2)); // --|-- | ||
| 
     | 
||
| assert_eq!(parse_comment(b"->", Seen0), Err(Seen0)); // xx|-> | ||
| assert_eq!(parse_comment(b"->", Seen1), Ok(2)); // x-|-> | ||
| assert_eq!(parse_comment(b"->", Seen2), Ok(2)); // --|-> | ||
| 
     | 
||
| assert_eq!(parse_comment(b"-->", Seen0), Ok(3)); // xx|--> | ||
| assert_eq!(parse_comment(b"-->", Seen1), Ok(3)); // x-|--> | ||
| assert_eq!(parse_comment(b"-->", Seen2), Ok(3)); // --|--> | ||
| 
     | 
||
| assert_eq!(parse_comment(b">-->", Seen0), Ok(4)); // xx|>--> | ||
| assert_eq!(parse_comment(b">-->", Seen1), Ok(4)); // x-|>--> | ||
| assert_eq!(parse_comment(b">-->", Seen2), Ok(1)); // --|>--> | ||
| 
     | 
||
| assert_eq!(parse_comment(b"->-->", Seen0), Ok(5)); // xx|->--> | ||
| assert_eq!(parse_comment(b"->-->", Seen1), Ok(2)); // x-|->--> | ||
| assert_eq!(parse_comment(b"->-->", Seen2), Ok(2)); // --|->--> | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.