Advertisement
rplantiko

UltraEdit - Compare two documents

Jul 7th, 2012
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // An UltraEdit script which compares two documents
  2. // Much simpler than UltraCompare, it only extracts a RegExp from each line (default: \w+)
  3. // Then compares the two sets of extracted strings
  4. // Output:
  5. // - Extracted strings of first doc
  6. // - Extracted strings of second doc
  7. // - Strings contained in the first but not in the second doc
  8. // - Strings contained in the second but not in the first doc
  9. // - Strings contained in both docs
  10. start();
  11.  
  12. function start() {
  13.   if (UltraEdit.document.length >= 2) {
  14.     var param = getParameters();
  15.     if (param.first && param.second) {  
  16.       compareDocuments(param.first, param.second, param.norm);
  17.       }
  18.     }
  19.   else {
  20.     UltraEdit.outputWindow.write( "Es müssen mindestens zwei Textfenster offen sein" );
  21.     }  
  22.   }
  23.  
  24. function compareDocuments(first,second,norm) {
  25.   var set1 = getLines( first, norm);
  26.   output( "Erstes Dokument", getKeys( set1 ) );
  27.   var set2 = getLines( second, norm);
  28.   output( "Zweites Dokument", getKeys( set2 ) );
  29.   compareSets( set1, set2 );
  30.   }
  31.  
  32. function getParameters() {
  33.  
  34.   var msg="[0 1 \\w+] ",i,fileName, param;
  35.  
  36.   for (i=0;i<UltraEdit.document.length;i++) {
  37.     if (UltraEdit.document[i].path.match(/\\([^\\]*)$/)) {
  38.       fileName = RegExp.$1;
  39.       }
  40.     else {
  41.       fileName = UltraEdit.document[i].path;
  42.       }
  43.     msg += i + ":" + fileName + ",";
  44.     }
  45.    
  46.   var inp = UltraEdit.getString( msg, 1 );
  47.   if (inp.match(/(\d+)\D+(\d+)(?:\s+(.*))?/)) {
  48.     var re = RegExp.$3 || "\\w+";
  49.     re = "(" + re + ")";
  50.     var ret = {};
  51.     ret.norm = new RegExp( re );
  52.     ret.first = UltraEdit.document[1*RegExp.$1];
  53.     ret.second = UltraEdit.document[1*RegExp.$2];
  54.     return ret;
  55.     }
  56.   else
  57.     return { norm:/(\w+)/, first:UltraEdit.document[0], second:UltraEdit.document[1]};
  58.  
  59.   }
  60.  
  61. function getLines(doc,re) {
  62.   doc.selectAll();
  63.   var selection = doc.selection
  64.                      .replace( /^\xEF\xBB\xBF/,"")  // Unicode-BOM
  65.                      .replace( /\x00/g,"");         // Unicode-Bytes enfernen
  66.   var result = {};
  67.   var lineEnd   = selection.match( /[\r\n]{1,2}/ ) || "\r\n" ;
  68.   var lines = selection.split( lineEnd );  
  69.   for (var i=0; i<lines.length;i++) {
  70.     if (lines[i].match(re)) {
  71.       if (result[RegExp.$1]) result[RegExp.$1]++;
  72.       else result[RegExp.$1] = 1;
  73.       }
  74.     }
  75.   return result;  
  76.   }  
  77.  
  78. function compareSets( set1, set2 ) {
  79.  
  80.   var result;
  81.  
  82.   result = getDifference(set1, set2);
  83.   output( "Nur im ersten Dok, nicht im zweiten", result);
  84.  
  85.   result = getDifference(set2, set1);
  86.   output( "Nur im zweiten Dok, nicht im ersten", result);
  87.  
  88.   result = getIntersection(set1, set2);
  89.   output( "Gemeinsame Felder", result);
  90.  
  91.   }
  92.  
  93. function output(title, result) {
  94.   beginSection(title);
  95.   showArray(result);
  96.   endSection();
  97.   }  
  98.  
  99. function beginSection(title) {
  100.   UltraEdit.outputWindow.write("");
  101.   UltraEdit.outputWindow.write("* " + title);
  102.   }
  103.  
  104. function endSection() {
  105.   UltraEdit.outputWindow.write("* ---");
  106.   UltraEdit.outputWindow.write("");  
  107.   }      
  108.  
  109. function showArray(lines) {
  110.   for (var i=0;i<lines.length;i++)
  111.     UltraEdit.outputWindow.write( lines[i] );
  112.   }  
  113.  
  114. function getDifference(set1, set2) {
  115.   var result = [];
  116.   for (var x in set1) {
  117.     if (! (x in set2) ) result.push(x);
  118.     }
  119.   return result;  
  120.   }
  121.  
  122. function getIntersection(set1, set2) {
  123.   var result = [];
  124.   for (var x in set1) {
  125.     if (x in set2) result.push(x);
  126.     }
  127.   return result;  
  128.   }
  129.  
  130. function getKeys( obj ) {
  131.   var result = [];
  132.   for (var x in obj) result.push( x );
  133.   return result;
  134.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement