-
Notifications
You must be signed in to change notification settings - Fork 2
/
PureFunctions.js
2257 lines (2123 loc) · 79.1 KB
/
PureFunctions.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Welcome!
//Here you can find pure functions, where functions don't interact with the world. But they may change the argument. Or use other functions, however I'm sure these functions will not use the DOM
//Beware, the comments are longer than the code.
//>inb4 reinventing the wheel
//Oh also...
//I lost this file and got it back corrupt, I had to re edit, copy paste, delete entire blocks of code that came out of nowhere, it seems it's working fine, but it may be very bug prone
//blame c9.io at least there is a revision history, god dammit
function UnicodeToUTF8Array(UnicodeValue) {//Only 1 unicode value!!!!!! //Char Number Range as called from the RFC
//Converts Unicode number or character.toCharCodeAt() to an "UTF8 array"
//After this has been done, converting to UTF-8 is just trivial, just call UTF8ArrayToUTF8
//JavaScript cannot "convert" strings to UTF-8 so we're just using byte arrays to save the information in case we need to save it to a file
/*Q:So why converting from Unicode to UTF-8 takes two functions? How is that a good Idea?
A:Well, I just thought that it could be really nice for debugging purposes, also knowing what the hell the String is converted to before being converted to*/
var thearr = [];
if (UnicodeValue > 0x7FFFFFFF) {
throw new Error("Value cannot be greater than " + 0x7FFFFFFF);
/*You should remove this, if you don't like errors much, it will be annoying to enclose everything
with try{}, make something like:*/
//return [];//then you check if the array is empty, yeah, that will be faster..
//Or you can simply.. don't change this function but check whatever parameter you're going to call this function with
//Stop reading comments, The code is obvious
}
if ((UnicodeValue >>> 7) > 0) { //For some reason after 0x7FFFFFFF if you used the '>>' operator it would convert it to a negative number but with '>>>' operator it doesn't work that way
while (UnicodeValue) { //I also made my own '>>>' operator which is "parseInt(Unicode.toString(2).substring(7),2)" which works after 0xFFFFFFFF but stops working around 0xFFFFFFFFFFFFF and is more likely hell slowler
thearr.push(UnicodeValue & 63); //Gotta love parseInt(int,2) helped me making this function. Aside UTF-8 wikipedia article
UnicodeValue = UnicodeValue >>> 6; //And (int).toString(2) too
}
} else {
thearr.push(UnicodeValue)
}
return thearr;//This is the char number range NOT the UTF-8 use UTF8ArrayToUTF8 to get the UTF8 byte array..
}
//Changes Argument
function UTF8ArrayToUTF8(UTF8Array) { //Only 1 character
//Alright, so basically we convert the array from the previous function to UTF8 byte array
//Obvious code is obvious, even this uglified&minified should be easy to understand
var leng = UTF8Array.length, //Pros:more performance; Cons:more memory usage. Not that anyone cares or something. Or, the fact that I need the original length (Char Number Range)
z,a,
utf8bytearray = []; //all the bytes will be here
if (leng && leng < 2) { //basically leng===1
if ((-1 < UTF8Array[0]) && (UTF8Array[0] < 126)) { //Too paranoic?
return UTF8Array; //Do nothing, only happens with ASCII values
} else { //Too restrictive?
throw new Error("Incorrect value, the array provided doesn't have a correct UTF8 value");
}
} else if (leng < 6) { //On another imaginary format it can be longer than 6, but this is UTF-8 (At least the old version which accepted 6 bytes..)
a = (252 << 6 - leng) & 255; //I don't even remember what I'm doing here, and I just wrote it
utf8bytearray.push(a | UTF8Array.pop());
while ((z = UTF8Array.pop()) !== undefined) {
if (z > 63) { //obvious code is... not so obvious.. try anyway
throw new Error("Incorrect value, the array provided doesn't have a correct UTF8 value");
}
utf8bytearray.push(128 | z);
}
return utf8bytearray; //Done!
} else {
throw new Error("UTF-8 Array cannot have more than 6 values");
}
}
function stringToUint8utf8array(string){//Fuck old browsers
function c(x){var i,a=[];for(i=0;i<x.length;i++)a=a.concat(UTF8ArrayToUTF8(UnicodeToUTF8Array(x.charCodeAt(i))));return a};
var a=c(string),ui8arr=new Uint8Array(a.length);
for(var i=0,l=a.length;i<l;i++){
ui8arr[i]=a[i];
}
return ui8arr;
}
function addOffsetToEachIndexOfNumericArray(arr,numberToAdd){//Brilliant name
for(var i=0,l=arr.length;i<l;i++){
arr[i]+=numberToAdd;
}
return arr;
}
function addArrayKeyToEachIndexOfNumericArray(arr,arrayToAdd){//Brilliant name
for(var i=0,j=0,l=arr.length;i<l;i++,j++){
if(j==arrayToAdd.length){j=0;}
arr[i]+=arrayToAdd[j];
}
return arr;
}
function xorArraysKeyToEachIndexOfNumericArray(arr/*, more than 1 array*/){//Brilliant name
for(var i=0,l=arr.length;i<l;i++){
for(var x=1,ll=arguments.length;x<ll;x++){
arr[i]^=arguments[x][i%arguments[x].length];
}
}
return arr;
}
//uzeful function stringToArrayUnicode(str){for(var i=0,l=str.length,n=[];i<l;i++)n.push(str.charCodeAt(i));return n;}
//useful String.fromCharCode.apply(null,addOffsetToEachIndexOfNumericArray(stringToArrayUnicode("we"),n));
//it never ends, never
function UTF8toUnicode(ByTeS) { //Single Character Version
//UTF8 byte array to Unicode, uhh character.
//This doesn't check if the UTF-8 is correct or valid so you can't directly put UTF-8 strings here (unless the UTF-8 is perfect which is unlikely) if you do there will be some unexpected behaviour
var aUnicode = [],
UnIcOdE = function (x, leng) { //I don't even remember what I'm doing here, but looks important.. It's probably the most important function on this function.
var i, n = 0;
x[0] = x[0] << (6 * (leng - 1));
for (i = 1; i < leng; i++) {
x[i] = x[i] << (6 * (leng - (i + 1)));
for (i = 0; i < leng; i++) {
n += x[i];
}
}
return n;
}, a, i, leng = ByTeS.length; //Im So CrAzY
if (leng === 1) {
if (0 > ByTeS[0] || ByTeS[0] > 127) {
return null;
//throw new Error("Those bytes aren't valid");//damn I'm sleepy
//Can't throw error, it may be very possible
} else {
return ByTeS[0]; //Probably an ASCII value, the most common type
}
} else if (leng <= 6) {
a = (252 << 6 - leng) & 255;
aUnicode.push(a ^ ByTeS[0]);
for (i = 1; i < leng; i++) {
aUnicode.push(128 ^ ByTeS[i]);
}
return UnIcOdE(aUnicode, leng);
} else {
throw new Error("not longer than 6 or shorter than 1");
}
}
//now this is one of the hardest functions (to write, understand, etc)
//POSSIBLY NOT PURE, it calls a pure function but does that makes it inpure?
//Requires:UTF8toUnicode()
function BinaryUTF8toUnicode(bytesArray) {
//To UCS
//splits characters so UTF8toUnicode can convert them without trouble, however it also takes into account if UTF-8 is valid and stuff
//needs an option to disallow overlong encodings
var leng = bytesArray.length,
i, a = [], //I don't even have relevant or interesting variable names for these..
e, c, x = function (x) {
//Gives the exponent.. in base 2, by repeatedly diving in 2
//Precision problems when using Math.log
var b = ~~x, //It's kinda funny '~~' acts as Math.floor()
i;
for (i = -1; b; i++) {
b /= 2;
b = ~~b;
}
return i;
}, k, previousErrors = false, //Finally! a descriptive variable!
g, errorArrayBytes = [],
s; //Another! Amazing!
First: for (i = 0; i < leng; i++) { //OOOOHH I had to use labels for reasons...
if ((e = bytesArray[i]) > 127) {
c = []; //small variables take less time typing
for (k = 6; k > 1; k--) { //is this even lazy? Also, you shouldn't edit it to (k=1; k < 7; k++) since it needs to end with k being one.
if (((252 << 6 - k) & 255) <= e) { //yay, overlong encodings rulez
if (previousErrors) {
i--;
console.warn("Unknown character bytes " + errorArrayBytes);
errorArrayBytes[-1] = "Error"; //EasterEgg shh
a.push(errorArrayBytes); //BUT WHY?!!?!
previousErrors = false; //obvious code is obvious
errorArrayBytes = [];
continue First; //LABELS! DO YOU EVEN KNOW THEM?
}
c.push(e);
break;
}
}
if (k === 1) {
if (((252 << 6 - k) & 255) <= e) { //WUT!!
//throw new Error("OH SHIT ERROR");//Too descriptive
previousErrors = true; //is this variable even necessary? I could check if there's something on errorArrayBytes
errorArrayBytes.push(e); //gotta chek it first
}
} else { //oh god it's happening
g = i;
for (s = k - 1; s > 0; s--) {
if ((e = bytesArray[++i]) > 127 && 192 > e) { //accpeting overlong encodings.. again
c.push(e);
} else {
i = g;
continue First; //gotta love them labels
}
}
console.log(g, k, e);
a.push(UTF8toUnicode(c)); //doesn't sounds reasonable?
}
} else {
a.push(UTF8toUnicode([e]));
}
}
if (previousErrors) { //Yes, I copypasted, should I convert this to a function instead?
console.warn("Unknown character bytes " + errorArrayBytes);
errorArrayBytes[-1] = ("Error"); //Okay, not easter Egg this tells if there was an error, and you don't have to check if you don't want to, it's quite clever, gotta love JavaScript
a.push(errorArrayBytes); //BUT WHY?!!?!
previousErrors = false; //obvious code is obvious
errorArrayBytes = []; //what a strange -1 property for an array?
}
return a;
}
//Uzeful: function c(x){var i,a=[];for(i=0;i<x.length;i++)a=a.concat(UTF8ArrayToUTF8(UnicodeToUTF8Array(x.charCodeAt(i))));return a}
function giveFirstZeroIndex(y) {
//This... gives the index of the first zero.. in binary.
//I planed to use this function on BinaryUTF8toUnicode() but seems using < and > operators was a better idea, I'm still putting this function if ever turns out useful someday.
var x = function (x) {
//Gives the exponent.. in base 2, by repeatedly diving in 2
//Precision problems when using Math.log
var b = ~~x, //It's kinda funny '~~' acts as Math.floor()
i;
for (i = -1; b; i++) {
b /= 2;
b = ~~b;
}
return i;
}, l = x(y),
z = l;
while (l + 1) {
z = y >> l--; //no idea what I'm doing here
if (!(z & 1)) { //checks if the byte is 1
return ++l; //You could say it checks if z is odd or even
} else if (!(~l)) {
return (l = -1); //this is probably pointless
}
}
}
function FindDistanceBetween2Points(x1,y1,x2,y2){//yay math!
var x=x1-x2,y=y1-y2,h2=(x*x)+(y*y);//am I reivnenting the wheel, or there is a function that already does it?
return Math.sqrt(h2);
}
function getAngleFromP1ToP2(x1,y1,x2,y2){//google doesn't even give me the functions!
//The angle from the "origin"
var x=x2-x1,y=y2-y1;//reinventing the wheel is mandatory
return Math.atan2(y,x);
}
function PolarCoordinatesToCartesian(Angle, Distance) {
var y = Math.sin(Angle) * Distance,
x = Math.cos(Angle) * Distance;
return {
x: x,
y: y
};
}
function addTrailingZeros(int, length) { //int should be a string
var i;
int = String(int);
for (i = int.length; i < length; i++) {
int = "0" + int;
}
return int;
}
/*
* Encode a string as utf-8.
* For efficiency, this assumes the input is valid utf-16.
*/
function str2rstr_utf8(input) { //this function isn't actually mine, however it seems to convert a string to UTF-8 to a string... so the bytes are there, but you can't read it..
var output = "";
var i = -1;
var x, y;
while (++i < input.length) {
/* Decode utf-16 surrogate pairs */
x = input.charCodeAt(i);
y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
i++;
}
/* Encode output as utf-8 */
if (x <= 0x7F)
output += String.fromCharCode(x);
else if (x <= 0x7FF)
output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
0x80 | (x & 0x3F));
else if (x <= 0xFFFF)
output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
0x80 | ((x >>> 6) & 0x3F),
0x80 | (x & 0x3F));
else if (x <= 0x1FFFFF)
output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
0x80 | ((x >>> 12) & 0x3F),
0x80 | ((x >>> 6) & 0x3F),
0x80 | (x & 0x3F));
}
return output;
}
function stringcharcodetoarray(x) {
var a = [],
l;
for (i = 0, l = x.length; i < l; i++) {
a.push(strrr.charCodeAt(i))
};
return a
} //guess what it does!
//the opposite can be done with String.fromCharCode.apply(String,array);
function hsvToRgb(h, s, v) {
//A better, neater hsvToRgb.
var r, g, b,f;
var i = Math.floor((f=h * 6));
f -= i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
r = v, g = t, b = p;
break;
case 1:
r = q, g = v, b = p;
break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
case 5:
r = v, g = p, b = q;
break;
}
return [r * 255, g * 255, b * 255];
}
function permutate(array, callback) {
// Do the actual permuation work on array[], starting at index
function p(array, index, callback) {
// Swap elements i1 and i2 in array a[]
function swap(a, i1, i2) {
var t = a[i1];
a[i1] = a[i2];
a[i2] = t;
}
if (index == array.length - 1) {
callback(array);
return 1;
} else {
var count = p(array, index + 1, callback);
for (var i = index + 1; i < array.length; i++) {
swap(array, i, index);
count += p(array, index + 1, callback);
swap(array, i, index);
}
return count;
}
}
if (!array || array.length === 0) {
return 0;
}
return p(array, 0, callback);
}
//STUPID BULLSHIT
/*function ipermutate(a,c){//The old permutation function was recursive, and NOT made by me, because at the time I was stupid and dumb, so I copy pasted from somewhere else, however, seeing how it takes a REALLY long time permutating an array with 10 items, I decided I would make my own, at first it was hard, also the algorithm is completely different, but it's faster, iterative, one downside would be, that the permutations are.. disorganized, somehow, but the good thing is.. that is fast
var x,y,d;
y=a.length-1;
while(y--){
d=a[y];
a[y]=a[y+1];
a[y+1]=d;
x=a.length;
while(x--){
a.unshift(a.pop());
c(a);
}
}
}*/
function ipermutations(a,c){//now this is real
var n=a.length;
var stack=[],prestack=[];//iterative ftw
for(var i=0,ii;i<n;i++){
stack.push(n);
}
c(a)
for(i=0;i<stack.length;i++){
ii=stack[i];
a.splice(n-ii,0,a.pop());
//TOO SLOW
/*
if(ii==stack[i+1]){
//c(a);
}
if(ii==2){continue;}
prestack=[i+1,0];
while(--ii){
prestack.push(stack[i]-1)
}
Array.prototype.splice.apply(stack,prestack)*/
if(ii==stack[i+1]){
c(a);
prestack=[i+1,0];
if(ii==2){continue;}
while(--ii){
prestack.push(stack[i]-1);
}
Array.prototype.splice.apply(stack,prestack);
}
}
return stack.length;
}
//Pick functicon, it picks
//the callback gives indexes of the arrays so you can do whatever you want with them.
function CombinationPick(NumbOfSelects,Items,callbak,singlePick){
var combs=[];
if(singlePick&&NumbOfSelects>Items){NumbOfSelects=Items}
for(var i=0;i<NumbOfSelects;i++){
combs[i]=singlePick?NumbOfSelects-i-1:0;
}
var r=0,c,t=combs.length-1;
callbak(combs.slice());
do{
//NEXT NUMB PROCEDURE
//Big endian
c=++combs[r];
if(c&&(c%Items===0)||(singlePick&&(c+r)%Items===0)){
r++;
continue;
}
if(r){
while(r){r--;combs[r]=singlePick?++c:c}
}
callbak(combs.slice())
}while((combs[t]+1)%Items&&(!(singlePick&&(!((combs[t]+NumbOfSelects)%Items)))));
return combs;
}
//let your nightmare begin here
function getHTMLTag(a) {
if (a === '') {
return null;
}
var stringRemaining = a,
b, n, d, f, k, g, tag, j = -1,
v = [],
A, ii = 0,
use;
while (~(b = stringRemaining.indexOf('<'))) {
//console.log(ii)
if (ii++ > 90) {
return 'u gay';
} //remove or change this limit LOL
d = [];
d.push(stringRemaining.substr(b, b + 1));
f = stringRemaining.substr(b + 1);
v.push(k = []);
j++;
use = '';
if (use += stringRemaining.substr(0, b)) {
k.push('#text', use, null);
stringRemaining = stringRemaining.substr(b);
continue;
}
g = getStringIndexOfIgnoringQuoteMarks(f, '>');
if (~g) {
d.push(y = f.substr(0, g + 1));
k.push((p = getETN(d[1])) ? p : '#text');
console.log(k[0]);
if (k[0] === "#text") {
stringRemaining = f.substr(g + 1);
k.push(d.join(''), null);
continue;
} else if (k[0] === "!--") {
stringRemaining = f.substr(f.indexOf('-->') + 3);
k.push(d.join(''), null);
continue;
}
stringRemaining = f.substr(g + 1);
if (f.charAt(g - 1) === '/') { //oh noes
k.push(null, d[1].match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g));
stringRemaining = f.substr(g + 1);
continue;
} else {
A = getLastTag(stringRemaining, k[0]);
//console.log(A)
A.start = A.start === -1 ? Infinity : A.start;
//k.push(getHTMLTag(stringRemaining.substr(0, A.start)));
k.push(stringRemaining.substr(0, A.start));
}
if(k[0]==='div'){console.log(k,v,getLastTag(stringRemaining, k[0]),JSON.stringify(stringRemaining))}
k.push(d[1].match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g)) //are u even implying this regex doesn't work
A.end = A.end === -1 ? Infinity : A.end;
stringRemaining = stringRemaining.substr(A.end);
} else {
k[j] += f;
break;
}
}
//console.log("b",b)
if (stringRemaining) {
v.push(['#text', stringRemaining, null])
}
return v;
}
function getNearestIndex(str) {
var a=Array.prototype.slice.call(arguments,1),b=[],d={},c;
a.forEach(function(a){var e=str.indexOf(a);~e&&b.push(e)&&(d[e]=a)}) //replace str.indexOf by getStringIndexOfIgnoringQuoteMarks
c=Math.min.apply(void 0,b);
return c!==Infinity?{i:c,t:d[c]}:{i:-1};
}
function getLastTag(string, tag) {
var s = string,
a, i = 0,
ini = '<' + tag,
fi = '</' + tag,
il, inl = ini.length,
con = 0,
p, TAGC = 0;
while (true) {
//console.log(con,TAGC);
a = getNearestIndex(s, ini, fi);
if (~a.i) {
if (a.t.substr(0, 2) === "</") {
con += (p = s.indexOf(fi) + fi.length + 1);
//console.log(con,a,s)
s = s.substr(p-1);
if (--TAGC === -1) {
return {
start: con - fi.length-1,
end: getStringIndexOfIgnoringQuoteMarks(s, '>') + con
};
}
} else {
il = inl + a.i;
s = s.substr(con+=(getStringIndexOfIgnoringQuoteMarks(s.substr(il), '>'))+ il + 1);
TAGC++;
//console.log(con,s)
// con += il;
}
} else {
return {
start: -1,
end: -1
};
}
}
}
function getETN(a){//Element Tag Name
//Element must not have initial '<'
function getNI(str) {
var a=Array.prototype.slice.call(arguments,1),b=[],d={},c;
a.forEach(function(a){var e=getStringIndexOfIgnoringQuoteMarks(str,a);~e&&b.push(e)&&(d[e]=a)}) //replace str.indexOf by getStringIndexOfIgnoringQuoteMarks
c=Math.min.apply(void 0,b);
return c!==Infinity?{i:c,t:d[c]}:{i:-1};
}
var b=getNI(a,' ','>');
return a.substr(0,b.i);
}
function getStringIndexOfIgnoringQuoteMarks(string, indexof) { //best function in the universe, ever
//eureka motherfuckers
//Well. is not really the function it is what the function does.
//What this functions does? Gets the index of something... that isn't enclosed with quotation marks
var a, b = [],
s = string,
h = 0,
f = 0,
x; //need to edit shit
while (true) {
a = [s.indexOf('"'), s.indexOf(indexof), s.indexOf('\'')]; //All the indexes are stored in a array
a.forEach(function (a) {
if (~a) {
b.push(a)
}
}); //Push the values in another array except if the value is -1 the values in another array except if b);//Get the nearest the values in another array except if the value is -1
x = Math.min.apply(Math, b); //Get the nearest character to the start either ',"" or indexof
//console.log(s,x,f,h,string.substr(h+x,indexof.length))
b = [];
if (x === Infinity) { //Math.min returns infinite when there are no parameters at all, meaning it didn't found what it was looking for..
return -1;
}
h += f;
if (s === "") {
return -1;
}
if (string.substr(h+x,indexof.length) === indexof) {
return h + x;
} else {
s = s.substring(f = s.substring(x + 1).indexOf(s.charAt(x)) + 1 + (x + 1));
}
}
}
function ASCIIToEncodedBatch(str){
return str.replace(/%/g,'%%').replace(/\^/g,'^^').replace(/[&\|<>"'`\=\\\/,;\(!\)0-9]/g,function(a){return '^'+a;}).replace(/[\n\r]/g,function(a){return '^'+a+a})
}
function makeElem(elemName,attribs){//atribs must be an Object
var e=document.createElement(elemName);
for(var a in attribs){
if(attribs.hasOwnProperty(a)){
e.setAttribute(a,attribs[a]);
}
}
return e;
}
function GreatestDivisor(n){
if(!(n%2)){return n/2;}else{var p=1;
while((p+=2)<n/2){
if(!(n%p)){return n/p}
}
return n;
}
}
function getDivisors(n){
var lel=[];
if(!(n%2)){lel.push(n/2,2);}
var p=1;
lel.push(p);
while((p+=2)<n/2){
if(!(n%p)){lel.push(p,n/p);}
}
lel.push(n);
lel.sort(function(a,b){return a===b?0:a>b?1:-1;})
return lel;
}
function HTTP(url,callback,method,post,headers){ //headers is an object like this {Connection:"keep-alive"}
function looProp(object,callback){
var a;
for(a in object){
if(object.hasOwnProperty(a))callback.call(object,a,object[a]);
}
}
method=method||"GET";
var xhr=new XMLHttpRequest();
xhr.open(method,url,true);
looProp(headers,function(a,b){xhr.setRequestHeader(a,b)})
xhr.onloadend=function(){if(xhr.readyState==xhr.DONE){callback(xhr)}};
xhr.send(post);
//console.log(xhr,arguments,'HTTP Function')
return xhr;
}
function inherit(childClass,parentClass,allObjects) {
var f=function(){var x;for(x in allObjects){this[x]=allObjects[x]}}; // defining temp empty function
f.prototype=parentClass.prototype;
f.prototype.constructor=f;
childClass.prototype=new f;
childClass.prototype.constructor=childClass; // restoring proper constructor for child class
parentClass.prototype.constructor=parentClass; // restoring proper constructor for parent class
}
//I knew you were going to love this
//This function is what gave me the idea of the bitset function
function toBase64(uint8array){//of bits, each value may not have more than 255 bits... //a normal "array" should work fine too..
//From 0x29 to 0x5a plus from 0x61 to 0x7A AND from 0x30 to 0x39
//Will not report errors if an array index has a value bigger than 255.. it will likely fail.
var a=[],i,output=[];
for(i=0x41;i<=0x5a;i++){//A-Z
a.push(String.fromCharCode(i));
}
for(i=0x61;i<=0x7A;i++){//a-z
a.push(String.fromCharCode(i));
}
for(i=0x30;i<=0x39;i++){//0-9
a.push(String.fromCharCode(i));
}
a.push('+','/');
//function inside functions are dumb so why don't put the both functions inside an anonymous function..?
function generateOnesByLength(n){//Attempts to generate a binary number full of ones given a length..
//This function original was inside the paf function, nested functions are retarded so I put that function inside the parent function now they don't redefine each other that much.
//They are still on a parent function so it's not so.. efficient as you would expect
var x=0;
for(var i=0;i<n;i++){
x<<=1;x|=1;//I don't know if this is performant faster than Math.pow but seriously I don't think I'll need Math.pow, do I?
}
return x;
}
function paf(_offset,_offsetlength,_number){//I don't have any name for this function at ALL, but I will explain what it doess, it takes an offset, a number and returns the base64 number and the offset of the next number.
//the next function will be used to extract the offset of the number..
var a=6-_offsetlength,b=8-a;//Oh god, 8 is HARDCODED! Because 8 is the number of bits in a byte!!!
//And 6 is the mini-byte used by wikipedia base64 article... at least on 2013.
//I imagine this code being read in 2432 or something, that probably won't happen..
return [_number&generateOnesByLength(b),b,(_offset<<a)|(_number>>b)];//offset & offsetlength & number
}
var offset=0,offsetLength=0,x;
for(var i=0,l=uint8array.length;i<l;i++){
if(offsetLength==6){//if offsetlength is 6 that means that a whole offset is occupying the space of a byte, can you believe it.
offsetLength=0;
output.push(a[offset]);
offset=0;
i--;
continue;
}
x=paf(offset,offsetLength,uint8array[i]);
offset=x[0];
offsetLength=x[1];
output.push(a[x[2]]);
}
if(offsetLength){
if(offsetLength==6){
output.push(a[offset]);
}else{
var y=(6-offsetLength)/2;
x=paf(offset,offsetLength,0);
offset=x[0];
output.push(a[x[2]]);
switch (y){
case 2:output.push('=');//This thingy right here, you know.. the offsets also, no break statement;
case 1:output.push('=');break;
}
}}
return output.join('');//You can change it so the result is an array instead!!!!
}
function numberToLatinPeriodicElementName(n){
var pSymbols=['n',"u",'b','t','q','p','h','s','o','e'],
names=["nil","un","bi","tri","quad","pent","hex","sept",'oct','enn'],v=[],x=n,y=[];
while(x){
v.push(x%10);
x=Math.floor(x/10);
}
var first=v.shift(),f;
f=names[first]+"ium";
if(first==2||first==3){//Not hardcoding would waste valuable resources
f=names[first]+"um";
}
return {str:v.map(function(a){y.push(pSymbols[a]);
return names[a];
}).reverse().join('')+f,symbol:y.reverse().join('')+pSymbols[first]};
}
//OMFG the previous HTML parser was fucking horrible!
//With the help of IRC pony friends I managed to make it.. better,
function TokenizeHTML(string){
var state=0,firststatechars=['<!','</','<'],endingTag=false,returnObject=[],ret='',c='',d,ii,ll,tagname='';
//sorry for uncommented shit, I don't even know what Im'd doing
//state 0 is outside tag, 1 is inside a normal tag
//ret,c,d,ii,ll have various uses dependent on the state and loop, so I can't really know what each one does, they're like counters and state keepers
//ret usually works as adding the text outside the tags
function ifret(){
if(ret){
returnObject.push({Type:"text",Content:ret});
ret='';
}
}
//this is a lot faster than creating a character array;
function isUpperCharacterSet(c){return c.charCodeAt()>=0x41&&c.charCodeAt()<=0x5a;}
function isLowerCharacterSet(c){return c.charCodeAt()>=0x61&&c.charCodeAt()<=0x7a;}
var whitespaceCodePoints=[0,0x20,0xA0,0x2f/*FOR SCIENCE*/];//the / character as a whitespace maay be helpful in occasions, whatever
function isWhitespace(c){
var carh=c.charCodeAt();
for(var i=0,l=whitespaceCodePoints.length;i<l;i++){
if(whitespaceCodePoints[i]==carh){return true;}
}
return carh>=0x9&&carh<=0xd;
}
function submittag(content,state){
if(state==1){
returnObject.push({Type:"WhiteSpace",Content:content});
}else if(state==2){returnObject.push({Type:"attributeName",Content:content});}}
MAINLOOP:for(var i=0,l=string.length;i<l;i++){
if(state==0){
c=string.substr(i,2);
for(ii=0,ll=firststatechars.length;ii<ll;ii++){
//console.log(string[i],c,ret);
if(c.indexOf(firststatechars[ii])==0){
if(ii==0){
if(string.substr(i+2,2)=="--"){
d=string.substr(i+2).indexOf('-->');
c=d!=-1?string.substr(i,d+5):string.substr(i);
ifret();
returnObject.push({Type:"comment",Content:c});
//console.log("Comment",c,state);
i=i+c.length-1;
c='';
continue MAINLOOP;
}else{
state=3;
i++;
c='';
ifret();
returnObject.push({Type:"startTag",Content:"<!"});
}
} else if(ii==2||ii==1){
if(ii==1){i++;endingTag=true;};
if(isUpperCharacterSet(string.charAt(i+1))||isLowerCharacterSet(string.charAt(i+1))){
ifret();
state=1;
c='';
if(ii==1){returnObject.push({Type:"startTag",Content:"</"});continue MAINLOOP;}
returnObject.push({Type:"startTag",Content:"<"});
continue MAINLOOP;
}else{
ret+=string.charAt(i);
//console.log("RET1",ret)
continue MAINLOOP;
}
};
break;
}
}
ret+=string.charAt(i);
//console.log("RET2",ret)
}else if(state==1){
//console.log("RET",ret);
d=string.charAt(i);
//console.log(d);
if(!tagname){
//console.log(c.charCodeAt())
if(isWhitespace(d)||d=='>'){
returnObject.push({Type:"tagName",Content:c});
tagname=c;i--;
c='';ii=0;ll=0;
}else{c+=d;}
}else{
if(d=='/'&&string.charAt(i+1)=='>'){
if(ii==1||ii==2){
submittag(c,ii);c='';
}
endingTag=true;
ii=3;
continue;
}
if(isWhitespace(d)){
if(ii==2){
submittag(c,ii);c='';
}
c+=d;
ii=1;
continue;
}
if(ii==1){
submittag(c,ii);c='';
}
if(d=='>'){
//console.log
if(ii==2){
submittag(c,ii);c='';
}
state=0;
c='';
returnObject.push({Type:"endTag",Content:ii==3?"/>":">"});
//console.log("TAGNAME",tagname,endingTag,ii)
if((tagname=='script'||tagname=='style')&&!endingTag){
i++;
returnObject.push({Type:"verbatimText",Content:string.substr(i,state=string.substr(i).indexOf("</"+tagname))});//saving mem
i+=state-1;
state=0;
}
endingTag=false;
tagname='';
continue;
}
if(d=="="){
if(ii==2){
submittag(c,ii);c='';
}
state=2;
ii=0;
returnObject.push({Type:"setter",Content:"="});
continue;
}
ii=2;
c+=d;
}
}else if(state==2){
d=string.charAt(i);
if((isWhitespace(d)||d=='>')&&d!=='/'){
//console.log(ii)
if(ii==2){
ii=0;//console.log("HO GOD")
i--;state=1;
returnObject.push({Type:"attributeValue",Content:c});
c='';continue;
}
if(d=='>'){ii=0;state=0;
if(ii==1){
submittag(c,ii);c='';
}
c='';
returnObject.push({Type:"endTag",Content:">"});
continue;
}
c+=d;
ii=1;
continue;
}
if(ii==1){
submittag(c,ii);c='';
}
if(d=="'"||d=='"'){if(ii==2){
submittag(c,ii);c='';
};
ii=string.substring(i+1).indexOf(d)+1;
//console.log(string.substr(i,ii));
returnObject.push({Type:"attributeValue",Content:string.substr(i,ii+1)});
i+=ii;
ii=0;state=1;continue;}
ii=2;
c+=d;
}
}
ifret();
return returnObject;
}
var maht = (function () {//WRAPS ALL FUNCTIONS IN AN OBJECT, VERIFIES, AND CHECKS IF ITS ENDIANNESS
var mat = (function () {//WRAPS ALL FUNCTIONS IN AN OBJECT, STILL DOESN'T VERIFY
var math;//SUPER BASIC WRAPPER, CONTAINS ALL MAIN FUNCTIONS DOESN'T VERIFY
function IntToBase(integer, base) { //doesn't support base 1
var digits = [],
i = integer;
do {
digits.push(i % base);
i /= base;
} while (i |= 0);
return digits; //Little Endian!
}
function checkIfarraysareEqual(a, b) {
var x;
if ((x = a.length) === b.length) {
for (var i = 0, l = a.length; i < l; i++) {
if (a[i] !== b[i]) return false;
}
} else return false;
return true;
}
function Division(array1, array2, b) { //We just combined the small division with this method and it should be fastest as ever for long numbers with huge bases!
if (isGreaterThan(array2, array1)) return [[], array1.slice(0)];
var power = [],
dividend = array1,
i = array1.length - 1;
while (i--) {
power.push(0);
}
//console.log("ARRAYS",array1,array2);
power.push(1);
var quotient = [],
z, f;
while (power.length) {
//if(i++>30)return"faggot";
if (isGreaterThan(dividend, z = multiplication(power, array2, b)) || checkIfarraysareEqual(z, dividend)) { //THIS CAN BE OPTMIZED EVEN FURTHER
//console.log(dividend,z,array2,power);
f = smalldivision(dividend, z, b);
//console.log(dividend,z,f)
dividend = substraction(dividend, multiplication(z, f, b), b);
quotient = addition(quotient, multiplication(power, f, b), b);