-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathString_random.js
More file actions
278 lines (252 loc) · 6.48 KB
/
String_random.js
File metadata and controls
278 lines (252 loc) · 6.48 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function String_random (pattern, options) {
if (pattern instanceof RegExp) pattern = pattern.source;
const random = options && options.random || Math.random;
function processGrouping (pattern) {
const tree = [];
const stack = [ tree ];
let n = 1;
while (pattern.length) {
const chr = pattern.shift();
if (chr === '\\') {
const next = pattern.shift();
if (next === '(' || next === ')') {
stack[0].push(next);
} else {
stack[0].push(chr, next);
}
} else
if (chr === '(') {
const inner = [];
stack[0].push(inner);
stack.unshift(inner);
let next = pattern.shift(); // no warnings
if (next === '?') {
next = pattern.shift();
if (next === ':') {
// just create a group
} else {
throw Error("Invalid group");
}
} else
if (next === '(' || next === ')') {
pattern.unshift(next);
} else {
inner.n = n++;
inner.push(next);
}
} else
if (chr === ')') {
stack.shift();
} else {
stack[0].push(chr);
}
}
if (stack.length > 1) throw Error("mismatch paren");
return tree;
}
function processSelect (tree) {
const candidates = [ [] ];
while (tree.length) {
let chr = tree.shift();
if (chr === '\\') {
const next = tree.shift();
if (next === '|') {
candidates[0].push(next);
} else {
candidates[0].push(chr, next);
}
} else
if (chr === '[') {
candidates[0].push(chr);
while (tree.length) {
chr = tree.shift();
candidates[0].push(chr);
if (chr === '\\') {
const next = tree.shift(); // no warnings
candidates[0].push(next);
} else
if (chr === ']') {
break;
}
}
} else
if (chr === '|') {
candidates.unshift([]);
} else {
candidates[0].push(chr);
}
}
for (let i = 0, it; (it = candidates[i]); i++) {
tree.push(it);
for (let j = 0, len = it.length; j < len; j++) {
if (it[j] instanceof Array) {
processSelect(it[j]);
}
}
}
// 入れ子、 奇数段が pattern 偶数段が candidates,
return [ tree ];
}
const UPPERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
const LOWERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
const DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
const SPACES = [" ", "\n", "\t"];
const OTHERS = ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~"];
const ALL = [].concat(UPPERS, LOWERS, DIGITS, " ", OTHERS, ["_"]);
const CLASSES = {
'd' : DIGITS,
'D' : [].concat(UPPERS, LOWERS, SPACES, OTHERS, ['_']),
'w' : [].concat(UPPERS, LOWERS, DIGITS, ['_']),
'W' : [].concat(SPACES, OTHERS),
't' : [ '\t' ],
'n' : [ '\n' ],
'v' : [ '\u000B' ],
'f' : [ '\u000C' ],
'r' : [ '\r' ],
's' : SPACES,
'S' : [].concat(UPPERS, LOWERS, DIGITS, OTHERS, ['_']),
'0' : [ '\0' ]
};
const REFERENCE = {};
function processOthers (tree) {
let ret = '', candidates = [];
tree = tree.slice(0);
function choice () {
let ret = candidates[ Math.floor(candidates.length * random()) ];
if (ret instanceof Array) {
ret = processOthers(ret);
}
if (candidates.n) REFERENCE[candidates.n] = ret;
return ret || '';
}
while (tree.length) {
let chr = tree.shift();
switch (chr) {
case '^':
case '$':
// do nothing
break;
case '*':
for (let i = 0, len = random() * 10; i < len; i++) {
ret += choice();
}
candidates = [];
break;
case '+':
for (let i = 0, len = random() * 10 + 1; i < len; i++) {
ret += choice();
}
candidates = [];
break;
case '{':
let brace = '';
while (tree.length) {
chr = tree.shift();
if (chr === '}') {
break;
} else {
brace += chr;
}
}
if (chr !== '}') throw Error(`missmatch brace: ${chr}`);
const dd = brace.split(/,/);
const min = +dd[0];
const max = (dd.length === 1) ? min : (+dd[1] || 10);
for (let i = 0, len = Math.floor(random() * (max - min + 1)) + min; i < len; i++) {
ret += choice();
}
candidates = [];
break;
case '?':
if (random() < 0.5) {
ret += choice();
}
candidates = [];
break;
case '\\':
ret += choice();
const escaped = tree.shift();
if (escaped.match(/^[1-9]$/)) {
candidates = [ REFERENCE[escaped] || '' ];
} else {
if (escaped === 'b' || escaped === 'B') {
throw Error(`\\b and \\B is not supported`);
}
candidates = CLASSES[escaped];
}
if (!candidates) candidates = [ escaped ];
break;
case '[':
ret += choice();
let sets = [], negative = false;
while (tree.length) {
chr = tree.shift();
if (chr === '\\') {
const next = tree.shift();
if (CLASSES[next]) {
sets = sets.concat(CLASSES[next]);
} else {
sets.push(next);
}
} else
if (chr === ']') {
break;
} else
if (chr === '^') {
const before = sets[ sets.length - 1];
if (!before) {
negative = true;
} else {
sets.push(chr);
}
} else
if (chr === '-') {
const next = tree.shift(); // no warnings
if (next === ']') {
sets.push(chr);
chr = next;
break;
}
const before = sets[ sets.length - 1]; // no warnings
if (!before) {
sets.push(chr);
} else {
for (let i = before.charCodeAt(0) + 1, len = next.charCodeAt(0); i <= len; i++) {
sets.push(String.fromCharCode(i));
}
}
} else {
sets.push(chr);
}
}
if (chr !== ']') throw "missmatch bracket: " + chr;
if (negative) {
const neg = {};
for (let i = 0, len = sets.length; i < len; i++) {
neg[sets[i]] = true;
}
candidates = [];
for (let i = 0, len = ALL.length; i < len; i++) {
if (!neg[ALL[i]]) candidates.push(ALL[i]);
}
} else {
candidates = sets;
}
break;
case '.':
ret += choice();
candidates = ALL;
break;
default:
ret += choice();
candidates = chr;
}
}
return ret + choice();
}
let tree;
tree = processGrouping(pattern.split(''));
tree = processSelect(tree);
return processOthers(tree);
}
export { String_random };