-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdsplit.js
More file actions
54 lines (46 loc) · 781 Bytes
/
cmdsplit.js
File metadata and controls
54 lines (46 loc) · 781 Bytes
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
51
52
53
54
module.exports = function(str, options) {
var ret = [];
var buffer = "";
var inString = false;
for (var i = 0; i < str.length; i++) {
var c = str[i];
// literals
if (c == "\\" && i + 1 < str.length) {
buffer += str[++i];
continue;
}
// strings
if (c == "\"") {
if (inString) {
// string ends
inString = false;
ret.push(buffer);
buffer = "";
if (i + 1 < str.length && str[i + 1] == " ") {
i++;
}
} else {
// string starts
inString = true;
}
continue;
}
// words
if (c == " ") {
if (inString) {
buffer += " ";
} else {
ret.push(buffer);
buffer = "";
}
continue;
}
// characters
buffer += c;
}
// last word
if (buffer != "") {
ret.push(buffer);
}
return ret;
};