Ref: https://wicg.github.io/urlpattern/#tokenize
( input: &str, policy: TokenizePolicy, )
| 124 | |
| 125 | // Ref: https://wicg.github.io/urlpattern/#tokenize |
| 126 | pub fn tokenize( |
| 127 | input: &str, |
| 128 | policy: TokenizePolicy, |
| 129 | ) -> Result<Vec<Token>, Error> { |
| 130 | let mut tokenizer = Tokenizer { |
| 131 | input, |
| 132 | policy, |
| 133 | token_list: vec![], |
| 134 | index: 0, |
| 135 | next_index: 0, |
| 136 | code_point: None, |
| 137 | }; |
| 138 | |
| 139 | while tokenizer.index < tokenizer.input.len() { |
| 140 | tokenizer.seek_and_get_next_codepoint(tokenizer.index); |
| 141 | |
| 142 | if tokenizer.code_point == Some('*') { |
| 143 | tokenizer.add_token_with_default_pos_and_len(TokenType::Asterisk); |
| 144 | continue; |
| 145 | } |
| 146 | if matches!(tokenizer.code_point, Some('+') | Some('?')) { |
| 147 | tokenizer.add_token_with_default_pos_and_len(TokenType::OtherModifier); |
| 148 | continue; |
| 149 | } |
| 150 | if tokenizer.code_point == Some('\\') { |
| 151 | if tokenizer.index == (tokenizer.input.len() - 1) { |
| 152 | tokenizer.process_tokenizing_error( |
| 153 | tokenizer.next_index, |
| 154 | tokenizer.index, |
| 155 | TokenizerError::IncompleteEscapeCode, |
| 156 | )?; |
| 157 | continue; |
| 158 | } |
| 159 | let escaped_index = tokenizer.next_index; |
| 160 | tokenizer.get_next_codepoint(); |
| 161 | tokenizer.add_token_with_default_len( |
| 162 | TokenType::EscapedChar, |
| 163 | tokenizer.next_index, |
| 164 | escaped_index, |
| 165 | ); |
| 166 | continue; |
| 167 | } |
| 168 | if tokenizer.code_point == Some('\n') |
| 169 | || tokenizer.code_point == Some('\r') |
| 170 | || tokenizer.code_point == Some('\t') |
| 171 | { |
| 172 | // ignore newline, carriage return and tab |
| 173 | tokenizer.index = tokenizer.next_index; |
| 174 | continue; |
| 175 | } |
| 176 | if tokenizer.code_point == Some('{') { |
| 177 | tokenizer.add_token_with_default_pos_and_len(TokenType::Open); |
| 178 | continue; |
| 179 | } |
| 180 | if tokenizer.code_point == Some('}') { |
| 181 | tokenizer.add_token_with_default_pos_and_len(TokenType::Close); |
| 182 | continue; |
| 183 | } |
no test coverage detected