tokenize.js
1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* jshint globalstrict: true */
/* global require */
/* global module */
"use strict";
function tokenize(str) {
var l = str.length;
var i;
var curr, next;
var tokens = [];
var tok = [0, ""];
for (i = 0; i < l; i++) {
curr = str.substr(i,1);
if (curr === "*") {
while (str.substr(i+1, 1) === "*") {
// sequences of wildcards must be collapsed into one
i++;
}
tokens.push(tok); // save the previously built srting token
tokens.push([1]); //add a wildcard token
tok = [0, ""]; //prepare a new string token
} else if (curr === "\\") {
// this is an escape sequence, skip to the next character
i++;
tok[1] += str.substr(i, 1);
} else {
tok[1] += curr;
}
}
if (tok[0] === 1 || tok[0] === 0 && tok[1] !== "") {
/*
* Prevent an emoty string token at the end.
*/
tokens.push(tok);
}
if (str !== "" && tokens[0][0] === 0 && tokens[0][1] === "") {
/*
* remove the empty string token at the beginning,
* except if the whole pattern is an empty string.
*/
tokens.shift();
}
return tokens;
}
module.exports = tokenize;