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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
|
/*
* This is the Changeset library copied from the old Etherpad with some modifications to use it in node.js
* Can be found in https://github.com/ether/pad/blob/master/infrastructure/ace/www/easysync2.js
*/
/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
/*
* Copyright 2009 Google Inc., 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var AttributePool = require("./AttributePool");
/**
* ==================== General Util Functions =======================
*/
/**
* This method is called whenever there is an error in the sync process
* @param msg {string} Just some message
*/
exports.error = function error(msg) {
var e = new Error(msg);
e.easysync = true;
throw e;
};
/**
* This method is user for assertions with Messages
* if assert fails, the error function called.
* @param b {boolean} assertion condition
* @param msgParts {string} error to be passed if it fails
*/
exports.assert = function assert(b, msgParts) {
if (!b) {
var msg = Array.prototype.slice.call(arguments, 1).join('');
exports.error("exports: " + msg);
}
};
/**
* Parses a number from string base 36
* @param str {string} string of the number in base 36
* @returns {int} number
*/
exports.parseNum = function (str) {
return parseInt(str, 36);
};
/**
* Writes a number in base 36 and puts it in a string
* @param num {int} number
* @returns {string} string
*/
exports.numToString = function (num) {
return num.toString(36).toLowerCase();
};
/**
* Converts stuff before $ to base 10
* @obsolete not really used anywhere??
* @param cs {string} the string
* @return integer
*/
exports.toBaseTen = function (cs) {
var dollarIndex = cs.indexOf('$');
var beforeDollar = cs.substring(0, dollarIndex);
var fromDollar = cs.substring(dollarIndex);
return beforeDollar.replace(/[0-9a-z]+/g, function (s) {
return String(exports.parseNum(s));
}) + fromDollar;
};
/**
* ==================== Changeset Functions =======================
*/
/**
* returns the required length of the text before changeset
* can be applied
* @param cs {string} String representation of the Changeset
*/
exports.oldLen = function (cs) {
return exports.unpack(cs).oldLen;
};
/**
* returns the length of the text after changeset is applied
* @param cs {string} String representation of the Changeset
*/
exports.newLen = function (cs) {
return exports.unpack(cs).newLen;
};
/**
* this function creates an iterator which decodes string changeset operations
* @param opsStr {string} String encoding of the change operations to be performed
* @param optStartIndex {int} from where in the string should the iterator start
* @return {Op} type object iterator
*/
exports.opIterator = function (opsStr, optStartIndex) {
//print(opsStr);
var regex = /((?:\*[0-9a-z]+)*)(?:\|([0-9a-z]+))?([-+=])([0-9a-z]+)|\?|/g;
var startIndex = (optStartIndex || 0);
var curIndex = startIndex;
var prevIndex = curIndex;
function nextRegexMatch() {
prevIndex = curIndex;
var result;
regex.lastIndex = curIndex;
result = regex.exec(opsStr);
curIndex = regex.lastIndex;
if (result[0] == '?') {
exports.error("Hit error opcode in op stream");
}
return result;
}
var regexResult = nextRegexMatch();
var obj = exports.newOp();
function next(optObj) {
var op = (optObj || obj);
if (regexResult[0]) {
op.attribs = regexResult[1];
op.lines = exports.parseNum(regexResult[2] || 0);
op.opcode = regexResult[3];
op.chars = exports.parseNum(regexResult[4]);
regexResult = nextRegexMatch();
} else {
exports.clearOp(op);
}
return op;
}
function hasNext() {
return !!(regexResult[0]);
}
function lastIndex() {
return prevIndex;
}
return {
next: next,
hasNext: hasNext,
lastIndex: lastIndex
};
};
/**
* Cleans an Op object
* @param {Op} object to be cleared
*/
exports.clearOp = function (op) {
op.opcode = '';
op.chars = 0;
op.lines = 0;
op.attribs = '';
};
/**
* Creates a new Op object
* @param optOpcode the type operation of the Op object
*/
exports.newOp = function (optOpcode) {
return {
opcode: (optOpcode || ''),
chars: 0,
lines: 0,
attribs: ''
};
};
/**
* Clones an Op
* @param op Op to be cloned
*/
exports.cloneOp = function (op) {
return {
opcode: op.opcode,
chars: op.chars,
lines: op.lines,
attribs: op.attribs
};
};
/**
* Copies op1 to op2
* @param op1 src Op
* @param op2 dest Op
*/
exports.copyOp = function (op1, op2) {
op2.opcode = op1.opcode;
op2.chars = op1.chars;
op2.lines = op1.lines;
op2.attribs = op1.attribs;
};
/**
* Writes the Op in a string the way that changesets need it
*/
exports.opString = function (op) {
// just for debugging
if (!op.opcode) return 'null';
var assem = exports.opAssembler();
assem.append(op);
return assem.toString();
};
/**
* Used just for debugging
*/
exports.stringOp = function (str) {
// just for debugging
return exports.opIterator(str).next();
};
/**
* Used to check if a Changeset if valid
* @param cs {Changeset} Changeset to be checked
*/
exports.checkRep = function (cs) {
// doesn't check things that require access to attrib pool (e.g. attribute order)
// or original string (e.g. newline positions)
var unpacked = exports.unpack(cs);
var oldLen = unpacked.oldLen;
var newLen = unpacked.newLen;
var ops = unpacked.ops;
var charBank = unpacked.charBank;
var assem = exports.smartOpAssembler();
var oldPos = 0;
var calcNewLen = 0;
var numInserted = 0;
var iter = exports.opIterator(ops);
while (iter.hasNext()) {
var o = iter.next();
switch (o.opcode) {
case '=':
oldPos += o.chars;
calcNewLen += o.chars;
break;
case '-':
oldPos += o.chars;
exports.assert(oldPos < oldLen, oldPos, " >= ", oldLen, " in ", cs);
break;
case '+':
{
calcNewLen += o.chars;
numInserted += o.chars;
exports.assert(calcNewLen < newLen, calcNewLen, " >= ", newLen, " in ", cs);
break;
}
}
assem.append(o);
}
calcNewLen += oldLen - oldPos;
charBank = charBank.substring(0, numInserted);
while (charBank.length < numInserted) {
charBank += "?";
}
assem.endDocument();
var normalized = exports.pack(oldLen, calcNewLen, assem.toString(), charBank);
exports.assert(normalized == cs, normalized, ' != ', cs);
return cs;
}
/**
* ==================== Util Functions =======================
*/
/**
* creates an object that allows you to append operations (type Op) and also
* compresses them if possible
*/
exports.smartOpAssembler = function () {
// Like opAssembler but able to produce conforming exportss
// from slightly looser input, at the cost of speed.
// Specifically:
// - merges consecutive operations that can be merged
// - strips final "="
// - ignores 0-length changes
// - reorders consecutive + and - (which margingOpAssembler doesn't do)
var minusAssem = exports.mergingOpAssembler();
var plusAssem = exports.mergingOpAssembler();
var keepAssem = exports.mergingOpAssembler();
var assem = exports.stringAssembler();
var lastOpcode = '';
var lengthChange = 0;
function flushKeeps() {
assem.append(keepAssem.toString());
keepAssem.clear();
}
function flushPlusMinus() {
assem.append(minusAssem.toString());
minusAssem.clear();
assem.append(plusAssem.toString());
plusAssem.clear();
}
function append(op) {
if (!op.opcode) return;
if (!op.chars) return;
if (op.opcode == '-') {
if (lastOpcode == '=') {
flushKeeps();
}
minusAssem.append(op);
lengthChange -= op.chars;
} else if (op.opcode == '+') {
if (lastOpcode == '=') {
flushKeeps();
}
plusAssem.append(op);
lengthChange += op.chars;
} else if (op.opcode == '=') {
if (lastOpcode != '=') {
flushPlusMinus();
}
keepAssem.append(op);
}
lastOpcode = op.opcode;
}
function appendOpWithText(opcode, text, attribs, pool) {
var op = exports.newOp(opcode);
op.attribs = exports.makeAttribsString(opcode, attribs, pool);
var lastNewlinePos = text.lastIndexOf('\n');
if (lastNewlinePos < 0) {
op.chars = text.length;
op.lines = 0;
append(op);
} else {
op.chars = lastNewlinePos + 1;
op.lines = text.match(/\n/g).length;
append(op);
op.chars = text.length - (lastNewlinePos + 1);
op.lines = 0;
append(op);
}
}
function toString() {
flushPlusMinus();
flushKeeps();
return assem.toString();
}
function clear() {
minusAssem.clear();
plusAssem.clear();
keepAssem.clear();
assem.clear();
lengthChange = 0;
}
function endDocument() {
keepAssem.endDocument();
}
function getLengthChange() {
return lengthChange;
}
return {
append: append,
toString: toString,
clear: clear,
endDocument: endDocument,
appendOpWithText: appendOpWithText,
getLengthChange: getLengthChange
};
};
exports.mergingOpAssembler = function () {
// This assembler can be used in production; it efficiently
// merges consecutive operations that are mergeable, ignores
// no-ops, and drops final pure "keeps". It does not re-order
// operations.
var assem = exports.opAssembler();
var bufOp = exports.newOp();
// If we get, for example, insertions [xxx\n,yyy], those don't merge,
// but if we get [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n].
// This variable stores the length of yyy and any other newline-less
// ops immediately after it.
var bufOpAdditionalCharsAfterNewline = 0;
function flush(isEndDocument) {
if (bufOp.opcode) {
if (isEndDocument && bufOp.opcode == '=' && !bufOp.attribs) {
// final merged keep, leave it implicit
} else {
assem.append(bufOp);
if (bufOpAdditionalCharsAfterNewline) {
bufOp.chars = bufOpAdditionalCharsAfterNewline;
bufOp.lines = 0;
assem.append(bufOp);
bufOpAdditionalCharsAfterNewline = 0;
}
}
bufOp.opcode = '';
}
}
function append(op) {
if (op.chars > 0) {
if (bufOp.opcode == op.opcode && bufOp.attribs == op.attribs) {
if (op.lines > 0) {
// bufOp and additional chars are all mergeable into a multi-line op
bufOp.chars += bufOpAdditionalCharsAfterNewline + op.chars;
bufOp.lines += op.lines;
bufOpAdditionalCharsAfterNewline = 0;
} else if (bufOp.lines == 0) {
// both bufOp and op are in-line
bufOp.chars += op.chars;
} else {
// append in-line text to multi-line bufOp
bufOpAdditionalCharsAfterNewline += op.chars;
}
} else {
flush();
exports.copyOp(op, bufOp);
}
}
}
function endDocument() {
flush(true);
}
function toString() {
flush();
return assem.toString();
}
function clear() {
assem.clear();
exports.clearOp(bufOp);
}
return {
append: append,
toString: toString,
clear: clear,
endDocument: endDocument
};
};
exports.opAssembler = function () {
var pieces = [];
// this function allows op to be mutated later (doesn't keep a ref)
function append(op) {
pieces.push(op.attribs);
if (op.lines) {
pieces.push('|', exports.numToString(op.lines));
}
pieces.push(op.opcode);
pieces.push(exports.numToString(op.chars));
}
function toString() {
return pieces.join('');
}
function clear() {
pieces.length = 0;
}
return {
append: append,
toString: toString,
clear: clear
};
};
/**
* A custom made String Iterator
* @param str {string} String to be iterated over
*/
exports.stringIterator = function (str) {
var curIndex = 0;
function assertRemaining(n) {
exports.assert(n <= remaining(), "!(", n, " <= ", remaining(), ")");
}
function take(n) {
assertRemaining(n);
var s = str.substr(curIndex, n);
curIndex += n;
return s;
}
function peek(n) {
assertRemaining(n);
var s = str.substr(curIndex, n);
return s;
}
function skip(n) {
assertRemaining(n);
curIndex += n;
}
function remaining() {
return str.length - curIndex;
}
return {
take: take,
skip: skip,
remaining: remaining,
peek: peek
};
};
/**
* A custom made StringBuffer
*/
exports.stringAssembler = function () {
var pieces = [];
function append(x) {
pieces.push(String(x));
}
function toString() {
return pieces.join('');
}
return {
append: append,
toString: toString
};
};
/**
* This class allows to iterate and modify texts which have several lines
* It is used for applying Changesets on arrays of lines
* Note from prev docs: "lines" need not be an array as long as it supports certain calls (lines_foo inside).
*/
exports.textLinesMutator = function (lines) {
// Mutates lines, an array of strings, in place.
// Mutation operations have the same constraints as exports operations
// with respect to newlines, but not the other additional constraints
// (i.e. ins/del ordering, forbidden no-ops, non-mergeability, final newline).
// Can be used to mutate lists of strings where the last char of each string
// is not actually a newline, but for the purposes of N and L values,
// the caller should pretend it is, and for things to work right in that case, the input
// to insert() should be a single line with no newlines.
var curSplice = [0, 0];
var inSplice = false;
// position in document after curSplice is applied:
var curLine = 0,
curCol = 0;
// invariant: if (inSplice) then (curLine is in curSplice[0] + curSplice.length - {2,3}) &&
// curLine >= curSplice[0]
// invariant: if (inSplice && (curLine >= curSplice[0] + curSplice.length - 2)) then
// curCol == 0
function lines_applySplice(s) {
lines.splice.apply(lines, s);
}
function lines_toSource() {
return lines.toSource();
}
function lines_get(idx) {
if (lines.get) {
return lines.get(idx);
} else {
return lines[idx];
}
}
// can be unimplemented if removeLines's return value not needed
function lines_slice(start, end) {
if (lines.slice) {
return lines.slice(start, end);
} else {
return [];
}
}
function lines_length() {
if ((typeof lines.length) == "number") {
return lines.length;
} else {
return lines.length();
}
}
function enterSplice() {
curSplice[0] = curLine;
curSplice[1] = 0;
if (curCol > 0) {
putCurLineInSplice();
}
inSplice = true;
}
function leaveSplice() {
lines_applySplice(curSplice);
curSplice.length = 2;
curSplice[0] = curSplice[1] = 0;
inSplice = false;
}
function isCurLineInSplice() {
return (curLine - curSplice[0] < (curSplice.length - 2));
}
function debugPrint(typ) {
print(typ + ": " + curSplice.toSource() + " / " + curLine + "," + curCol + " / " + lines_toSource());
}
function putCurLineInSplice() {
if (!isCurLineInSplice()) {
curSplice.push(lines_get(curSplice[0] + curSplice[1]));
curSplice[1]++;
}
return 2 + curLine - curSplice[0];
}
function skipLines(L, includeInSplice) {
if (L) {
if (includeInSplice) {
if (!inSplice) {
enterSplice();
}
for (var i = 0; i < L; i++) {
curCol = 0;
putCurLineInSplice();
curLine++;
}
} else {
if (inSplice) {
if (L > 1) {
leaveSplice();
} else {
putCurLineInSplice();
}
}
curLine += L;
curCol = 0;
}
//print(inSplice+" / "+isCurLineInSplice()+" / "+curSplice[0]+" / "+curSplice[1]+" / "+lines.length);
/*if (inSplice && (! isCurLineInSplice()) && (curSplice[0] + curSplice[1] < lines.length)) {
print("BLAH");
putCurLineInSplice();
}*/
// tests case foo in remove(), which isn't otherwise covered in current impl
}
//debugPrint("skip");
}
function skip(N, L, includeInSplice) {
if (N) {
if (L) {
skipLines(L, includeInSplice);
} else {
if (includeInSplice && !inSplice) {
enterSplice();
}
if (inSplice) {
putCurLineInSplice();
}
curCol += N;
//debugPrint("skip");
}
}
}
function removeLines(L) {
var removed = '';
if (L) {
if (!inSplice) {
enterSplice();
}
function nextKLinesText(k) {
var m = curSplice[0] + curSplice[1];
return lines_slice(m, m + k).join('');
}
if (isCurLineInSplice()) {
//print(curCol);
if (curCol == 0) {
removed = curSplice[curSplice.length - 1];
// print("FOO"); // case foo
curSplice.length--;
removed += nextKLinesText(L - 1);
curSplice[1] += L - 1;
} else {
removed = nextKLinesText(L - 1);
curSplice[1] += L - 1;
var sline = curSplice.length - 1;
removed = curSplice[sline].substring(curCol) + removed;
curSplice[sline] = curSplice[sline].substring(0, curCol) + lines_get(curSplice[0] + curSplice[1]);
curSplice[1] += 1;
}
} else {
removed = nextKLinesText(L);
curSplice[1] += L;
}
//debugPrint("remove");
}
return removed;
}
function remove(N, L) {
var removed = '';
if (N) {
if (L) {
return removeLines(L);
} else {
if (!inSplice) {
enterSplice();
}
var sline = putCurLineInSplice();
removed = curSplice[sline].substring(curCol, curCol + N);
curSplice[sline] = curSplice[sline].substring(0, curCol) + curSplice[sline].substring(curCol + N);
//debugPrint("remove");
}
}
return removed;
}
function insert(text, L) {
if (text) {
if (!inSplice) {
enterSplice();
}
if (L) {
var newLines = exports.splitTextLines(text);
if (isCurLineInSplice()) {
//if (curCol == 0) {
//curSplice.length--;
//curSplice[1]--;
//Array.prototype.push.apply(curSplice, newLines);
//curLine += newLines.length;
//}
//else {
var sline = curSplice.length - 1;
var theLine = curSplice[sline];
var lineCol = curCol;
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
curLine++;
newLines.splice(0, 1);
Array.prototype.push.apply(curSplice, newLines);
curLine += newLines.length;
curSplice.push(theLine.substring(lineCol));
curCol = 0;
//}
} else {
Array.prototype.push.apply(curSplice, newLines);
curLine += newLines.length;
}
} else {
var sline = putCurLineInSplice();
curSplice[sline] = curSplice[sline].substring(0, curCol) + text + curSplice[sline].substring(curCol);
curCol += text.length;
}
//debugPrint("insert");
}
}
function hasMore() {
//print(lines.length+" / "+inSplice+" / "+(curSplice.length - 2)+" / "+curSplice[1]);
var docLines = lines_length();
if (inSplice) {
docLines += curSplice.length - 2 - curSplice[1];
}
return curLine < docLines;
}
function close() {
if (inSplice) {
leaveSplice();
}
//debugPrint("close");
}
var self = {
skip: skip,
remove: remove,
insert: insert,
close: close,
hasMore: hasMore,
removeLines: removeLines,
skipLines: skipLines
};
return self;
};
/**
* Function allowing iterating over two Op strings.
* @params in1 {string} first Op string
* @params idx1 {int} integer where 1st iterator should start
* @params in2 {string} second Op string
* @params idx2 {int} integer where 2nd iterator should start
* @params func {function} which decides how 1st or 2nd iterator
* advances. When opX.opcode = 0, iterator X advances to
* next element
* func has signature f(op1, op2, opOut)
* op1 - current operation of the first iterator
* op2 - current operation of the second iterator
* opOut - result operator to be put into Changeset
* @return {string} the integrated changeset
*/
exports.applyZip = function (in1, idx1, in2, idx2, func) {
var iter1 = exports.opIterator(in1, idx1);
var iter2 = exports.opIterator(in2, idx2);
var assem = exports.smartOpAssembler();
var op1 = exports.newOp();
var op2 = exports.newOp();
var opOut = exports.newOp();
while (op1.opcode || iter1.hasNext() || op2.opcode || iter2.hasNext()) {
if ((!op1.opcode) && iter1.hasNext()) iter1.next(op1);
if ((!op2.opcode) && iter2.hasNext()) iter2.next(op2);
func(op1, op2, opOut);
if (opOut.opcode) {
//print(opOut.toSource());
assem.append(opOut);
opOut.opcode = '';
}
}
assem.endDocument();
return assem.toString();
};
/**
* Unpacks a string encoded Changeset into a proper Changeset object
* @params cs {string} String encoded Changeset
* @returns {Changeset} a Changeset class
*/
exports.unpack = function (cs) {
var headerRegex = /Z:([0-9a-z]+)([><])([0-9a-z]+)|/;
var headerMatch = headerRegex.exec(cs);
if ((!headerMatch) || (!headerMatch[0])) {
exports.error("Not a exports: " + cs);
}
var oldLen = exports.parseNum(headerMatch[1]);
var changeSign = (headerMatch[2] == '>') ? 1 : -1;
var changeMag = exports.parseNum(headerMatch[3]);
var newLen = oldLen + changeSign * changeMag;
var opsStart = headerMatch[0].length;
var opsEnd = cs.indexOf("$");
if (opsEnd < 0) opsEnd = cs.length;
return {
oldLen: oldLen,
newLen: newLen,
ops: cs.substring(opsStart, opsEnd),
charBank: cs.substring(opsEnd + 1)
};
};
/**
* Packs Changeset object into a string
* @params oldLen {int} Old length of the Changeset
* @params newLen {int] New length of the Changeset
* @params opsStr {string} String encoding of the changes to be made
* @params bank {string} Charbank of the Changeset
* @returns {Changeset} a Changeset class
*/
exports.pack = function (oldLen, newLen, opsStr, bank) {
var lenDiff = newLen - oldLen;
var lenDiffStr = (lenDiff >= 0 ? '>' + exports.numToString(lenDiff) : '<' + exports.numToString(-lenDiff));
var a = [];
a.push('Z:', exports.numToString(oldLen), lenDiffStr, opsStr, '$', bank);
return a.join('');
};
/**
* Applies a Changeset to a string
* @params cs {string} String encoded Changeset
* @params str {string} String to which a Changeset should be applied
*/
exports.applyToText = function (cs, str) {
var unpacked = exports.unpack(cs);
exports.assert(str.length == unpacked.oldLen, "mismatched apply: ", str.length, " / ", unpacked.oldLen);
var csIter = exports.opIterator(unpacked.ops);
var bankIter = exports.stringIterator(unpacked.charBank);
var strIter = exports.stringIterator(str);
var assem = exports.stringAssembler();
while (csIter.hasNext()) {
var op = csIter.next();
switch (op.opcode) {
case '+':
assem.append(bankIter.take(op.chars));
break;
case '-':
strIter.skip(op.chars);
break;
case '=':
assem.append(strIter.take(op.chars));
break;
}
}
assem.append(strIter.take(strIter.remaining()));
return assem.toString();
};
/**
* applies a changeset on an array of lines
* @param CS {Changeset} the changeset to be applied
* @param lines The lines to which the changeset needs to be applied
*/
exports.mutateTextLines = function (cs, lines) {
var unpacked = exports.unpack(cs);
var csIter = exports.opIterator(unpacked.ops);
var bankIter = exports.stringIterator(unpacked.charBank);
var mut = exports.textLinesMutator(lines);
while (csIter.hasNext()) {
var op = csIter.next();
switch (op.opcode) {
case '+':
mut.insert(bankIter.take(op.chars), op.lines);
break;
case '-':
mut.remove(op.chars, op.lines);
break;
case '=':
mut.skip(op.chars, op.lines, ( !! op.attribs));
break;
}
}
mut.close();
};
/**
* Composes two attribute strings (see below) into one.
* @param att1 {string} first attribute string
* @param att2 {string} second attribue string
* @param resultIsMutaton {boolean}
* @param pool {AttribPool} attribute pool
*/
exports.composeAttributes = function (att1, att2, resultIsMutation, pool) {
// att1 and att2 are strings like "*3*f*1c", asMutation is a boolean.
// Sometimes attribute (key,value) pairs are treated as attribute presence
// information, while other times they are treated as operations that
// mutate a set of attributes, and this affects whether an empty value
// is a deletion or a change.
// Examples, of the form (att1Items, att2Items, resultIsMutation) -> result
// ([], [(bold, )], true) -> [(bold, )]
// ([], [(bold, )], false) -> []
// ([], [(bold, true)], true) -> [(bold, true)]
// ([], [(bold, true)], false) -> [(bold, true)]
// ([(bold, true)], [(bold, )], true) -> [(bold, )]
// ([(bold, true)], [(bold, )], false) -> []
// pool can be null if att2 has no attributes.
if ((!att1) && resultIsMutation) {
// In the case of a mutation (i.e. composing two exportss),
// an att2 composed with an empy att1 is just att2. If att1
// is part of an attribution string, then att2 may remove
// attributes that are already gone, so don't do this optimization.
return att2;
}
if (!att2) return att1;
var atts = [];
att1.replace(/\*([0-9a-z]+)/g, function (_, a) {
atts.push(pool.getAttrib(exports.parseNum(a)));
return '';
});
att2.replace(/\*([0-9a-z]+)/g, function (_, a) {
var pair = pool.getAttrib(exports.parseNum(a));
var found = false;
for (var i = 0; i < atts.length; i++) {
var oldPair = atts[i];
if (oldPair[0] == pair[0]) {
if (pair[1] || resultIsMutation) {
oldPair[1] = pair[1];
} else {
atts.splice(i, 1);
}
found = true;
break;
}
}
if ((!found) && (pair[1] || resultIsMutation)) {
atts.push(pair);
}
return '';
});
atts.sort();
var buf = exports.stringAssembler();
for (var i = 0; i < atts.length; i++) {
buf.append('*');
buf.append(exports.numToString(pool.putAttrib(atts[i])));
}
//print(att1+" / "+att2+" / "+buf.toString());
return buf.toString();
};
/**
* Function used as parameter for applyZip to apply a Changeset to an
* attribute
*/
exports._slicerZipperFunc = function (attOp, csOp, opOut, pool) {
// attOp is the op from the sequence that is being operated on, either an
// attribution string or the earlier of two exportss being composed.
// pool can be null if definitely not needed.
//print(csOp.toSource()+" "+attOp.toSource()+" "+opOut.toSource());
if (attOp.opcode == '-') {
exports.copyOp(attOp, opOut);
attOp.opcode = '';
} else if (!attOp.opcode) {
exports.copyOp(csOp, opOut);
csOp.opcode = '';
} else {
switch (csOp.opcode) {
case '-':
{
if (csOp.chars <= attOp.chars) {
// delete or delete part
if (attOp.opcode == '=') {
opOut.opcode = '-';
opOut.chars = csOp.chars;
opOut.lines = csOp.lines;
opOut.attribs = '';
}
attOp.chars -= csOp.chars;
attOp.lines -= csOp.lines;
csOp.opcode = '';
if (!attOp.chars) {
attOp.opcode = '';
}
} else {
// delete and keep going
if (attOp.opcode == '=') {
opOut.opcode = '-';
opOut.chars = attOp.chars;
opOut.lines = attOp.lines;
opOut.attribs = '';
}
csOp.chars -= attOp.chars;
csOp.lines -= attOp.lines;
attOp.opcode = '';
}
break;
}
case '+':
{
// insert
exports.copyOp(csOp, opOut);
csOp.opcode = '';
break;
}
case '=':
{
if (csOp.chars <= attOp.chars) {
// keep or keep part
opOut.opcode = attOp.opcode;
opOut.chars = csOp.chars;
opOut.lines = csOp.lines;
opOut.attribs = exports.composeAttributes(attOp.attribs, csOp.attribs, attOp.opcode == '=', pool);
csOp.opcode = '';
attOp.chars -= csOp.chars;
attOp.lines -= csOp.lines;
if (!attOp.chars) {
attOp.opcode = '';
}
} else {
// keep and keep going
opOut.opcode = attOp.opcode;
opOut.chars = attOp.chars;
opOut.lines = attOp.lines;
opOut.attribs = exports.composeAttributes(attOp.attribs, csOp.attribs, attOp.opcode == '=', pool);
attOp.opcode = '';
csOp.chars -= attOp.chars;
csOp.lines -= attOp.lines;
}
break;
}
case '':
{
exports.copyOp(attOp, opOut);
attOp.opcode = '';
break;
}
}
}
};
/**
* Applies a Changeset to the attribs string of a AText.
* @param cs {string} Changeset
* @param astr {string} the attribs string of a AText
* @param pool {AttribsPool} the attibutes pool
*/
exports.applyToAttribution = function (cs, astr, pool) {
var unpacked = exports.unpack(cs);
return exports.applyZip(astr, 0, unpacked.ops, 0, function (op1, op2, opOut) {
return exports._slicerZipperFunc(op1, op2, opOut, pool);
});
};
/*exports.oneInsertedLineAtATimeOpIterator = function(opsStr, optStartIndex, charBank) {
var iter = exports.opIterator(opsStr, optStartIndex);
var bankIndex = 0;
};*/
exports.mutateAttributionLines = function (cs, lines, pool) {
//dmesg(cs);
//dmesg(lines.toSource()+" ->");
var unpacked = exports.unpack(cs);
var csIter = exports.opIterator(unpacked.ops);
var csBank = unpacked.charBank;
var csBankIndex = 0;
// treat the attribution lines as text lines, mutating a line at a time
var mut = exports.textLinesMutator(lines);
var lineIter = null;
function isNextMutOp() {
return (lineIter && lineIter.hasNext()) || mut.hasMore();
}
function nextMutOp(destOp) {
if ((!(lineIter && lineIter.hasNext())) && mut.hasMore()) {
var line = mut.removeLines(1);
lineIter = exports.opIterator(line);
}
if (lineIter && lineIter.hasNext()) {
lineIter.next(destOp);
} else {
destOp.opcode = '';
}
}
var lineAssem = null;
function outputMutOp(op) {
//print("outputMutOp: "+op.toSource());
if (!lineAssem) {
lineAssem = exports.mergingOpAssembler();
}
lineAssem.append(op);
if (op.lines > 0) {
exports.assert(op.lines == 1, "Can't have op.lines of ", op.lines, " in attribution lines");
// ship it to the mut
mut.insert(lineAssem.toString(), 1);
lineAssem = null;
}
}
var csOp = exports.newOp();
var attOp = exports.newOp();
var opOut = exports.newOp();
while (csOp.opcode || csIter.hasNext() || attOp.opcode || isNextMutOp()) {
if ((!csOp.opcode) && csIter.hasNext()) {
csIter.next(csOp);
}
//print(csOp.toSource()+" "+attOp.toSource()+" "+opOut.toSource());
//print(csOp.opcode+"/"+csOp.lines+"/"+csOp.attribs+"/"+lineAssem+"/"+lineIter+"/"+(lineIter?lineIter.hasNext():null));
//print("csOp: "+csOp.toSource());
if ((!csOp.opcode) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
break; // done
} else if (csOp.opcode == '=' && csOp.lines > 0 && (!csOp.attribs) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
// skip multiple lines; this is what makes small changes not order of the document size
mut.skipLines(csOp.lines);
//print("skipped: "+csOp.lines);
csOp.opcode = '';
} else if (csOp.opcode == '+') {
if (csOp.lines > 1) {
var firstLineLen = csBank.indexOf('\n', csBankIndex) + 1 - csBankIndex;
exports.copyOp(csOp, opOut);
csOp.chars -= firstLineLen;
csOp.lines--;
opOut.lines = 1;
opOut.chars = firstLineLen;
} else {
exports.copyOp(csOp, opOut);
csOp.opcode = '';
}
outputMutOp(opOut);
csBankIndex += opOut.chars;
opOut.opcode = '';
} else {
if ((!attOp.opcode) && isNextMutOp()) {
nextMutOp(attOp);
}
//print("attOp: "+attOp.toSource());
exports._slicerZipperFunc(attOp, csOp, opOut, pool);
if (opOut.opcode) {
outputMutOp(opOut);
opOut.opcode = '';
}
}
}
exports.assert(!lineAssem, "line assembler not finished");
mut.close();
//dmesg("-> "+lines.toSource());
};
/**
* joins several Attribution lines
* @param theAlines collection of Attribution lines
* @returns {string} joined Attribution lines
*/
exports.joinAttributionLines = function (theAlines) {
var assem = exports.mergingOpAssembler();
for (var i = 0; i < theAlines.length; i++) {
var aline = theAlines[i];
var iter = exports.opIterator(aline);
while (iter.hasNext()) {
assem.append(iter.next());
}
}
return assem.toString();
};
exports.splitAttributionLines = function (attrOps, text) {
var iter = exports.opIterator(attrOps);
var assem = exports.mergingOpAssembler();
var lines = [];
var pos = 0;
function appendOp(op) {
assem.append(op);
if (op.lines > 0) {
lines.push(assem.toString());
assem.clear();
}
pos += op.chars;
}
while (iter.hasNext()) {
var op = iter.next();
var numChars = op.chars;
var numLines = op.lines;
while (numLines > 1) {
var newlineEnd = text.indexOf('\n', pos) + 1;
exports.assert(newlineEnd > 0, "newlineEnd <= 0 in splitAttributionLines");
op.chars = newlineEnd - pos;
op.lines = 1;
appendOp(op);
numChars -= op.chars;
numLines -= op.lines;
}
if (numLines == 1) {
op.chars = numChars;
op.lines = 1;
}
appendOp(op);
}
return lines;
};
/**
* splits text into lines
* @param {string} text to be splitted
*/
exports.splitTextLines = function (text) {
return text.match(/[^\n]*(?:\n|[^\n]$)/g);
};
/**
* compose two Changesets
* @param cs1 {Changeset} first Changeset
* @param cs2 {Changeset} second Changeset
* @param pool {AtribsPool} Attribs pool
*/
exports.compose = function (cs1, cs2, pool) {
var unpacked1 = exports.unpack(cs1);
var unpacked2 = exports.unpack(cs2);
var len1 = unpacked1.oldLen;
var len2 = unpacked1.newLen;
exports.assert(len2 == unpacked2.oldLen, "mismatched composition");
var len3 = unpacked2.newLen;
var bankIter1 = exports.stringIterator(unpacked1.charBank);
var bankIter2 = exports.stringIterator(unpacked2.charBank);
var bankAssem = exports.stringAssembler();
var newOps = exports.applyZip(unpacked1.ops, 0, unpacked2.ops, 0, function (op1, op2, opOut) {
//var debugBuilder = exports.stringAssembler();
//debugBuilder.append(exports.opString(op1));
//debugBuilder.append(',');
//debugBuilder.append(exports.opString(op2));
//debugBuilder.append(' / ');
var op1code = op1.opcode;
var op2code = op2.opcode;
if (op1code == '+' && op2code == '-') {
bankIter1.skip(Math.min(op1.chars, op2.chars));
}
exports._slicerZipperFunc(op1, op2, opOut, pool);
if (opOut.opcode == '+') {
if (op2code == '+') {
bankAssem.append(bankIter2.take(opOut.chars));
} else {
bankAssem.append(bankIter1.take(opOut.chars));
}
}
//debugBuilder.append(exports.opString(op1));
//debugBuilder.append(',');
//debugBuilder.append(exports.opString(op2));
//debugBuilder.append(' -> ');
//debugBuilder.append(exports.opString(opOut));
//print(debugBuilder.toString());
});
return exports.pack(len1, len3, newOps, bankAssem.toString());
};
/**
* returns a function that tests if a string of attributes
* (e.g. *3*4) contains a given attribute key,value that
* is already present in the pool.
* @param attribPair array [key,value] of the attribute
* @param pool {AttribPool} Attribute pool
*/
exports.attributeTester = function (attribPair, pool) {
if (!pool) {
return never;
}
var attribNum = pool.putAttrib(attribPair, true);
if (attribNum < 0) {
return never;
} else {
var re = new RegExp('\\*' + exports.numToString(attribNum) + '(?!\\w)');
return function (attribs) {
return re.test(attribs);
};
}
function never(attribs) {
return false;
}
};
/**
* creates the identity Changeset of length N
* @param N {int} length of the identity changeset
*/
exports.identity = function (N) {
return exports.pack(N, N, "", "");
};
/**
* creates a Changeset which works on oldFullText and removes text
* from spliceStart to spliceStart+numRemoved and inserts newText
* instead. Also gives possibility to add attributes optNewTextAPairs
* for the new text.
* @param oldFullText {string} old text
* @param spliecStart {int} where splicing starts
* @param numRemoved {int} number of characters to be removed
* @param newText {string} string to be inserted
* @param optNewTextAPairs {string} new pairs to be inserted
* @param pool {AttribPool} Attribution Pool
*/
exports.makeSplice = function (oldFullText, spliceStart, numRemoved, newText, optNewTextAPairs, pool) {
var oldLen = oldFullText.length;
if (spliceStart >= oldLen) {
spliceStart = oldLen - 1;
}
if (numRemoved > oldFullText.length - spliceStart - 1) {
numRemoved = oldFullText.length - spliceStart - 1;
}
var oldText = oldFullText.substring(spliceStart, spliceStart + numRemoved);
var newLen = oldLen + newText.length - oldText.length;
var assem = exports.smartOpAssembler();
assem.appendOpWithText('=', oldFullText.substring(0, spliceStart));
assem.appendOpWithText('-', oldText);
assem.appendOpWithText('+', newText, optNewTextAPairs, pool);
assem.endDocument();
return exports.pack(oldLen, newLen, assem.toString(), newText);
};
/**
* Transforms a changeset into a list of splices in the form
* [startChar, endChar, newText] meaning replace text from
* startChar to endChar with newText
* @param cs Changeset
*/
exports.toSplices = function (cs) {
//
var unpacked = exports.unpack(cs);
var splices = [];
var oldPos = 0;
var iter = exports.opIterator(unpacked.ops);
var charIter = exports.stringIterator(unpacked.charBank);
var inSplice = false;
while (iter.hasNext()) {
var op = iter.next();
if (op.opcode == '=') {
oldPos += op.chars;
inSplice = false;
} else {
if (!inSplice) {
splices.push([oldPos, oldPos, ""]);
inSplice = true;
}
if (op.opcode == '-') {
oldPos += op.chars;
splices[splices.length - 1][1] += op.chars;
} else if (op.opcode == '+') {
splices[splices.length - 1][2] += charIter.take(op.chars);
}
}
}
return splices;
};
/**
*
*/
exports.characterRangeFollow = function (cs, startChar, endChar, insertionsAfter) {
var newStartChar = startChar;
var newEndChar = endChar;
var splices = exports.toSplices(cs);
var lengthChangeSoFar = 0;
for (var i = 0; i < splices.length; i++) {
var splice = splices[i];
var spliceStart = splice[0] + lengthChangeSoFar;
var spliceEnd = splice[1] + lengthChangeSoFar;
var newTextLength = splice[2].length;
var thisLengthChange = newTextLength - (spliceEnd - spliceStart);
if (spliceStart <= newStartChar && spliceEnd >= newEndChar) {
// splice fully replaces/deletes range
// (also case that handles insertion at a collapsed selection)
if (insertionsAfter) {
newStartChar = newEndChar = spliceStart;
} else {
newStartChar = newEndChar = spliceStart + newTextLength;
}
} else if (spliceEnd <= newStartChar) {
// splice is before range
newStartChar += thisLengthChange;
newEndChar += thisLengthChange;
} else if (spliceStart >= newEndChar) {
// splice is after range
} else if (spliceStart >= newStartChar && spliceEnd <= newEndChar) {
// splice is inside range
newEndChar += thisLengthChange;
} else if (spliceEnd < newEndChar) {
// splice overlaps beginning of range
newStartChar = spliceStart + newTextLength;
newEndChar += thisLengthChange;
} else {
// splice overlaps end of range
newEndChar = spliceStart;
}
lengthChangeSoFar += thisLengthChange;
}
return [newStartChar, newEndChar];
};
/**
* Iterate over attributes in a changeset and move them from
* oldPool to newPool
* @param cs {Changeset} Chageset/attribution string to be iterated over
* @param oldPool {AttribPool} old attributes pool
* @param newPool {AttribPool} new attributes pool
* @return {string} the new Changeset
*/
exports.moveOpsToNewPool = function (cs, oldPool, newPool) {
// works on exports or attribution string
var dollarPos = cs.indexOf('$');
if (dollarPos < 0) {
dollarPos = cs.length;
}
var upToDollar = cs.substring(0, dollarPos);
var fromDollar = cs.substring(dollarPos);
// order of attribs stays the same
return upToDollar.replace(/\*([0-9a-z]+)/g, function (_, a) {
var oldNum = exports.parseNum(a);
var pair = oldPool.getAttrib(oldNum);
if(!pair) exports.error('Can\'t copy unknown attrib (reference attrib string to non-existant pool entry). Inconsistent attrib state!');
var newNum = newPool.putAttrib(pair);
return '*' + exports.numToString(newNum);
}) + fromDollar;
};
/**
* create an attribution inserting a text
* @param text {string} text to be inserted
*/
exports.makeAttribution = function (text) {
var assem = exports.smartOpAssembler();
assem.appendOpWithText('+', text);
return assem.toString();
};
/**
* Iterates over attributes in exports, attribution string, or attribs property of an op
* and runs function func on them
* @param cs {Changeset} changeset
* @param func {function} function to be called
*/
exports.eachAttribNumber = function (cs, func) {
var dollarPos = cs.indexOf('$');
if (dollarPos < 0) {
dollarPos = cs.length;
}
var upToDollar = cs.substring(0, dollarPos);
upToDollar.replace(/\*([0-9a-z]+)/g, function (_, a) {
func(exports.parseNum(a));
return '';
});
};
/**
* Filter attributes which should remain in a Changeset
* callable on a exports, attribution string, or attribs property of an op,
* though it may easily create adjacent ops that can be merged.
* @param cs {Changeset} changeset to be filtered
* @param filter {function} fnc which returns true if an
* attribute X (int) should be kept in the Changeset
*/
exports.filterAttribNumbers = function (cs, filter) {
return exports.mapAttribNumbers(cs, filter);
};
/**
* does exactly the same as exports.filterAttribNumbers
*/
exports.mapAttribNumbers = function (cs, func) {
var dollarPos = cs.indexOf('$');
if (dollarPos < 0) {
dollarPos = cs.length;
}
var upToDollar = cs.substring(0, dollarPos);
var newUpToDollar = upToDollar.replace(/\*([0-9a-z]+)/g, function (s, a) {
var n = func(exports.parseNum(a));
if (n === true) {
return s;
} else if ((typeof n) === "number") {
return '*' + exports.numToString(n);
} else {
return '';
}
});
return newUpToDollar + cs.substring(dollarPos);
};
/**
* Create a Changeset going from Identity to a certain state
* @params text {string} text of the final change
* @attribs attribs {string} optional, operations which insert
* the text and also puts the right attributes
*/
exports.makeAText = function (text, attribs) {
return {
text: text,
attribs: (attribs || exports.makeAttribution(text))
};
};
/**
* Apply a Changeset to a AText
* @param cs {Changeset} Changeset to be applied
* @param atext {AText}
* @param pool {AttribPool} Attribute Pool to add to
*/
exports.applyToAText = function (cs, atext, pool) {
return {
text: exports.applyToText(cs, atext.text),
attribs: exports.applyToAttribution(cs, atext.attribs, pool)
};
};
/**
* Clones a AText structure
* @param atext {AText}
*/
exports.cloneAText = function (atext) {
return {
text: atext.text,
attribs: atext.attribs
};
};
/**
* Copies a AText structure from atext1 to atext2
* @param atext {AText}
*/
exports.copyAText = function (atext1, atext2) {
atext2.text = atext1.text;
atext2.attribs = atext1.attribs;
};
/**
* Append the set of operations from atext to an assembler
* @param atext {AText}
* @param assem Assembler like smartOpAssembler
*/
exports.appendATextToAssembler = function (atext, assem) {
// intentionally skips last newline char of atext
var iter = exports.opIterator(atext.attribs);
var op = exports.newOp();
while (iter.hasNext()) {
iter.next(op);
if (!iter.hasNext()) {
// last op, exclude final newline
if (op.lines <= 1) {
op.lines = 0;
op.chars--;
if (op.chars) {
assem.append(op);
}
} else {
var nextToLastNewlineEnd =
atext.text.lastIndexOf('\n', atext.text.length - 2) + 1;
var lastLineLength = atext.text.length - nextToLastNewlineEnd - 1;
op.lines--;
op.chars -= (lastLineLength + 1);
assem.append(op);
op.lines = 0;
op.chars = lastLineLength;
if (op.chars) {
assem.append(op);
}
}
} else {
assem.append(op);
}
}
};
/**
* Creates a clone of a Changeset and it's APool
* @param cs {Changeset}
* @param pool {AtributePool}
*/
exports.prepareForWire = function (cs, pool) {
var newPool = new AttributePool();
var newCs = exports.moveOpsToNewPool(cs, pool, newPool);
return {
translated: newCs,
pool: newPool
};
};
/**
* Checks if a changeset s the identity changeset
*/
exports.isIdentity = function (cs) {
var unpacked = exports.unpack(cs);
return unpacked.ops == "" && unpacked.oldLen == unpacked.newLen;
};
/**
* returns all the values of attributes with a certain key
* in an Op attribs string
* @param attribs {string} Attribute string of a Op
* @param key {string} string to be seached for
* @param pool {AttribPool} attribute pool
*/
exports.opAttributeValue = function (op, key, pool) {
return exports.attribsAttributeValue(op.attribs, key, pool);
};
/**
* returns all the values of attributes with a certain key
* in an attribs string
* @param attribs {string} Attribute string
* @param key {string} string to be seached for
* @param pool {AttribPool} attribute pool
*/
exports.attribsAttributeValue = function (attribs, key, pool) {
var value = '';
if (attribs) {
exports.eachAttribNumber(attribs, function (n) {
if (pool.getAttribKey(n) == key) {
value = pool.getAttribValue(n);
}
});
}
return value;
};
/**
* Creates a Changeset builder for a string with initial
* length oldLen. Allows to add/remove parts of it
* @param oldLen {int} Old length
*/
exports.builder = function (oldLen) {
var assem = exports.smartOpAssembler();
var o = exports.newOp();
var charBank = exports.stringAssembler();
var self = {
// attribs are [[key1,value1],[key2,value2],...] or '*0*1...' (no pool needed in latter case)
keep: function (N, L, attribs, pool) {
o.opcode = '=';
o.attribs = (attribs && exports.makeAttribsString('=', attribs, pool)) || '';
o.chars = N;
o.lines = (L || 0);
assem.append(o);
return self;
},
keepText: function (text, attribs, pool) {
assem.appendOpWithText('=', text, attribs, pool);
return self;
},
insert: function (text, attribs, pool) {
assem.appendOpWithText('+', text, attribs, pool);
charBank.append(text);
return self;
},
remove: function (N, L) {
o.opcode = '-';
o.attribs = '';
o.chars = N;
o.lines = (L || 0);
assem.append(o);
return self;
},
toString: function () {
assem.endDocument();
var newLen = oldLen + assem.getLengthChange();
return exports.pack(oldLen, newLen, assem.toString(), charBank.toString());
}
};
return self;
};
exports.makeAttribsString = function (opcode, attribs, pool) {
// makeAttribsString(opcode, '*3') or makeAttribsString(opcode, [['foo','bar']], myPool) work
if (!attribs) {
return '';
} else if ((typeof attribs) == "string") {
return attribs;
} else if (pool && attribs && attribs.length) {
if (attribs.length > 1) {
attribs = attribs.slice();
attribs.sort();
}
var result = [];
for (var i = 0; i < attribs.length; i++) {
var pair = attribs[i];
if (opcode == '=' || (opcode == '+' && pair[1])) {
result.push('*' + exports.numToString(pool.putAttrib(pair)));
}
}
return result.join('');
}
};
// like "substring" but on a single-line attribution string
exports.subattribution = function (astr, start, optEnd) {
var iter = exports.opIterator(astr, 0);
var assem = exports.smartOpAssembler();
var attOp = exports.newOp();
var csOp = exports.newOp();
var opOut = exports.newOp();
function doCsOp() {
if (csOp.chars) {
while (csOp.opcode && (attOp.opcode || iter.hasNext())) {
if (!attOp.opcode) iter.next(attOp);
if (csOp.opcode && attOp.opcode && csOp.chars >= attOp.chars && attOp.lines > 0 && csOp.lines <= 0) {
csOp.lines++;
}
exports._slicerZipperFunc(attOp, csOp, opOut, null);
if (opOut.opcode) {
assem.append(opOut);
opOut.opcode = '';
}
}
}
}
csOp.opcode = '-';
csOp.chars = start;
doCsOp();
if (optEnd === undefined) {
if (attOp.opcode) {
assem.append(attOp);
}
while (iter.hasNext()) {
iter.next(attOp);
assem.append(attOp);
}
} else {
csOp.opcode = '=';
csOp.chars = optEnd - start;
doCsOp();
}
return assem.toString();
};
exports.inverse = function (cs, lines, alines, pool) {
// lines and alines are what the exports is meant to apply to.
// They may be arrays or objects with .get(i) and .length methods.
// They include final newlines on lines.
function lines_get(idx) {
if (lines.get) {
return lines.get(idx);
} else {
return lines[idx];
}
}
function lines_length() {
if ((typeof lines.length) == "number") {
return lines.length;
} else {
return lines.length();
}
}
function alines_get(idx) {
if (alines.get) {
return alines.get(idx);
} else {
return alines[idx];
}
}
function alines_length() {
if ((typeof alines.length) == "number") {
return alines.length;
} else {
return alines.length();
}
}
var curLine = 0;
var curChar = 0;
var curLineOpIter = null;
var curLineOpIterLine;
var curLineNextOp = exports.newOp('+');
var unpacked = exports.unpack(cs);
var csIter = exports.opIterator(unpacked.ops);
var builder = exports.builder(unpacked.newLen);
function consumeAttribRuns(numChars, func /*(len, attribs, endsLine)*/ ) {
if ((!curLineOpIter) || (curLineOpIterLine != curLine)) {
// create curLineOpIter and advance it to curChar
curLineOpIter = exports.opIterator(alines_get(curLine));
curLineOpIterLine = curLine;
var indexIntoLine = 0;
var done = false;
while (!done) {
curLineOpIter.next(curLineNextOp);
if (indexIntoLine + curLineNextOp.chars >= curChar) {
curLineNextOp.chars -= (curChar - indexIntoLine);
done = true;
} else {
indexIntoLine += curLineNextOp.chars;
}
}
}
while (numChars > 0) {
if ((!curLineNextOp.chars) && (!curLineOpIter.hasNext())) {
curLine++;
curChar = 0;
curLineOpIterLine = curLine;
curLineNextOp.chars = 0;
curLineOpIter = exports.opIterator(alines_get(curLine));
}
if (!curLineNextOp.chars) {
curLineOpIter.next(curLineNextOp);
}
var charsToUse = Math.min(numChars, curLineNextOp.chars);
func(charsToUse, curLineNextOp.attribs, charsToUse == curLineNextOp.chars && curLineNextOp.lines > 0);
numChars -= charsToUse;
curLineNextOp.chars -= charsToUse;
curChar += charsToUse;
}
if ((!curLineNextOp.chars) && (!curLineOpIter.hasNext())) {
curLine++;
curChar = 0;
}
}
function skip(N, L) {
if (L) {
curLine += L;
curChar = 0;
} else {
if (curLineOpIter && curLineOpIterLine == curLine) {
consumeAttribRuns(N, function () {});
} else {
curChar += N;
}
}
}
function nextText(numChars) {
var len = 0;
var assem = exports.stringAssembler();
var firstString = lines_get(curLine).substring(curChar);
len += firstString.length;
assem.append(firstString);
var lineNum = curLine + 1;
while (len < numChars) {
var nextString = lines_get(lineNum);
len += nextString.length;
assem.append(nextString);
lineNum++;
}
return assem.toString().substring(0, numChars);
}
function cachedStrFunc(func) {
var cache = {};
return function (s) {
if (!cache[s]) {
cache[s] = func(s);
}
return cache[s];
};
}
var attribKeys = [];
var attribValues = [];
while (csIter.hasNext()) {
var csOp = csIter.next();
if (csOp.opcode == '=') {
if (csOp.attribs) {
attribKeys.length = 0;
attribValues.length = 0;
exports.eachAttribNumber(csOp.attribs, function (n) {
attribKeys.push(pool.getAttribKey(n));
attribValues.push(pool.getAttribValue(n));
});
var undoBackToAttribs = cachedStrFunc(function (attribs) {
var backAttribs = [];
for (var i = 0; i < attribKeys.length; i++) {
var appliedKey = attribKeys[i];
var appliedValue = attribValues[i];
var oldValue = exports.attribsAttributeValue(attribs, appliedKey, pool);
if (appliedValue != oldValue) {
backAttribs.push([appliedKey, oldValue]);
}
}
return exports.makeAttribsString('=', backAttribs, pool);
});
consumeAttribRuns(csOp.chars, function (len, attribs, endsLine) {
builder.keep(len, endsLine ? 1 : 0, undoBackToAttribs(attribs));
});
} else {
skip(csOp.chars, csOp.lines);
builder.keep(csOp.chars, csOp.lines);
}
} else if (csOp.opcode == '+') {
builder.remove(csOp.chars, csOp.lines);
} else if (csOp.opcode == '-') {
var textBank = nextText(csOp.chars);
var textBankIndex = 0;
consumeAttribRuns(csOp.chars, function (len, attribs, endsLine) {
builder.insert(textBank.substr(textBankIndex, len), attribs);
textBankIndex += len;
});
}
}
return exports.checkRep(builder.toString());
};
// %CLIENT FILE ENDS HERE%
exports.follow = function (cs1, cs2, reverseInsertOrder, pool) {
var unpacked1 = exports.unpack(cs1);
var unpacked2 = exports.unpack(cs2);
var len1 = unpacked1.oldLen;
var len2 = unpacked2.oldLen;
exports.assert(len1 == len2, "mismatched follow");
var chars1 = exports.stringIterator(unpacked1.charBank);
var chars2 = exports.stringIterator(unpacked2.charBank);
var oldLen = unpacked1.newLen;
var oldPos = 0;
var newLen = 0;
var hasInsertFirst = exports.attributeTester(['insertorder', 'first'], pool);
var newOps = exports.applyZip(unpacked1.ops, 0, unpacked2.ops, 0, function (op1, op2, opOut) {
if (op1.opcode == '+' || op2.opcode == '+') {
var whichToDo;
if (op2.opcode != '+') {
whichToDo = 1;
} else if (op1.opcode != '+') {
whichToDo = 2;
} else {
// both +
var firstChar1 = chars1.peek(1);
var firstChar2 = chars2.peek(1);
var insertFirst1 = hasInsertFirst(op1.attribs);
var insertFirst2 = hasInsertFirst(op2.attribs);
if (insertFirst1 && !insertFirst2) {
whichToDo = 1;
} else if (insertFirst2 && !insertFirst1) {
whichToDo = 2;
}
// insert string that doesn't start with a newline first so as not to break up lines
else if (firstChar1 == '\n' && firstChar2 != '\n') {
whichToDo = 2;
} else if (firstChar1 != '\n' && firstChar2 == '\n') {
whichToDo = 1;
}
// break symmetry:
else if (reverseInsertOrder) {
whichToDo = 2;
} else {
whichToDo = 1;
}
}
if (whichToDo == 1) {
chars1.skip(op1.chars);
opOut.opcode = '=';
opOut.lines = op1.lines;
opOut.chars = op1.chars;
opOut.attribs = '';
op1.opcode = '';
} else {
// whichToDo == 2
chars2.skip(op2.chars);
exports.copyOp(op2, opOut);
op2.opcode = '';
}
} else if (op1.opcode == '-') {
if (!op2.opcode) {
op1.opcode = '';
} else {
if (op1.chars <= op2.chars) {
op2.chars -= op1.chars;
op2.lines -= op1.lines;
op1.opcode = '';
if (!op2.chars) {
op2.opcode = '';
}
} else {
op1.chars -= op2.chars;
op1.lines -= op2.lines;
op2.opcode = '';
}
}
} else if (op2.opcode == '-') {
exports.copyOp(op2, opOut);
if (!op1.opcode) {
op2.opcode = '';
} else if (op2.chars <= op1.chars) {
// delete part or all of a keep
op1.chars -= op2.chars;
op1.lines -= op2.lines;
op2.opcode = '';
if (!op1.chars) {
op1.opcode = '';
}
} else {
// delete all of a keep, and keep going
opOut.lines = op1.lines;
opOut.chars = op1.chars;
op2.lines -= op1.lines;
op2.chars -= op1.chars;
op1.opcode = '';
}
} else if (!op1.opcode) {
exports.copyOp(op2, opOut);
op2.opcode = '';
} else if (!op2.opcode) {
// @NOTE: Critical bugfix for EPL issue #1625. We do not copy op1 here
// in order to prevent attributes from leaking into result changesets.
// exports.copyOp(op1, opOut);
op1.opcode = '';
} else {
// both keeps
opOut.opcode = '=';
opOut.attribs = exports.followAttributes(op1.attribs, op2.attribs, pool);
if (op1.chars <= op2.chars) {
opOut.chars = op1.chars;
opOut.lines = op1.lines;
op2.chars -= op1.chars;
op2.lines -= op1.lines;
op1.opcode = '';
if (!op2.chars) {
op2.opcode = '';
}
} else {
opOut.chars = op2.chars;
opOut.lines = op2.lines;
op1.chars -= op2.chars;
op1.lines -= op2.lines;
op2.opcode = '';
}
}
switch (opOut.opcode) {
case '=':
oldPos += opOut.chars;
newLen += opOut.chars;
break;
case '-':
oldPos += opOut.chars;
break;
case '+':
newLen += opOut.chars;
break;
}
});
newLen += oldLen - oldPos;
return exports.pack(oldLen, newLen, newOps, unpacked2.charBank);
};
exports.followAttributes = function (att1, att2, pool) {
// The merge of two sets of attribute changes to the same text
// takes the lexically-earlier value if there are two values
// for the same key. Otherwise, all key/value changes from
// both attribute sets are taken. This operation is the "follow",
// so a set of changes is produced that can be applied to att1
// to produce the merged set.
if ((!att2) || (!pool)) return '';
if (!att1) return att2;
var atts = [];
att2.replace(/\*([0-9a-z]+)/g, function (_, a) {
atts.push(pool.getAttrib(exports.parseNum(a)));
return '';
});
att1.replace(/\*([0-9a-z]+)/g, function (_, a) {
var pair1 = pool.getAttrib(exports.parseNum(a));
for (var i = 0; i < atts.length; i++) {
var pair2 = atts[i];
if (pair1[0] == pair2[0]) {
if (pair1[1] <= pair2[1]) {
// winner of merge is pair1, delete this attribute
atts.splice(i, 1);
}
break;
}
}
return '';
});
// we've only removed attributes, so they're already sorted
var buf = exports.stringAssembler();
for (var i = 0; i < atts.length; i++) {
buf.append('*');
buf.append(exports.numToString(pool.putAttrib(atts[i])));
}
return buf.toString();
};
exports.composeWithDeletions = function (cs1, cs2, pool) {
var unpacked1 = exports.unpack(cs1);
var unpacked2 = exports.unpack(cs2);
var len1 = unpacked1.oldLen;
var len2 = unpacked1.newLen;
exports.assert(len2 == unpacked2.oldLen, "mismatched composition");
var len3 = unpacked2.newLen;
var bankIter1 = exports.stringIterator(unpacked1.charBank);
var bankIter2 = exports.stringIterator(unpacked2.charBank);
var bankAssem = exports.stringAssembler();
var newOps = exports.applyZip(unpacked1.ops, 0, unpacked2.ops, 0, function (op1, op2, opOut) {
var op1code = op1.opcode;
var op2code = op2.opcode;
if (op1code == '+' && op2code == '-') {
bankIter1.skip(Math.min(op1.chars, op2.chars));
}
exports._slicerZipperFuncWithDeletions(op1, op2, opOut, pool);
if (opOut.opcode == '+') {
if (op2code == '+') {
bankAssem.append(bankIter2.take(opOut.chars));
} else {
bankAssem.append(bankIter1.take(opOut.chars));
}
}
});
return exports.pack(len1, len3, newOps, bankAssem.toString());
};
// This function is 95% like _slicerZipperFunc, we just changed two lines to ensure it merges the attribs of deletions properly.
// This is necassary for correct paddiff. But to ensure these changes doesn't affect anything else, we've created a seperate function only used for paddiffs
exports._slicerZipperFuncWithDeletions= function (attOp, csOp, opOut, pool) {
// attOp is the op from the sequence that is being operated on, either an
// attribution string or the earlier of two exportss being composed.
// pool can be null if definitely not needed.
//print(csOp.toSource()+" "+attOp.toSource()+" "+opOut.toSource());
if (attOp.opcode == '-') {
exports.copyOp(attOp, opOut);
attOp.opcode = '';
} else if (!attOp.opcode) {
exports.copyOp(csOp, opOut);
csOp.opcode = '';
} else {
switch (csOp.opcode) {
case '-':
{
if (csOp.chars <= attOp.chars) {
// delete or delete part
if (attOp.opcode == '=') {
opOut.opcode = '-';
opOut.chars = csOp.chars;
opOut.lines = csOp.lines;
opOut.attribs = csOp.attribs; //changed by yammer
}
attOp.chars -= csOp.chars;
attOp.lines -= csOp.lines;
csOp.opcode = '';
if (!attOp.chars) {
attOp.opcode = '';
}
} else {
// delete and keep going
if (attOp.opcode == '=') {
opOut.opcode = '-';
opOut.chars = attOp.chars;
opOut.lines = attOp.lines;
opOut.attribs = csOp.attribs; //changed by yammer
}
csOp.chars -= attOp.chars;
csOp.lines -= attOp.lines;
attOp.opcode = '';
}
break;
}
case '+':
{
// insert
exports.copyOp(csOp, opOut);
csOp.opcode = '';
break;
}
case '=':
{
if (csOp.chars <= attOp.chars) {
// keep or keep part
opOut.opcode = attOp.opcode;
opOut.chars = csOp.chars;
opOut.lines = csOp.lines;
opOut.attribs = exports.composeAttributes(attOp.attribs, csOp.attribs, attOp.opcode == '=', pool);
csOp.opcode = '';
attOp.chars -= csOp.chars;
attOp.lines -= csOp.lines;
if (!attOp.chars) {
attOp.opcode = '';
}
} else {
// keep and keep going
opOut.opcode = attOp.opcode;
opOut.chars = attOp.chars;
opOut.lines = attOp.lines;
opOut.attribs = exports.composeAttributes(attOp.attribs, csOp.attribs, attOp.opcode == '=', pool);
attOp.opcode = '';
csOp.chars -= attOp.chars;
csOp.lines -= attOp.lines;
}
break;
}
case '':
{
exports.copyOp(attOp, opOut);
attOp.opcode = '';
break;
}
}
}
};
|