File: scorediff.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 210,672 kB
  • sloc: cpp: 291,093; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 240; perl: 82; pascal: 79
file content (1453 lines) | stat: -rw-r--r-- 55,314 bytes parent folder | download | duplicates (5)
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
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2018 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "scorediff.h"

#include "duration.h"
#include "measure.h"
#include "score.h"
#include "staff.h"
#include "xml.h"

#include "dtl/dtl.hpp"

#include <algorithm>
#include <utility>

namespace Ms {

//---------------------------------------------------------
//   MscxModeDiff
//---------------------------------------------------------

class MscxModeDiff {
      static constexpr const char* tagRegExpStr = "</?(?<name>[A-z_][A-z_0-9\\-.:]*)( [A-z_0-9\\-.:\\s=\"]*)?/?>";
      const QRegularExpression tagRegExp;

      enum TagType { START_TAG, END_TAG };
      struct Tag {
            int line;
            TagType type;
            QString name;

            Tag(int l, TagType t, const QString& n) : line(l), type(t), name(n) {}
            };

      static DiffType fromDtlDiffType(dtl::edit_t dtlType);

      void adjustSemanticsMscx(std::vector<TextDiff>&);
      int adjustSemanticsMscxOneDiff(std::vector<TextDiff>& diffs, int index);
      int nextDiffOnShiftIndex(const std::vector<TextDiff>& diffs, int index, bool down);
      static QString getOuterLines(const QString& str, int lines, bool start);
      bool assessShiftDiff(const std::vector<TextDiff>&, const std::vector<Tag>& extraTags, int index, int lines);
      int performShiftDiff(std::vector<TextDiff>&, int index, int lines);
      void readMscx(const QString& xml, std::vector<Tag>& extraTags);
      void handleTag(int line, TagType type, const QString& name, std::vector<Tag>& extraTags);

   public:
      MscxModeDiff();

      std::vector<TextDiff> lineModeDiff(const QString& s1, const QString& s2);
      std::vector<TextDiff> mscxModeDiff(const QString& s1, const QString& s2);
      };

//---------------------------------------------------------
//   TextDiffParser
//    Used by ScoreDiff class during parsing MSCX text diff
//---------------------------------------------------------

class TextDiffParser {
      const int iScore;
      const int iOtherScore;

      int tagLevel = 0;
      int currentDiffTagLevel = 0;
      std::vector<const ScoreElement*> contextsStack;
      std::vector<bool> tagIsElement;
      const ScoreElement* lastElementEnded = nullptr;
      QString markupContext;

      BaseDiff* handleToken(const QXmlStreamReader& r, const ScoreElement* newElement, bool saveDiff);
      bool insideDiffTag() const { return currentDiffTagLevel != 0; }

   public:
      TextDiffParser(int iScore);

      void makeDiffs(const QString& mscx, const std::vector<std::pair<const ScoreElement*, QString>>& elements, const std::vector<TextDiff>& textDiffs, std::vector<BaseDiff*>& diffs);
      };

//---------------------------------------------------------
//   MscxModeDiff::MscxModeDiff
//---------------------------------------------------------

MscxModeDiff::MscxModeDiff()
   : tagRegExp(tagRegExpStr)
      {}

//---------------------------------------------------------
//   MscxModeDiff::fromDtlDiffType
//---------------------------------------------------------

DiffType MscxModeDiff::fromDtlDiffType(dtl::edit_t dtlType)
      {
      switch(dtlType) {
            case dtl::SES_DELETE:
                  return DiffType::DELETE;
            case dtl::SES_COMMON:
                  return DiffType::EQUAL;
            case dtl::SES_ADD:
                  return DiffType::INSERT;
            }
      Q_ASSERT(false); // dtlType must have one of the values handled above.
      return DiffType::EQUAL;
      }

//---------------------------------------------------------
//   MscxModeDiff::lineModeDiff
//---------------------------------------------------------

std::vector<TextDiff> MscxModeDiff::lineModeDiff(const QString& s1, const QString& s2)
      {
      // type declarations for dtl library
      typedef QStringRef elem;
      typedef std::pair<elem, dtl::elemInfo> sesElem;
      typedef std::vector<sesElem> sesElemVec;

      // QVector does not contain range constructor used inside dtl
      // so we have to convert to std::vector.
      std::vector<QStringRef> lines1 = s1.splitRef('\n').toStdVector();
      std::vector<QStringRef> lines2 = s2.splitRef('\n').toStdVector();
      dtl::Diff<QStringRef, std::vector<QStringRef>> diff(lines1, lines2);

      diff.compose();

      const sesElemVec changes = diff.getSes().getSequence();
      std::vector<TextDiff> diffs;
      int line[2][2] {{1, 1}, {1, 1}}; // for correct assigning line numbers to
                                       // DELETE and INSERT diffs we need to
                                       // count lines separately for these diff
                                       // types (EQUAL can use both counters).

      for (const sesElem& ch : changes) {
            DiffType type = fromDtlDiffType(ch.second.type);
            const int iThis = (type == DiffType::DELETE) ? 0 : 1; // for EQUAL doesn't matter

            if (diffs.empty() || diffs.back().type != type) {
                  if (!diffs.empty()) {
                        // sync line numbers
                        DiffType prevType = diffs.back().type;
                        const int prevThis = (prevType == DiffType::DELETE) ? 0 : 1;
                        const int prevOther = (prevThis == 1) ? 0 : 1;
                        if (prevType == DiffType::EQUAL)
                              std::copy_n(line[prevThis], 2, line[prevOther]);
                        else
                              line[prevThis][prevOther] = line[prevOther][prevOther];

                        if (type == DiffType::EQUAL) {
                              const int iOther = (iThis == 1) ? 0 : 1;
                              line[iThis][iOther] = line[iOther][iOther];
                              }
                        }

                  diffs.emplace_back();
                  TextDiff& d = diffs.back();
                  d.type = type;
                  std::copy_n(line[iThis], 2, d.start);
                  d.end[0] = line[iThis][0] - 1; // equal line numbers would mean an actual change in that line.
                  d.end[1] = line[iThis][1] - 1;
                  }

            TextDiff& d = diffs.back();
            d.end[iThis] = (line[iThis][iThis]++);
            d.text[iThis].append(ch.first).append('\n');
            if (type == DiffType::EQUAL) {
                  const int iOther = (iThis == 1) ? 0 : 1;
                  d.end[iOther] = (line[iThis][iOther]++);
                  d.text[iOther].append(ch.first).append('\n');
                  }
            }

      return diffs;
      }

//---------------------------------------------------------
//   MscxModeDiff::mscxModeDiff
//---------------------------------------------------------

std::vector<TextDiff> MscxModeDiff::mscxModeDiff(const QString& s1, const QString& s2)
      {
      std::vector<TextDiff> diffs = lineModeDiff(s1, s2);
      adjustSemanticsMscx(diffs);
      return diffs;
      }

//---------------------------------------------------------
//   MscxModeDiff::adjustSemanticsMscx
//---------------------------------------------------------

void MscxModeDiff::adjustSemanticsMscx(std::vector<TextDiff>& diffs)
      {
      for (int i = 0; i < int(diffs.size()); ++i)
            i = adjustSemanticsMscxOneDiff(diffs, i);
      }

//---------------------------------------------------------
//   MscxModeDiff::adjustSemanticsMscx
//    Returns new index of the given diff
//---------------------------------------------------------

int MscxModeDiff::adjustSemanticsMscxOneDiff(std::vector<TextDiff>& diffs, int index)
      {
      TextDiff* diff = &diffs[index];
      int iScore;
      switch(diff->type) {
            case DiffType::EQUAL:
                  /* FALLTROUGH */
            case DiffType::REPLACE:
                  // TODO: split a REPLACE diff, though they should not be here
                  return index;
            case DiffType::INSERT:
                  iScore = 1;
                  break;
            case DiffType::DELETE:
                  /* FALLTROUGH */
            default:
                  iScore = 0;
                  break;
            }

      std::vector<Tag> extraTags;
      readMscx(diff->text[iScore], extraTags);

      if (extraTags.empty())
            return index; // good, nothing to adjust

      auto firstStartExtra = extraTags.begin();
      auto lastStartExtra = extraTags.begin();
      auto firstEndExtra = extraTags.begin();
      auto lastEndExtra = extraTags.begin();
      for (auto tag = extraTags.begin(); tag != extraTags.end(); ++tag) {
            TagType type = tag->type;
            switch(type) {
                  case START_TAG:
                        if (firstStartExtra->type != START_TAG)
                              firstStartExtra = tag;
                        lastStartExtra = tag;
                        break;
                  case END_TAG:
                        if (firstStartExtra->type != END_TAG)
                              firstEndExtra = tag;
                        lastEndExtra = tag;
                        break;
                  default:
                        break;
                  }
            }

      // Try to shift current diff up and down and see whether this helps
      int lines = lastEndExtra->line - firstEndExtra->line + 1;
      if (assessShiftDiff(diffs, extraTags, index, lines))
            return performShiftDiff(diffs, index, lines);
      else {
            lines = - (lastStartExtra->line - firstStartExtra->line + 1);
            if (assessShiftDiff(diffs, extraTags, index, lines))
                  return performShiftDiff(diffs, index, lines);
            }

      return index;
      }

//---------------------------------------------------------
//   MscxModeDiff::assessShiftDiff
//    Returns true if shift of the diff with the given
//    index is possible and there would be no extra tags
//    left after such a shift.
//---------------------------------------------------------

bool MscxModeDiff::assessShiftDiff(const std::vector<TextDiff>& diffs, const std::vector<Tag>& origExtraTags, int index, int lines)
      {
      if (lines == 0)
            return diffs.empty();
      const bool down = (lines > 0);
      const int nextDiffIdx = nextDiffOnShiftIndex(diffs, index, down);
      if (nextDiffIdx == index)
            return false;

      const TextDiff& diff = diffs[index];
      const TextDiff& nextDiff = diffs[nextDiffIdx];

      Q_ASSERT(diff.type == DiffType::DELETE || diff.type == DiffType::INSERT);
      const int iScore = (diff.type == DiffType::DELETE ? 0 : 1);

      const QString& diffTxt = diff.text[iScore];
      const QString& nextTxt = nextDiff.text[iScore];
      const QString diffChunk = getOuterLines(diffTxt, lines, down);
      const QString nextChunk = getOuterLines(nextTxt, lines, down);

      if (diffChunk == nextChunk) {
            std::vector<Tag> extraTags(origExtraTags);
            std::vector<Tag> diffExtraTags;
            readMscx(diffChunk, diffExtraTags);

            // merge diff chunk reading result with the result
            // for the whole chunk
            while (!diffExtraTags.empty()) {
                  auto tag = down ? diffExtraTags.begin() : (diffExtraTags.end() - 1);

                  if (extraTags.size() < 2) // would normally erase 2 tags
                        return false;

                  auto firstExtra = extraTags.begin();
                  if ((firstExtra->type == END_TAG)
                        && (firstExtra->name == tag->name))
                        extraTags.erase(firstExtra);
                  else
                        return false;
                  auto lastExtra = extraTags.end() - 1;
                  if ((lastExtra->type == START_TAG)
                        && (lastExtra->name == tag->name))
                        extraTags.erase(lastExtra);
                  else
                        return false;

                  diffExtraTags.erase(tag);
                  }

            if (extraTags.empty()) {
                  // We have succeeded
                  return true;
                  }
            }
      return false;
      }

//---------------------------------------------------------
//   MscxModeDiff::performShiftDiff
//    Perform a shift of the diff with the given index and
//    return a new index of the diff.
//---------------------------------------------------------

int MscxModeDiff::performShiftDiff(std::vector<TextDiff>& diffs, int index, int lines)
      {
      if (lines == 0)
            return index;
      const bool down = (lines > 0);
      const int absLines = qAbs(lines);
      const int nextDiffIdx = nextDiffOnShiftIndex(diffs, index, down);
      if (nextDiffIdx == index)
            return index;

      TextDiff& diff = diffs[index];
      TextDiff& nextDiff = diffs[nextDiffIdx];

      Q_ASSERT(diff.type == DiffType::DELETE || diff.type == DiffType::INSERT);
      const int iScore = (diff.type == DiffType::DELETE ? 0 : 1);
      QString& diffTxt = diff.text[iScore];

      const QString diffChunk = getOuterLines(diffTxt, absLines, down);

      Q_ASSERT(nextDiff.type == DiffType::EQUAL);
      int chunkStart[2]; // line numbers of diffChunk in two scores
      int chunkEnd[2];
      if (down) {
            std::copy(diff.start, diff.start + 2, chunkStart);
            chunkEnd[0] = diff.start[0] + absLines - 1;
            chunkEnd[1] = diff.start[1] + absLines - 1;
            }
      else {
            chunkStart[0] = diff.end[0] - (absLines - 1);
            chunkStart[1] = diff.end[1] - (absLines - 1);
            std::copy(diff.end, diff.end + 2, chunkEnd);
            }

      if (down) {
            diffTxt.remove(0, diffChunk.size());
            diffTxt.append(diffChunk);
            nextDiff.text[0].remove(0, diffChunk.size());
            nextDiff.text[1].remove(0, diffChunk.size());
            }
      else {
            diffTxt.chop(diffChunk.size());
            diffTxt.prepend(diffChunk);
            nextDiff.text[0].chop(diffChunk.size());
            nextDiff.text[1].chop(diffChunk.size());
            }
      // shift line numbers: needed both for shifted diff and for diffs
      // between the shifted diff and nextDiff
      const int inc = down ? 1 : -1;
      for (int i = index; i != nextDiffIdx; i += inc) {
            TextDiff& d = diffs[i];
            Q_UNUSED(d);
            Q_ASSERT(d.type != DiffType::EQUAL); // nextDiff should be the first EQUAL diff in that direction
            diff.start[0] += lines;
            diff.end[0] += lines;
            diff.start[1] += lines;
            diff.end[1] += lines;
            }
      int* nextChunkLines = down ? nextDiff.start : nextDiff.end;
      nextChunkLines[0] += lines;
      nextChunkLines[1] += lines;

      TextDiff eqDiff;
      eqDiff.type = DiffType::EQUAL;
      eqDiff.text[0] = diffChunk;
      eqDiff.text[1] = diffChunk;
      std::copy(chunkStart, chunkStart + 2, eqDiff.start);
      std::copy(chunkEnd, chunkEnd + 2, eqDiff.end);

      const int prevDiffIdx = index + (down ? -1 : 1);
      if (diffs[prevDiffIdx].type == DiffType::EQUAL)
            diffs[prevDiffIdx].merge(eqDiff);
      else {
            const int insertIdx = down ? index : (index + 1);
            diffs.insert(diffs.begin() + insertIdx, eqDiff);
            if (down)
                  ++index;
            }
      return index;
      }

//---------------------------------------------------------
//   MscxModeDiff::getOuterLines
//---------------------------------------------------------

QString MscxModeDiff::getOuterLines(const QString& str, int lines, bool start) {
      lines = qAbs(lines);
      const int secIdxStart = start ? 0 : (-1 - (lines - 1));
      const int secIdxEnd = start ? (lines - 1) : -1;
      constexpr auto secFlags = QString::SectionIncludeTrailingSep | QString::SectionSkipEmpty;
      return str.section('\n', secIdxStart, secIdxEnd, secFlags);
      }

//---------------------------------------------------------
//   MscxModeDiff::nextDiffOnShiftIndex
//---------------------------------------------------------

int MscxModeDiff::nextDiffOnShiftIndex(const std::vector<TextDiff>& diffs, int index, bool down)
      {
      const DiffType diffType = diffs[index].type;
      Q_ASSERT(diffType == DiffType::DELETE || diffType == DiffType::INSERT);
      int nextDiffIdx = index + (down ? 1 : -1);
      while (nextDiffIdx >= 0 && nextDiffIdx < int(diffs.size())) {
            const TextDiff& d = diffs[nextDiffIdx];
            if (d.type == diffType) {
                  // Two INSERT or DELETE diffs in a row, probably shouldn't happen
                  return index;
                  }
            if (d.type == DiffType::EQUAL)
                  return nextDiffIdx;
            // REPLACE is not here, the other type can be skipped
            Q_ASSERT(d.type != DiffType::REPLACE);
            nextDiffIdx += (down ? 1 : -1);
            }
      return index;
      }

//---------------------------------------------------------
//   MscxModeDiff::readMscx
//---------------------------------------------------------

void MscxModeDiff::readMscx(const QString& xml, std::vector<Tag>& extraTags)
      {
      int line = 0;
      int nextLineIndex = xml.indexOf('\n');
      QRegularExpressionMatchIterator m = tagRegExp.globalMatch(xml);
      while (m.hasNext()) {
            QRegularExpressionMatch match = m.next();
            QStringRef tag(match.capturedRef("name"));
            while ((match.capturedStart("name") > nextLineIndex) && (nextLineIndex > 0)) {
                  ++line;
                  nextLineIndex = xml.indexOf('\n', nextLineIndex + 1);
                  }
            if (match.capturedRef().startsWith("</")) {
                  // end element
                  handleTag(line, END_TAG, tag.toString(), extraTags);
                  if (tag == "Tuplet") {
                        // special case: <Tuplet> / <endTuplet/> pair
                        handleTag(line, START_TAG, "__tupletBound", extraTags);
                        }
                  }
            else if (match.capturedRef().endsWith("/>")) {
                  // empty element: do nothing
                  if (tag == "endTuplet") {
                        // special case: <Tuplet> / <endTuplet/> pair
                        handleTag(line, END_TAG, "__tupletBound", extraTags);
                        }
                  }
            else {
                  // start element
                  handleTag(line, START_TAG, tag.toString(), extraTags);
                  }
            }
      }

//---------------------------------------------------------
//   MscxModeDiff::handleTag
//---------------------------------------------------------

void MscxModeDiff::handleTag(int line, TagType type, const QString& name, std::vector<Tag>& extraTags)
      {
      switch(type) {
            case START_TAG:
                  extraTags.emplace_back(line, type, name);
                  break;
            case END_TAG:
                  {
                  auto lastTag = extraTags.empty() ? nullptr : &extraTags.back();
                  if (lastTag
                     && lastTag->type == START_TAG && lastTag->name == name)
                        extraTags.pop_back();
                  else
                        extraTags.emplace_back(line, type, name);
                  }
                  break;
            }
      }

//---------------------------------------------------------
//   TextDiffParser
//---------------------------------------------------------

TextDiffParser::TextDiffParser(int iScore)
   : iScore(iScore), iOtherScore(iScore == 1 ? 0 : 1)
      {
      contextsStack.push_back(nullptr);
      }

//---------------------------------------------------------
//   TextDiffParser::makeDiffs
//---------------------------------------------------------

void TextDiffParser::makeDiffs(const QString& mscx, const std::vector<std::pair<const ScoreElement*, QString>>& elements, const std::vector<TextDiff>& textDiffs, std::vector<BaseDiff*>& diffs)
      {
      QXmlStreamReader r(mscx);
      r.readNext(); // read first tag
      auto textDiff = textDiffs.begin();
      auto textDiffEnd = textDiffs.end();
      auto nextElement = elements.begin();
      auto nextElementEnd = elements.end();
      while (!r.atEnd()) {
            // Scroll to the correct diff to be handled now
            while (textDiff != textDiffEnd
               && textDiff->end[iScore] < r.lineNumber()) {
                  // Save ContextChange when leaving equal chunk
                  if (textDiff->type == DiffType::EQUAL) {
                        ContextChange* c = new ContextChange;
                        c->type = textDiff->type;
                        c->textDiff = &(*textDiff);
                        c->ctx[iScore] = contextsStack.back();
                        c->ctx[iOtherScore] = nullptr;
                        c->before[iScore] = lastElementEnded;
                        c->before[iOtherScore] = nullptr;
                        diffs.push_back(c);
                        }

                  ++textDiff;
                  }
            if (textDiff == textDiffEnd)
                  break;

            bool saveDiff = false;
            if (!insideDiffTag()
               && (textDiff->type == DiffType::DELETE || textDiff->type == DiffType::INSERT)
               ) {
                  const int iDiffScore = (textDiff->type == DiffType::DELETE) ? 0 : 1;
                  if (iScore == iDiffScore)
                        saveDiff = true;
                  }

            const ScoreElement* newElement = nullptr;
            if (r.isStartElement()) {
                  if (nextElement != nextElementEnd
                     && nextElement->second == r.name())
                        newElement = (nextElement++)->first;

                  ++tagLevel;
                  tagIsElement.push_back(newElement);
                  }

            BaseDiff* diff = handleToken(r, newElement, saveDiff);

            if (diff) {
                  diff->type = textDiff->type;
                  diff->textDiff = &(*textDiff);
                  diff->before[iScore] = lastElementEnded;
                  diff->before[iOtherScore] = nullptr;
                  diffs.push_back(diff);

                  if (saveDiff) {
                        // It is assumed that diff items are created in handleToken
                        // only for start tags
                        Q_ASSERT(r.isStartElement());
                        ItemType type = diff->itemType();
                        if (type == ItemType::ELEMENT || type == ItemType::PROPERTY) {
                              currentDiffTagLevel = tagLevel;
                              if (type == ItemType::ELEMENT && contextsStack.back()->isSpanner()) {
                                    // Consider <Spanner> tag as the current differing tag
                                    --currentDiffTagLevel;
                                    }
                              }
                        }
                  }

            if (r.isEndElement()) {
                  if (currentDiffTagLevel == tagLevel)
                        currentDiffTagLevel = 0;
                  --tagLevel;
                  tagIsElement.pop_back();
                  }

            r.readNext();
            }
      if (r.hasError())
            qWarning("TextDiffParser::makeDiffs: error while reading MSCX output: %s", r.errorString().toLatin1().constData());
      }

//---------------------------------------------------------
//   TextDiffParser::handleToken
//    Returns nullptr or a newly allocated BaseDiff with
//    properly filled context and fields that are unique to
//    descendants of BaseDiff (e.g. el[2] for ElementDiff).
//    Type and textDiff are left unfilled.
//---------------------------------------------------------

BaseDiff* TextDiffParser::handleToken(const QXmlStreamReader& r, const ScoreElement* newElement, bool saveDiff)
      {
      if (!r.isStartElement() && !r.isEndElement())
            return nullptr;
      BaseDiff* diff = nullptr;
      const QStringRef tag(r.name());

      if (r.isStartElement()) {
            if (newElement) {
                  if (saveDiff) {
                        ElementDiff* d = new ElementDiff;
                        diff = d;
                        d->ctx[iScore] = contextsStack.back();
                        d->el[iScore] = newElement;
                        d->el[iOtherScore] = nullptr;
                        }
                  contextsStack.push_back(newElement);
                  }
            else if (tag == "Spanner")
                  markupContext = tag.toString();
            else if (saveDiff) {
                  const ScoreElement* ctx = contextsStack.back();
                  if (markupContext == "Spanner" && !(ctx && ctx->isSpanner())) {
                        // some property inside the end of a spanner
                        }
                  else if (tag == "prev" || tag == "next") {
                        // Ignore these tags
                        }
                  else if (tag == "location" || tag == "voice") {
                        if (ctx && ctx->isMeasure()) {
                              MarkupDiff* d = new MarkupDiff;
                              diff = d;
                              d->ctx[iScore] = ctx;
                              d->name = tag.toString();
                              const ScoreElement* widerCtx = *(contextsStack.end() - 2);
                              if (widerCtx && widerCtx->isStaff())
                                    d->info = toStaff(widerCtx)->idx();
                              }
                        }
                  else {
                        Pid propId = ctx ? ctx->propertyId(tag) : Pid::END;
                        if (propId != Pid::END) {
                              PropertyDiff* d = new PropertyDiff;
                              diff = d;
                              d->ctx[iScore] = contextsStack.back();
                              d->pid = propId;
                              }
                        else {
                              MarkupDiff* d = new MarkupDiff;
                              diff = d;
                              d->ctx[iScore] = contextsStack.back();
                              d->name = tag.toString();
                              if (tag == "metaTag")
                                    d->info = r.attributes().value("name").toString();
                              }
                        }
                  }
            }
      else { // end element
            if ((tagIsElement.back() && !contextsStack.back()->isSpanner())
               || (tag == "Spanner" && contextsStack.back()->isSpanner())
               ) {
                  lastElementEnded = contextsStack.back();
                  contextsStack.pop_back();
                  }
            if (!markupContext.isEmpty() && tag == markupContext)
                  markupContext.clear();
            }

      if (diff)
            diff->ctx[iOtherScore] = nullptr;
      return diff;
      }

//---------------------------------------------------------
//   lineNumberSort
//---------------------------------------------------------

static bool lineNumberSort(BaseDiff* d1, BaseDiff* d2)
      {
      const TextDiff* t1 = d1->textDiff;
      const TextDiff* t2 = d2->textDiff;
      return ((t1->end[0] < t2->end[0]) || (t1->end[1] < t2->end[1]));
      }

//---------------------------------------------------------
//   positionSort
//---------------------------------------------------------

static bool positionSort(BaseDiff* d1, BaseDiff* d2)
      {
      return (d1->afrac(0) < d2->afrac(0) || d1->afrac(1) < d2->afrac(1));
      }

//---------------------------------------------------------
//   scoreToMscx
//---------------------------------------------------------

static QString scoreToMscx(Score* s, XmlWriter& xml)
      {
      QString mscx;
      xml.setString(&mscx, QIODevice::WriteOnly);
      xml.setRecordElements(true);
      s->write(xml, /* onlySelection */ false);
      xml.flush();
      return mscx;
      }

//---------------------------------------------------------
//   ScoreDiff
//    s1, s2 are scores to compare.
//    ScoreDiff does NOT take ownership of the scores.
//---------------------------------------------------------

ScoreDiff::ScoreDiff(Score* s1, Score* s2, bool textDiffOnly)
   : _s1(s1), _s2(s2), _textDiffOnly(textDiffOnly)
      {
      update();
      }

//---------------------------------------------------------
//   makeDiffs
//---------------------------------------------------------

static void makeDiffs(const QString& mscx1, const QString& mscx2, const XmlWriter& xml1, const XmlWriter& xml2, const std::vector<TextDiff>& textDiffs, std::vector<BaseDiff*>& diffs)
      {
      TextDiffParser p1(0);
      p1.makeDiffs(mscx1, xml1.elements(), textDiffs, diffs);
      TextDiffParser p2(1);
      p2.makeDiffs(mscx2, xml2.elements(), textDiffs, diffs);

      std::stable_sort(diffs.begin(), diffs.end(), lineNumberSort);

      // Fill correct contexts for diffs
      const ScoreElement* lastCtx[2] {};
      const ScoreElement* lastBefore[2] {};
      for (BaseDiff* diff : diffs) {
            for (int i = 0; i < 2; ++i) {
                  if (diff->ctx[i])
                        lastCtx[i] = diff->ctx[i];
                  else
                        diff->ctx[i] = lastCtx[i];

                  if (diff->before[i])
                        lastBefore[i] = diff->before[i];
                  else
                        diff->before[i] = lastBefore[i];
                  }
            }

      // Filter out EQUAL diffs and dummy ContextChange items
      std::vector<BaseDiff*> filteredDiffs;
      for (BaseDiff* diff : diffs) {
            if ((diff->itemType() == ItemType::CONTEXTCHANGE)
               || (diff->type == DiffType::EQUAL))
                  delete diff;
            else
                  filteredDiffs.push_back(diff);
            }
      std::swap(diffs, filteredDiffs);
      }

//---------------------------------------------------------
//   ScoreDiff::update
//    Updates the diff according to the current state of
//    the compared scores.
//---------------------------------------------------------

void ScoreDiff::update()
      {
      if (updated())
            return;

      qDeleteAll(_diffs);
      _diffs.clear();
      _textDiffs.clear();
      qDeleteAll(_mergedTextDiffs);
      _mergedTextDiffs.clear();

      XmlWriter xml1(_s1);
      XmlWriter xml2(_s2);
      QString mscx1(scoreToMscx(_s1, xml1));
      QString mscx2(scoreToMscx(_s2, xml2));

      _textDiffs = MscxModeDiff().mscxModeDiff(mscx1, mscx2);

      if (!_textDiffOnly) {
            makeDiffs(mscx1, mscx2, xml1, xml2, _textDiffs, _diffs);

            processMarkupDiffs();
            mergeInsertDeleteDiffs();
            mergeElementDiffs();
            editPropertyDiffs();

            std::stable_sort(_diffs.begin(), _diffs.end(), positionSort);
            }

      _scoreState1 = _s1->state();
      _scoreState2 = _s2->state();
      }

//---------------------------------------------------------
//   ~ScoreDiff
//---------------------------------------------------------

ScoreDiff::~ScoreDiff()
      {
      qDeleteAll(_mergedTextDiffs);
      qDeleteAll(_diffs);
      }

//---------------------------------------------------------
//   deleteDiffs
//---------------------------------------------------------

static void deleteDiffs(std::vector<BaseDiff*>& diffsList, const std::vector<BaseDiff*>& toDelete)
      {
      for (BaseDiff* diff : toDelete) {
            for (auto it = diffsList.begin(); it != diffsList.end(); ++it) {
                  if (*it == diff) {
                        diffsList.erase(it);
                        break;
                        }
                  }
            delete diff;
            }
      }

//---------------------------------------------------------
//   measureToMscx
//---------------------------------------------------------

static QString measureToMscx(const Measure* m, XmlWriter& xml, int staff)
      {
      QString mscx;
      xml.setString(&mscx, QIODevice::WriteOnly);
      xml.setRecordElements(true);
      m->write(xml, staff, false, false);
      xml.flush();
      return mscx;
      }

//---------------------------------------------------------
//   measureInfo
//---------------------------------------------------------

struct MeasureInfo {
      const Measure* m1;
      const Measure* m2;
      int staff;

      MeasureInfo(const Measure* m1, const Measure* m2, int staff) : m1(m1), m2(m2), staff(staff) {}

      bool operator==(const MeasureInfo& other) { return m1 == other.m1 && m2 == other.m2 && staff == other.staff; }
      bool operator!=(const MeasureInfo& other) { return !(*this == other); }
      };

//---------------------------------------------------------
//   ScoreDiff::processMarkupDiffs
//    Find MarkupDiffs and convert them to the appropriate
//    diffs of other types if needed.
//---------------------------------------------------------

void ScoreDiff::processMarkupDiffs()
      {
      std::vector<BaseDiff*> abandonedDiffs;
      std::vector<MeasureInfo> measuresToProcess;
      Pid pids[] { Pid::TRACK, Pid::POSITION }; // list of pids to be accepted on measures processing
      for (BaseDiff* diff : _diffs) {
            if (diff->itemType() != ItemType::MARKUP)
                  continue;

            MarkupDiff* d = static_cast<MarkupDiff*>(diff);
            const QString& name = d->name;

            if (name == "voice" || name == "location") {
                  if (d->ctx[0]->isMeasure() && d->ctx[1]->isMeasure()) {
                        MeasureInfo m(toMeasure(d->ctx[0]), toMeasure(d->ctx[1]), d->info.toInt());
                        bool add = true;
                        for (auto& mScheduled : measuresToProcess) {
                              if (mScheduled == m) {
                                    add = false;
                                    break;
                                    }
                              }
                        if (add)
                              measuresToProcess.push_back(m);
                        abandonedDiffs.push_back(d);
                        }
                  }
            }

      std::vector<BaseDiff*> newDiffs;
      for (auto& m : measuresToProcess) {
            XmlWriter xml1(m.m1->score());
            xml1.setWriteTrack(true);
            xml1.setWritePosition(true);
            QString mscx1 = measureToMscx(m.m1, xml1, m.staff);

            XmlWriter xml2(m.m2->score());
            xml2.setWriteTrack(true);
            xml2.setWritePosition(true);
            QString mscx2 = measureToMscx(m.m2, xml2, m.staff);

            std::vector<TextDiff> textDiffs = MscxModeDiff().mscxModeDiff(mscx1, mscx2);
            makeDiffs(mscx1, mscx2, xml1, xml2, textDiffs, newDiffs);
            }

      for (BaseDiff* newDiff : newDiffs) {
            bool save = false;
            if (newDiff->itemType() == ItemType::PROPERTY) {
                  PropertyDiff* pDiff = static_cast<PropertyDiff*>(newDiff);
                  for (Pid pid : pids) {
                        if (pDiff->pid == pid) {
                              save = true;
                              break;
                              }
                        }
                  if (pDiff->pid == Pid::TRACK)
                        pDiff->pid = Pid::VOICE;

                  if (pDiff->ctx[0]->isMeasure() || pDiff->ctx[1]->isMeasure())
                        save = false;
                  }


            if (save) {
                  newDiff->textDiff = nullptr;
                  _diffs.push_back(newDiff);
                  }
            else
                  delete newDiff;
            }

      deleteDiffs(_diffs, abandonedDiffs);
      }

//---------------------------------------------------------
//   ScoreDiff::mergeInsertDeleteDiffs
//    Merge INSERT and DELETE diffs to REPLACE diffs where
//    possible.
//---------------------------------------------------------

void ScoreDiff::mergeInsertDeleteDiffs()
      {
      const ScoreElement* lastCtx1 = _s1;
      const ScoreElement* lastCtx2 = _s2;
      std::vector<BaseDiff*> diffsToMerge;
      std::vector<BaseDiff*> abandonedDiffs;
      for (BaseDiff* diff : _diffs) {
            if ((diff->ctx[0] != lastCtx1) || (diff->ctx[1] != lastCtx2)) {
                  diffsToMerge.clear();
                  lastCtx1 = diff->ctx[0];
                  lastCtx2 = diff->ctx[1];
                  }
            bool merge = false;
            for (auto diff2It = diffsToMerge.begin(); diff2It != diffsToMerge.end(); ++diff2It) {
                  BaseDiff* diff2 = (*diff2It);
                  if (diff->type == DiffType::DELETE) {
                        if ((diff2->type == DiffType::INSERT) && diff->sameItem(*diff2))
                              merge = true;
                        }
                  else if (diff->type == DiffType::INSERT) {
                        if ((diff2->type == DiffType::DELETE) && diff->sameItem(*diff2))
                              merge = true;
                        }

                  if (merge) {
                        // To preserve the correct order of diffs, we should
                        // leave diff2 in place since it appeared earlier in
                        // the _diffs list.
                        diff2->type = DiffType::REPLACE;

                        if (diff->textDiff && diff2->textDiff) {
                              TextDiff* mergedTextDiff = new TextDiff(*diff2->textDiff);
                              mergedTextDiff->merge(*diff->textDiff);
                              _mergedTextDiffs.push_back(mergedTextDiff);
                              diff2->textDiff = mergedTextDiff;
                              }
                        else if (!diff2->textDiff)
                              diff2->textDiff = diff->textDiff;

                        int idx1 = (diff->type == DiffType::DELETE) ? 0 : 1;
                        switch(diff2->itemType()) {
                              case ItemType::ELEMENT:
                                    {
                                    ElementDiff* d = static_cast<ElementDiff*>(diff);
                                    ElementDiff* d2 = static_cast<ElementDiff*>(diff2);
                                    // d2->el[idx2] is already filled
                                    d2->el[idx1] = d->el[idx1];
                                    }
                                    break;
                              case ItemType::PROPERTY:
                              case ItemType::MARKUP:
                                    // nothing to fill
                                    break;
                              case ItemType::CONTEXTCHANGE:
                                    break;
                              }

                        abandonedDiffs.push_back(diff);
                        diffsToMerge.erase(diff2It);
                        break; // loop on diffsToMerge
                        }
                  }

            if (!merge)
                  diffsToMerge.push_back(diff);
            }

      deleteDiffs(_diffs, abandonedDiffs);
      }

//---------------------------------------------------------
//   ScoreDiff::mergeElementDiffs
//    Merge element diffs with equal elements
//---------------------------------------------------------

void ScoreDiff::mergeElementDiffs()
      {
      std::map<const ScoreElement*, ElementDiff*> elementDiffs;
      std::vector<BaseDiff*> abandonedDiffs;
      for (BaseDiff* diff : _diffs) {
            if (diff->itemType() != ItemType::ELEMENT)
                  continue;

            ElementDiff* elDiff = static_cast<ElementDiff*>(diff);
            auto foundDiffIt1 = elementDiffs.find(elDiff->el[0]);
            auto foundDiffIt2 = elementDiffs.find(elDiff->el[1]);
            ElementDiff* foundDiff = nullptr;
            if (foundDiffIt1 != elementDiffs.end())
                  foundDiff = foundDiffIt1->second;
            else if (foundDiffIt2 != elementDiffs.end())
                  foundDiff = foundDiffIt2->second;

            if (foundDiff && foundDiff->type == elDiff->type
               && foundDiff->el[0] == elDiff->el[0]
               && foundDiff->el[1] == elDiff->el[1]) {
                  for (int i = 0; i < 2; ++i) {
                        const ScoreElement* se = foundDiff->el[i];
                        if (!se)
                              continue;
                        if (!se->isMeasure() && se->isElement()) {
                              const Element* m = toElement(se)->findMeasure();
                              if (m)
                                    foundDiff->ctx[i] = m;
                              else
                                    foundDiff->ctx[i] = se->score();
                              }
                        else
                              foundDiff->ctx[i] = se->score();
                        }
                  abandonedDiffs.push_back(elDiff);
                  }
            else {
                  for (int i = 0; i < 2; ++i) {
                        if (const ScoreElement* se = elDiff->el[i])
                              elementDiffs[se] = elDiff;
                        }
                  }
            }

      deleteDiffs(_diffs, abandonedDiffs);
      }

//---------------------------------------------------------
//   ScoreDiff::editPropertyDiffs
//    Merge property diffs with equal elements, edit some
//    diffs if necessary
//---------------------------------------------------------

void ScoreDiff::editPropertyDiffs()
      {
      std::multimap<const ScoreElement*, PropertyDiff*> propertyDiffs;
      std::vector<BaseDiff*> abandonedDiffs;
      for (BaseDiff* diff : _diffs) {
            if (diff->itemType() != ItemType::PROPERTY)
                  continue;

            PropertyDiff* pDiff = static_cast<PropertyDiff*>(diff);

            if (pDiff->pid == Pid::TPC1 || pDiff->pid == Pid::TPC2) {
                  // Special case: "tpc" and "pitch" properties are partially
                  // overlapped so we will mark tpc changes as pitch changes
                  pDiff->pid = Pid::PITCH;
                  }

            auto foundRange = propertyDiffs.equal_range(pDiff->ctx[0]);
            bool merged = false;
            for (auto it = foundRange.first; it != foundRange.second; ++it) {
                  PropertyDiff* foundDiff = it->second;
                  if (foundDiff && foundDiff->type == pDiff->type
                     && foundDiff->ctx[0] == pDiff->ctx[0]
                     && foundDiff->ctx[1] == pDiff->ctx[1]
                     && foundDiff->pid == pDiff->pid
                     ) {
                        abandonedDiffs.push_back(pDiff);
                        merged = true;
                        break;
                        }
                  }

            if (!merged)
                  propertyDiffs.emplace(pDiff->ctx[0], pDiff);
            }

      deleteDiffs(_diffs, abandonedDiffs);
      }

//---------------------------------------------------------
//   ScoreDiff::equal
//---------------------------------------------------------

bool ScoreDiff::equal() const
      {
      for (const TextDiff& td : _textDiffs) {
            if (td.type != DiffType::EQUAL)
                  return false;
            }
      return true;
      }

//---------------------------------------------------------
//   ScoreDiff::rawDiff
//---------------------------------------------------------

QString ScoreDiff::rawDiff(bool skipEqual) const
      {
      QStringList list;
      for (const TextDiff& td : _textDiffs) {
            if (!skipEqual || td.type != DiffType::EQUAL)
                  list.push_back(td.toString(true));
            }
      return list.join('\n');
      }

//---------------------------------------------------------
//   ScoreDiff::userDiff
//---------------------------------------------------------

QString ScoreDiff::userDiff() const
      {
      QStringList list;
      for (const BaseDiff* d : _diffs)
            list.push_back(d->toString());
      return list.join('\n');
      }

//---------------------------------------------------------
//   TextDiff::merge
//---------------------------------------------------------

void TextDiff::merge(const TextDiff& other)
      {
      if (type == other.type) {
            if (other.end[0] == (start[0] - 1) && other.end[1] == (start[1] - 1)) {
                  start[0] = other.start[0];
                  start[1] = other.start[1];
                  text[0].prepend(other.text[0]);
                  text[1].prepend(other.text[1]);
                  }
            else if ((end[0] + 1) == other.start[0] && (end[1] + 1) == other.start[1]) {
                  end[0] = other.end[0];
                  end[1] = other.end[1];
                  text[0].append(other.text[0]);
                  text[1].append(other.text[1]);
                  }
            else
                  qFatal("TextDiff:merge: invalid argument: wrong line numbers");
            }
      else if ((type == DiffType::INSERT && other.type == DiffType::DELETE)
         || (type == DiffType::DELETE && other.type == DiffType::INSERT)
         ) {
            type = DiffType::REPLACE;
            const int iOther = (other.type == DiffType::DELETE) ? 0 : 1;
            start[iOther] = other.start[iOther];
            end[iOther] = other.end[iOther];
            text[iOther] = other.text[iOther];
            }
      else
            qFatal("TextDiff:merge: invalid argument: wrong types");
      }

//---------------------------------------------------------
//   addLinePrefix
//---------------------------------------------------------

static QString addLinePrefix(const QString& str, const QString& prefix)
      {
      if (prefix.isEmpty())
            return str;
      QVector<QStringRef> lines = str.splitRef('\n');
      if (lines.back().isEmpty())
            lines.pop_back();
      QStringList processedLines;
      for (QStringRef& line : lines)
            processedLines.push_back(QString(prefix).append(line));
      return processedLines.join('\n');
      }

//---------------------------------------------------------
//   TextDiff::toString
//    type argument allows to define which part of diff
//    should be printed. For example, it allows to print
//    only deleted chunk for REPLACE diff item.
//---------------------------------------------------------

QString TextDiff::toString(DiffType dt, bool prefixLines) const
      {
      if (dt == DiffType::REPLACE) {
            QStringList l;
            l.push_back(toString(DiffType::DELETE, prefixLines));
            l.push_back(toString(DiffType::INSERT, prefixLines));
            return l.join('\n');
            }

      int idx = (dt == DiffType::INSERT) ? 1 : 0;
      const char* prefix = (dt == DiffType::INSERT) ? ">" : "<";

      QString lines[2];
      for (int i = 0; i < 2; ++i) {
            if ((i != idx && dt != DiffType::EQUAL)
                  || end[i] <= start[i]
                  ) {
                  lines[i] = QString::number(start[i]);
                  }
            else
                  lines[i] = QString("%1--%2")
                        .arg(start[i]).arg(end[i]);
            }

      return QString("@%1;%2\n%3")
            .arg(lines[0]).arg(lines[1])
            .arg(prefixLines ? addLinePrefix(text[idx], prefix) : text[idx]);
      }

//---------------------------------------------------------
//   BaseDiff::sameItem
//---------------------------------------------------------

bool BaseDiff::sameItem(const BaseDiff& other) const
      {
      if (itemType() != other.itemType()
         || ctx[0] != other.ctx[0] || ctx[1] != other.ctx[1])
            return false;
      if (textDiff == other.textDiff)
            return true;
      else if (!textDiff || !other.textDiff)
            return false;
      return (textDiff->start[0] == other.textDiff->start[0]
         && textDiff->start[1] == other.textDiff->start[1]
         );
      }

//---------------------------------------------------------
//   BaseDiff::afrac
//    Returns position of the changed chunk
//---------------------------------------------------------

Fraction BaseDiff::afrac(int score) const
      {
      Q_ASSERT(score == 0 || score == 1);
      if (ctx[score] && ctx[score]->isElement())
            return toElement(ctx[score])->tick();
      if (before[score] && before[score]->isElement()) {
            const Element* bef = toElement(before[score]);
            Fraction f = bef->tick();
            if (bef->isDurationElement()) {
                  const DurationElement* de = toDurationElement(bef);
                  return f + de->actualTicks();
                  }
            return f;
            }
      return Fraction(0,1);
      }

//---------------------------------------------------------
//   describeContext
//---------------------------------------------------------

static QString describeContext(const ScoreElement* ctx)
      {
      if (!ctx)
            return QString("no context");
      QString descr;
      if (ctx->isElement()) {
            const Element* e = toElement(ctx);
            if (const Measure* m = toMeasure(e->findMeasure()))
                  descr += QString("%1 %2").arg(m->userName()).arg(m->no() + 1);
            }
      if (!ctx->isMeasure()) {
            if (!descr.isEmpty())
                  descr += ": ";
            descr += ctx->userName();
            // TODO: add more info
            }
      return descr;
      }

//---------------------------------------------------------
//   ContextChange::toString
//    ContextChange elements are not intended to be shown
//    to user, this function is for debugging purposes.
//---------------------------------------------------------

QString ContextChange::toString() const
      {
      return QString("Context change: ctx1 (%1), ctx2(%2)").arg(describeContext(ctx[0])).arg(describeContext(ctx[1]));
      }

//---------------------------------------------------------
//   ElementDiff::sameItem
//---------------------------------------------------------

bool ElementDiff::sameItem(const BaseDiff& other) const
      {
      return BaseDiff::sameItem(other);
      }

//---------------------------------------------------------
//   ElementDiff::afrac
//    Returns position of the changed chunk
//---------------------------------------------------------

Fraction ElementDiff::afrac(int score) const
      {
      Q_ASSERT(score == 0 || score == 1);
      const ScoreElement* se = el[score];
      if (se && se->isElement())
            return toElement(se)->tick();
      return BaseDiff::afrac(score);
      }

//---------------------------------------------------------
//   ElementDiff::toString
//---------------------------------------------------------

QString ElementDiff::toString() const
      {
      QString ctxDescr = describeContext(ctx[0]);
      switch(type) {
            case DiffType::DELETE:
                  return QObject::tr("%1: removed element %2", "scorediff").arg(ctxDescr).arg(el[0]->userName());
            case DiffType::INSERT:
                  return QObject::tr("%1: inserted element %2", "scorediff").arg(ctxDescr).arg(el[1]->userName());
            case DiffType::REPLACE:
                  return QObject::tr("%1: replaced element %2 with element %3", "scorediff").arg(ctxDescr).arg(el[0]->userName()).arg(el[1]->userName());
            case DiffType::EQUAL:
                  Q_ASSERT(el[0]->type() == el[1]->type());
                  return QObject::tr("%1: equal element %2", "scorediff").arg(ctxDescr).arg(el[0]->userName());
            }
      return ctxDescr;
      }

//---------------------------------------------------------
//   PropertyDiff::sameItem
//---------------------------------------------------------

bool PropertyDiff::sameItem(const BaseDiff& otherBase) const
      {
      if (!BaseDiff::sameItem(otherBase))
            return false;
      if (itemType() != otherBase.itemType())
            return false;
      const PropertyDiff& other = static_cast<const PropertyDiff&>(otherBase);
      return (pid == other.pid);
      }

//---------------------------------------------------------
//   PropertyDiff::toString
//---------------------------------------------------------

QString PropertyDiff::toString() const
      {
      QString ctxDescr = describeContext(ctx[0]);
      QString propName = propertyUserName(pid);
      switch(propertyType(pid)) {
            case P_TYPE::BOOL:
                  {
                  bool b1 = ctx[0]->getProperty(pid).toBool();
                  Q_ASSERT(b1 != ctx[1]->getProperty(pid).toBool());
                  QString t;
                  if (b1)
                        t = QObject::tr("%1: property %2 is turned off", "scorediff");
                  else
                        t = QObject::tr("%1: property %2 is turned on", "scorediff");
                  return t.arg(ctxDescr).arg(propName);
                  }
            default:
                  {
                  QString val1 = ctx[0]->propertyUserValue(pid);
                  QString val2 = ctx[1]->propertyUserValue(pid);
                  QString t = QObject::tr("%1: property %2 changed from %3 to %4", "scorediff");
                  return t.arg(ctxDescr).arg(propName).arg(val1).arg(val2);
                  }
            }
      }

//---------------------------------------------------------
//   MarkupDiff::sameItem
//---------------------------------------------------------

bool MarkupDiff::sameItem(const BaseDiff& otherBase) const
      {
      if (!BaseDiff::sameItem(otherBase))
            return false;
      if (itemType() != otherBase.itemType())
            return false;
      const MarkupDiff& other = static_cast<const MarkupDiff&>(otherBase);
      return (name == other.name);
      }

//---------------------------------------------------------
//   MarkupDiff::toString
//---------------------------------------------------------

QString MarkupDiff::toString() const
      {
      QString ctxDescr = describeContext(ctx[0]);
      if (name == "metaTag") {
            QString tagName = info.toString();
            return QObject::tr("%1: %2 changed from %3 to %4", "scorediff")
               .arg(ctxDescr).arg(tagName)
               .arg(ctx[0]->score()->metaTag(tagName))
               .arg(ctx[1]->score()->metaTag(tagName));
            }
      return QObject::tr("%1: markup changes: %2", "scorediff")
         .arg(ctxDescr).arg(name);
      }
}