-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathformats.js
565 lines (463 loc) · 14.3 KB
/
formats.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
"use strict";
var
/** @const */
MIN_BUFFER_SIZE = 0x100,
/**
* A
* @const
*/
MAX_BUFFER_SIZE = 0x1000000,
/**
* An estimated guess for the density of a Life pattern, in alive/cell
* @const
*/
DENSITY_ESTIMATE = .009;
var formats = (function()
{
return {
parse_rle: parse_rle,
//parse_life105: parse_life105,
//parse_life106: parse_life106,
//parse_plaintext: parse_plaintext,
parse_pattern: parse_pattern,
rule2str: rule2str,
parse_rule: parse_rule,
parse_comments: parse_comments,
generate_rle: generate_rle,
};
function parse_rle(pattern_string)
{
var
result = parse_comments(pattern_string, "#"),
x = 0, y = 0,
header_match,
expr = /([a-zA-Z]+) *= *([a-zA-Z0-9\/()]+)/g,
match;
pattern_string = result.pattern_string;
var pos = pattern_string.indexOf("\n");
if(pos === -1)
{
return { error : "RLE Syntax Error: No Header" };
}
while(header_match = expr.exec(pattern_string.substr(0, pos)))
{
switch(header_match[1])
{
case "x":
result.width = Number(header_match[2]);
break;
case "y":
result.height = Number(header_match[2]);
break;
case "rule":
result.rule_s = parse_rule_rle(header_match[2], true);
result.rule_b = parse_rule_rle(header_match[2], false);
result.comment += "\nRule: " + rule2str(result.rule_s, result.rule_b) + "\n";
result.rule = rule2str(result.rule_s, result.rule_b);
break;
case "alpha":
case "color":
break;
default:
//console.log(header_match);
return { error : "RLE Syntax Error: Invalid Header: " + header_match[1] };
}
}
/*
Today I learned:
Optimize a certain string parsing algorithm (1 Megabyte input).
Timings:
1. using while(str = str.replace(re, callback)): Over 9000
2. using while(match = re.exec(str)): 800ms
3. using for(var pos = 0; pos < str.length; pos++) chr = str[pos]: 750ms
4. using for(var pos = 0; pos < str.length; pos++) chr = str.charCodeAt(pos): 50ms
=> re.exec is way faster than str.replace (obviously)
=> parsing character by character with str comparisons is same speed as re.exec
=> number comparisons are way faster than single character comparisons
*/
//console.time("parse rle");
var initial_size = MIN_BUFFER_SIZE;
if(result.width && result.height)
{
var size = result.width * result.height;
if(size > 0)
{
initial_size = Math.max(initial_size, size * DENSITY_ESTIMATE | 0);
initial_size = Math.min(MAX_BUFFER_SIZE, initial_size);
}
}
var count = 1,
in_number = false,
chr,
field_x = new Int32Array(initial_size),
field_y = new Int32Array(initial_size),
alive_count = 0,
len = pattern_string.length;
for(; pos < len; pos++)
{
chr = pattern_string.charCodeAt(pos);
if(chr >= 48 && chr <= 57)
{
if(in_number)
{
count *= 10;
count += chr ^ 48;
}
else
{
count = chr ^ 48;
in_number = true;
}
}
else
{
if(chr === 98) // b
{
x += count;
}
else if(chr >= 65 && chr <= 90 || chr >= 97 && chr < 122) // A-Za-z
{
if(alive_count + count > field_x.length)
{
field_x = increase_buf_size(field_x);
field_y = increase_buf_size(field_y);
}
while(count--)
{
field_x[alive_count] = x++;
field_y[alive_count] = y;
alive_count++;
}
}
else if(chr === 36) // $
{
y += count;
x = 0;
}
else if(chr === 33) // !
{
break;
}
count = 1;
in_number = false;
}
}
//console.timeEnd("parse rle");
//console.log(initial_size, alive_count);
result.field_x = new Int32Array(field_x.buffer, 0, alive_count);
result.field_y = new Int32Array(field_y.buffer, 0, alive_count);
return result;
}
function increase_buf_size(buffer)
{
var new_buffer = new Int32Array(buffer.length * 1.5 | 0);
new_buffer.set(buffer);
return new_buffer;
}
function parse_life105(pattern_string)
{
var result = parse_comments(pattern_string, "#");
// defunctional now
// parsing this is similiar to plaintext
return result;
}
function parse_life106(pattern_string)
{
// a list of coordinates essentially
var expr = /\s*(-?\d+)\s+(-?\d+)\s*(?:\n|$)/g,
match,
field_x = [],
field_y = [];
while(match = expr.exec(pattern_string))
{
field_x.push(Number(match[1]));
field_y.push(Number(match[2]));
}
return {
field_x: field_x,
field_y: field_y,
};
}
function parse_plaintext(pattern_string)
{
var result = parse_comments(pattern_string, "!");
pattern_string = result.pattern_string;
var field_x = [],
field_y = [],
x = 0,
y = 0,
len = pattern_string.length;
for(var i = 0; i < len; i++)
{
switch(pattern_string[i])
{
case ".":
x++;
break;
case "O":
field_x.push(x++);
field_y.push(y);
break;
case "\n":
y++;
x = 0;
break;
case "\r":
case " ":
break;
/*case "":
return result;
break;*/
default:
//console.log("Plaintext: Syntax Error");
return { error : "Plaintext: Syntax Error" };
}
}
result.field_x = field_x;
result.field_y = field_y;
return result;
}
function parse_comments(pattern_string, comment_char)
{
var result = {
comment: "",
urls: [],
short_comment: "",
},
nl,
line,
cont,
advanced = comment_char === "#";
while(pattern_string[0] === comment_char)
{
nl = pattern_string.indexOf("\n");
line = pattern_string.substr(1, nl - 1);
cont = true;
if(advanced)
{
line = line.substr(1).trim();
switch(pattern_string[1])
{
case "N":
if(line)
{
result.title = line;
}
else
{
result.rule = "23/3";
}
cont = false;
break;
case "C":
case "D":
if(!result.short_comment)
{
result.short_comment = line;
}
break;
case "O":
result.author = line;
break;
case "R":
result.rule = line;
cont = false;
break;
//case "P":
// center of the pattern
// cont = false;
// break;
default:
cont = false;
}
}
if(cont)
{
if(/^(?:https?:\/\/|www\.)[a-z0-9]/i.test(line))
{
if(line.substr(0, 4) !== "http")
{
line = "http://" + line;
}
result.urls.push(line);
}
else if(line.substr(0, 5) === "Name:")
{
result.title = line.substr(5);
}
else
{
result.comment += line;
if(nl !== 70 && nl !== 80)
{
result.comment += "\n";
}
}
}
pattern_string = pattern_string.substr(nl + 1);
}
result.pattern_string = pattern_string;
result.comment = result.comment.trim();
return result;
}
function parse_pattern(pattern_text)
{
pattern_text = pattern_text.replace(/\r/g, "");
if(pattern_text[0] === "!")
{
return parse_plaintext(pattern_text);
}
else if(/^(?:#[^\n]*\n)*\n*(?:(?:x|y|rule|color|alpha) *= *[a-z0-9\/(),]+,? *)+\s*\n/i.test(pattern_text))
{
return parse_rle(pattern_text);
}
// defunctional
//else if(pattern_text.substr(0, 10) === "#Life 1.05")
//{
// importer = parse_life105;
//}
else if(pattern_text.substr(0, 10) === "#Life 1.06")
{
return parse_life106(pattern_text);
}
else
{
return { error: "Format detection failed." };
}
}
function rule2str(rule_s, rule_b)
{
var rule = "";
for(var i = 0; rule_s; rule_s >>= 1, i++)
{
if(rule_s & 1)
{
rule += i;
}
}
rule += "/";
for(var i = 0; rule_b; rule_b >>= 1, i++)
{
if(rule_b & 1)
{
rule += i;
}
}
return rule;
}
function rule2str_rle(rule_s, rule_b)
{
let rule = formats.rule2str(rule_s, rule_b);
rule = rule.split("/");
rule = `B${rule[1]}/S${rule[0]}`;
return rule;
}
function parse_rule_rle(rule_str, survived)
{
rule_str = rule_str.split("/");
if(!rule_str[1])
{
return false;
}
if(Number(rule_str[0]))
{
return parse_rule(rule_str.join("/"), survived);
}
if(rule_str[0][0].toLowerCase() === "b")
{
rule_str.reverse();
}
return parse_rule(rule_str[0].substr(1) + "/" + rule_str[1].substr(1), survived);
}
function parse_rule(rule_str, survived)
{
var rule = 0,
parsed = rule_str.split("/")[survived ? 0 : 1];
for(var i = 0; i < parsed.length; i++)
{
var n = Number(parsed[i]);
if(isNaN(n) || rule & 1 << n)
{
return false;
}
rule |= 1 << n;
}
return rule;
}
function* rle_generator(life, bounds)
{
function make(length, is_empty)
{
console.assert(length >= 0);
if(length === 0)
{
return "";
}
let length_tag = "";
if(length > 1)
{
length_tag = String(length);
}
return length_tag + (is_empty ? "b" : "o");
}
for(let y = bounds.top; y <= bounds.bottom; y++)
{
let state_is_empty = true;
let run_start = bounds.left;
for(let x = bounds.left; x <= bounds.right; x++)
{
const is_empty = !life.get_bit(x, y);
const run_length = x - run_start;
console.assert(run_length >= 0);
if(state_is_empty !== is_empty)
{
yield make(run_length, state_is_empty);
run_start = x;
state_is_empty = is_empty;
}
}
if(!state_is_empty)
{
const run_length = bounds.right + 1 - run_start;
yield make(run_length, state_is_empty);
}
if(y !== bounds.bottom)
{
yield "$";
}
}
yield "!";
}
// implemented according to http://www.conwaylife.com/w/index.php?title=Run_Length_Encoded
function generate_rle(life, name, comments)
{
const lines = [];
const MAX_LINE_LENGTH = 70;
if(name)
{
lines.push("#N " + name);
}
lines.push.apply(lines, comments.map(c => "#C " + c));
const root = life.root;
const bounds = life.get_root_bounds();
{
const width = bounds.right - bounds.left + 1;
const height = bounds.bottom - bounds.top + 1;
const rule = rule2str_rle(life.rule_s, life.rule_b);
lines.push(`x = ${width}, y = ${height}, rule = ${rule}`);
}
let current_line = "";
for(let fragment of rle_generator(life, bounds))
{
if(current_line.length + fragment.length > MAX_LINE_LENGTH)
{
console.assert(fragment.length < MAX_LINE_LENGTH);
lines.push(current_line);
current_line = "";
}
current_line += fragment;
}
lines.push(current_line);
return lines.join("\n");
}
})();