Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // An UltraEdit script which compares two documents
- // Much simpler than UltraCompare, it only extracts a RegExp from each line (default: \w+)
- // Then compares the two sets of extracted strings
- // Output:
- // - Extracted strings of first doc
- // - Extracted strings of second doc
- // - Strings contained in the first but not in the second doc
- // - Strings contained in the second but not in the first doc
- // - Strings contained in both docs
- start();
- function start() {
- if (UltraEdit.document.length >= 2) {
- var param = getParameters();
- if (param.first && param.second) {
- compareDocuments(param.first, param.second, param.norm);
- }
- }
- else {
- UltraEdit.outputWindow.write( "Es müssen mindestens zwei Textfenster offen sein" );
- }
- }
- function compareDocuments(first,second,norm) {
- var set1 = getLines( first, norm);
- output( "Erstes Dokument", getKeys( set1 ) );
- var set2 = getLines( second, norm);
- output( "Zweites Dokument", getKeys( set2 ) );
- compareSets( set1, set2 );
- }
- function getParameters() {
- var msg="[0 1 \\w+] ",i,fileName, param;
- for (i=0;i<UltraEdit.document.length;i++) {
- if (UltraEdit.document[i].path.match(/\\([^\\]*)$/)) {
- fileName = RegExp.$1;
- }
- else {
- fileName = UltraEdit.document[i].path;
- }
- msg += i + ":" + fileName + ",";
- }
- var inp = UltraEdit.getString( msg, 1 );
- if (inp.match(/(\d+)\D+(\d+)(?:\s+(.*))?/)) {
- var re = RegExp.$3 || "\\w+";
- re = "(" + re + ")";
- var ret = {};
- ret.norm = new RegExp( re );
- ret.first = UltraEdit.document[1*RegExp.$1];
- ret.second = UltraEdit.document[1*RegExp.$2];
- return ret;
- }
- else
- return { norm:/(\w+)/, first:UltraEdit.document[0], second:UltraEdit.document[1]};
- }
- function getLines(doc,re) {
- doc.selectAll();
- var selection = doc.selection
- .replace( /^\xEF\xBB\xBF/,"") // Unicode-BOM
- .replace( /\x00/g,""); // Unicode-Bytes enfernen
- var result = {};
- var lineEnd = selection.match( /[\r\n]{1,2}/ ) || "\r\n" ;
- var lines = selection.split( lineEnd );
- for (var i=0; i<lines.length;i++) {
- if (lines[i].match(re)) {
- if (result[RegExp.$1]) result[RegExp.$1]++;
- else result[RegExp.$1] = 1;
- }
- }
- return result;
- }
- function compareSets( set1, set2 ) {
- var result;
- result = getDifference(set1, set2);
- output( "Nur im ersten Dok, nicht im zweiten", result);
- result = getDifference(set2, set1);
- output( "Nur im zweiten Dok, nicht im ersten", result);
- result = getIntersection(set1, set2);
- output( "Gemeinsame Felder", result);
- }
- function output(title, result) {
- beginSection(title);
- showArray(result);
- endSection();
- }
- function beginSection(title) {
- UltraEdit.outputWindow.write("");
- UltraEdit.outputWindow.write("* " + title);
- }
- function endSection() {
- UltraEdit.outputWindow.write("* ---");
- UltraEdit.outputWindow.write("");
- }
- function showArray(lines) {
- for (var i=0;i<lines.length;i++)
- UltraEdit.outputWindow.write( lines[i] );
- }
- function getDifference(set1, set2) {
- var result = [];
- for (var x in set1) {
- if (! (x in set2) ) result.push(x);
- }
- return result;
- }
- function getIntersection(set1, set2) {
- var result = [];
- for (var x in set1) {
- if (x in set2) result.push(x);
- }
- return result;
- }
- function getKeys( obj ) {
- var result = [];
- for (var x in obj) result.push( x );
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement