Fossil SCM

Additional work on improved diff. Incremental check-in. Still not working right.

drh 2021-08-27 21:11 diff-color-enhancements
Commit 4fea7cc0cab8d3d436d75823a9d0091d41729709db1a6b55a3799ed149f163bf
1 file changed +37 -44
+37 -44
--- src/diff.c
+++ src/diff.c
@@ -718,54 +718,47 @@
718718
const char *zRight, int nB, /* String on the right */
719719
int *aLCS /* Identify bounds of LCS here */
720720
){
721721
const unsigned char *zA = (const unsigned char*)zLeft; /* left string */
722722
const unsigned char *zB = (const unsigned char*)zRight; /* right string */
723
- int nt; /* Number of target points */
724
- int ti[3]; /* Index for start of each 4-byte target */
725
- unsigned int target[3]; /* 4-byte alignment targets */
726
- unsigned int probe; /* probe to compare against target */
727
- int iAS, iAE, iBS, iBE; /* Range of common segment */
728
- int i, j; /* Loop counters */
729
- int rc = 0; /* Result code. 1 for success */
730
-
731
- if( nA<6 || nB<6 ) return 0;
732
- memset(aLCS, 0, sizeof(int)*4);
733
- ti[0] = i = nB/2-2;
734
- target[0] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
735
- probe = 0;
736
- if( nB<16 ){
737
- nt = 1;
738
- }else{
739
- ti[1] = i = nB/4-2;
740
- target[1] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
741
- ti[2] = i = (nB*3)/4-2;
742
- target[2] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
743
- nt = 3;
744
- }
745
- probe = (zA[0]<<16) | (zA[1]<<8) | zA[2];
746
- for(i=3; i<nA; i++){
747
- probe = (probe<<8) | zA[i];
748
- for(j=0; j<nt; j++){
749
- if( probe==target[j] ){
750
- iAS = i-3;
751
- iAE = i+1;
752
- iBS = ti[j];
753
- iBE = ti[j]+4;
754
- while( iAE<nA && iBE<nB && zA[iAE]==zB[iBE] ){ iAE++; iBE++; }
755
- while( iAS>0 && iBS>0 && zA[iAS-1]==zB[iBS-1] ){ iAS--; iBS--; }
756
- if( iAE-iAS > aLCS[1] - aLCS[0] ){
757
- aLCS[0] = iAS;
758
- aLCS[1] = iAE;
759
- aLCS[2] = iBS;
760
- aLCS[3] = iBE;
761
- rc = 1;
762
- }
763
- }
764
- }
765
- }
766
- return rc;
723
+ int i, j, k; /* Loop counters */
724
+ int lenBest = 0; /* Match length to beat */
725
+
726
+#if 0
727
+ printf("testLCS(\"%.*s\",\"%.*s\") = ",
728
+ nA, zLeft, nB, zRight);
729
+#endif
730
+
731
+ for(i=0; i<nA-lenBest; i++){
732
+ unsigned char cA = zA[i];
733
+ for(j=0; j<nB-lenBest; j++ ){
734
+ if( zB[j]==cA ){
735
+ for(k=1; j+k<nB && zB[j+k]==zA[i+k]; k++){}
736
+ if( k>lenBest ){
737
+ lenBest = k;
738
+ aLCS[0] = i;
739
+ aLCS[1] = i+k;
740
+ aLCS[2] = j;
741
+ aLCS[3] = j+k;
742
+ }
743
+ }
744
+ }
745
+ }
746
+#if 0
747
+ if( lenBest<=0 ){
748
+ printf("no-match\n");
749
+ }else{
750
+ printf(" %d,%d,%d,%d\n", aLCS[0], aLCS[1], aLCS[2], aLCS[3]);
751
+ printf("%*s%.*s", 9+aLCS[0], "", aLCS[1]-aLCS[0],
752
+ "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
753
+ printf("%*s%.*s\n", nA-aLCS[1]+3+aLCS[2], "", aLCS[3]-aLCS[2],
754
+ "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
755
+ }
756
+ fflush(stdout);
757
+#endif
758
+
759
+ return lenBest>0;
767760
}
768761
769762
/*
770763
** Find the smallest spans that different between two text strings that
771764
** are known to be different on both ends.
772765
--- src/diff.c
+++ src/diff.c
@@ -718,54 +718,47 @@
718 const char *zRight, int nB, /* String on the right */
719 int *aLCS /* Identify bounds of LCS here */
720 ){
721 const unsigned char *zA = (const unsigned char*)zLeft; /* left string */
722 const unsigned char *zB = (const unsigned char*)zRight; /* right string */
723 int nt; /* Number of target points */
724 int ti[3]; /* Index for start of each 4-byte target */
725 unsigned int target[3]; /* 4-byte alignment targets */
726 unsigned int probe; /* probe to compare against target */
727 int iAS, iAE, iBS, iBE; /* Range of common segment */
728 int i, j; /* Loop counters */
729 int rc = 0; /* Result code. 1 for success */
730
731 if( nA<6 || nB<6 ) return 0;
732 memset(aLCS, 0, sizeof(int)*4);
733 ti[0] = i = nB/2-2;
734 target[0] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
735 probe = 0;
736 if( nB<16 ){
737 nt = 1;
738 }else{
739 ti[1] = i = nB/4-2;
740 target[1] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
741 ti[2] = i = (nB*3)/4-2;
742 target[2] = (zB[i]<<24) | (zB[i+1]<<16) | (zB[i+2]<<8) | zB[i+3];
743 nt = 3;
744 }
745 probe = (zA[0]<<16) | (zA[1]<<8) | zA[2];
746 for(i=3; i<nA; i++){
747 probe = (probe<<8) | zA[i];
748 for(j=0; j<nt; j++){
749 if( probe==target[j] ){
750 iAS = i-3;
751 iAE = i+1;
752 iBS = ti[j];
753 iBE = ti[j]+4;
754 while( iAE<nA && iBE<nB && zA[iAE]==zB[iBE] ){ iAE++; iBE++; }
755 while( iAS>0 && iBS>0 && zA[iAS-1]==zB[iBS-1] ){ iAS--; iBS--; }
756 if( iAE-iAS > aLCS[1] - aLCS[0] ){
757 aLCS[0] = iAS;
758 aLCS[1] = iAE;
759 aLCS[2] = iBS;
760 aLCS[3] = iBE;
761 rc = 1;
762 }
763 }
764 }
765 }
766 return rc;
767 }
768
769 /*
770 ** Find the smallest spans that different between two text strings that
771 ** are known to be different on both ends.
772
--- src/diff.c
+++ src/diff.c
@@ -718,54 +718,47 @@
718 const char *zRight, int nB, /* String on the right */
719 int *aLCS /* Identify bounds of LCS here */
720 ){
721 const unsigned char *zA = (const unsigned char*)zLeft; /* left string */
722 const unsigned char *zB = (const unsigned char*)zRight; /* right string */
723 int i, j, k; /* Loop counters */
724 int lenBest = 0; /* Match length to beat */
725
726 #if 0
727 printf("testLCS(\"%.*s\",\"%.*s\") = ",
728 nA, zLeft, nB, zRight);
729 #endif
730
731 for(i=0; i<nA-lenBest; i++){
732 unsigned char cA = zA[i];
733 for(j=0; j<nB-lenBest; j++ ){
734 if( zB[j]==cA ){
735 for(k=1; j+k<nB && zB[j+k]==zA[i+k]; k++){}
736 if( k>lenBest ){
737 lenBest = k;
738 aLCS[0] = i;
739 aLCS[1] = i+k;
740 aLCS[2] = j;
741 aLCS[3] = j+k;
742 }
743 }
744 }
745 }
746 #if 0
747 if( lenBest<=0 ){
748 printf("no-match\n");
749 }else{
750 printf(" %d,%d,%d,%d\n", aLCS[0], aLCS[1], aLCS[2], aLCS[3]);
751 printf("%*s%.*s", 9+aLCS[0], "", aLCS[1]-aLCS[0],
752 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
753 printf("%*s%.*s\n", nA-aLCS[1]+3+aLCS[2], "", aLCS[3]-aLCS[2],
754 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
755 }
756 fflush(stdout);
757 #endif
758
759 return lenBest>0;
 
 
 
 
 
 
 
760 }
761
762 /*
763 ** Find the smallest spans that different between two text strings that
764 ** are known to be different on both ends.
765

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button