Recent

Author Topic: Compare two text lines and highlight difference  (Read 22218 times)

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Compare two text lines and highlight difference
« Reply #45 on: July 13, 2025, 10:37:10 am »
Here's a Myers diff unit:
Code: Pascal  [Select][+][-]
  1. unit uMyersDiff;
  2. ...
  3.  

@Thaddy, it is suspected that the authorship of the uMyersDiff unit belongs to ChatGPT.

440bx

  • Hero Member
  • *****
  • Posts: 6391
Re: Compare two text lines and highlight difference
« Reply #46 on: July 13, 2025, 12:10:21 pm »
@Thaddy, it is suspected that the authorship of the uMyersDiff unit belongs to ChatGPT.
Now, THAT is an interesting statement.

These A.I things only produce derived works, which means it is highly dubious they could claim ownership of anything but, it's difficult not to wonder if someone or some company will eventually attempt to claim ownership of something that is the product of A.I.

"It's mine, I stole it fair and square" ;) ... reminds me of a company who tried to pocket the concept of a GUI for itself.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

simone

  • Hero Member
  • *****
  • Posts: 697
Re: Compare two text lines and highlight difference
« Reply #47 on: July 13, 2025, 12:39:48 pm »
compilation with FPC-3.2.2 requires some changes in the source code, despite the claimed compatibility with Lazarus;

In February, I submitted patches to the author, who accepted them in the following commit:

https://github.com/rickard67/TextDiff/commit/97f0bce070658c6206616bbe5041284c06008851

The changes I proposed fixed the compatibility issues with newer versions of Lazarus/Fpc.

Now, I've noticed that, unfortunately, new incompatibilities have been introduced in subsequent commits, which I'll try to fix as soon as I can.

I reported the compilation issues on Lazarus 4.0 / Fpc 3.2.2 to the author and submitted some small changes that fix them:

https://github.com/rickard67/TextDiff/issues/8

@avk: In the past, when I used this library, I too had doubts about the correctness of the results obtained with these implementations of the lcs algorithms
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 19015
  • Glad to be alive.
Re: Compare two text lines and highlight difference
« Reply #48 on: July 13, 2025, 12:56:50 pm »
Here's a Myers diff unit:
Code: Pascal  [Select][+][-]
  1. unit uMyersDiff;
  2. ...
  3.  

@Thaddy, it is suspected that the authorship of the uMyersDiff unit belongs to ChatGPT.
No. it was verified for correctness by DeepSeek, and Copilot, though. Hence you might get the same or about the same answer.
Happens all the time.
As you can see from my other answers, if I use AI to write code or partial code I always add  the source.
« Last Edit: July 13, 2025, 01:01:42 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Compare two text lines and highlight difference
« Reply #49 on: July 13, 2025, 02:02:02 pm »
...
"It's mine, I stole it fair and square" ;)
...

Isn't that how all business works? :-\
But I just meant that the code was produced by some so-called AI.

...
@Thaddy, it is suspected that the authorship of the uMyersDiff unit belongs to ChatGPT.
No. it was verified for correctness by DeepSeek, and Copilot, though.
...

However, the code seems to have a some small but very distinctive feature: it doesn't work.

Thaddy

  • Hero Member
  • *****
  • Posts: 19015
  • Glad to be alive.
Re: Compare two text lines and highlight difference
« Reply #50 on: July 13, 2025, 04:03:23 pm »
Where doesn't it work?
Because it works:
Code: Bash  [Select][+][-]
  1. - This is the third line
  2.   This is the third line
  3. - This is the fourth line
  4.   This is the fourth line
  5.  
  6. Detailed differences:
  7. Operation 1: Equal (1 lines)
  8. Old index: 2, New index: 1
  9. Operation 2: Add (1 lines)
  10. Old index: 3, New index: 3
  11. Operation 3: Delete (1 lines)
  12. Old index: 2, New index: 1
  13. Operation 4: Equal (1 lines)
  14. Old index: 2, New index: 1
  15. Operation 5: Delete (1 lines)
  16. Old index: 3, New index: 3
  17. Operation 6: Equal (1 lines)
  18. Old index: 3, New index: 3
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Compare two text lines and highlight difference
« Reply #51 on: July 13, 2025, 05:38:26 pm »
Where doesn't it work?
Because it works:
...

Judge for yourself, this simple program
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   SysUtils, uMyersDiff;
  7.  
  8. var
  9.   a: TStringArray = ('111',      '222','333',      '444', '555');
  10.   b: TStringArray = ('111','666','222','333','777','444');
  11.   I: Integer = 1;
  12.   op: TDiffOp;
  13.   s: string;
  14. const
  15.   Fmt = '%d. op: %s,'#9'old_index: %d, new_index: %d, length: %d';
  16. begin
  17.   for op in ComputeMyersDiff(a, b) do begin
  18.     WriteStr(s, op.Kind);
  19.     WriteLn(Format(Fmt, [I, s, op.OldIndex, op.NewIndex, op.Length]));
  20.     Inc(I);
  21.   end;
  22. end.
  23.  
prints as result
Code: [Select]
1. op: dEqual,  old_index: 2, new_index: 1, length: 2
2. op: dDelete, old_index: 4, new_index: 4, length: 1
3. op: dEqual,  old_index: 4, new_index: 4, length: 1
4. op: dDelete, old_index: 5, new_index: 6, length: 1
5. op: dAdd,    old_index: 4, new_index: 6, length: 1

dbannon

  • Hero Member
  • *****
  • Posts: 3783
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Compare two text lines and highlight difference
« Reply #52 on: July 14, 2025, 12:29:47 pm »
@Davo: You should have a 'LookSee' in 'StrUtils', IIRC there's an implementation hidden in there  ;)
  FindMatchesBoyerMooreCaseSensitive

Thanks Benny. I suspect Thaddy made a mistake (yes, I know, very unlikely) when he mentioned Boyer Moore, a search model. He has quietly switched to Myers Diff, a matching thing.

But my tests of Thaddy's Myers Diff reproduce AVK's results, apparently unrelated to input. I tried Thaddy's outputting method too, also not good. The code is beyond me, lots of I, J and K variables...

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Aruna

  • Hero Member
  • *****
  • Posts: 795
Re: Compare two text lines and highlight difference
« Reply #53 on: July 14, 2025, 05:11:31 pm »
Hi!

Is there any simple solution for compare two text lines and highlight difference with synedit?

If no, where am I start?

Thanks!
The attached ZIP has a fully working demo using TRichMemo (not TSynEdit). You can use it to guide you if you decide to modify or extend TSynEdit yourself. Hope this will help and good luck!

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Compare two text lines and highlight difference
« Reply #54 on: July 14, 2025, 06:18:31 pm »
...
The code is beyond me, lots of I, J and K variables...

Davo

The code is not particularly interesting, the algorithm behind it has space complexity O((N+M)*D).
But if it is really necessary, then I believe it is quite possible to fix it.

dbannon

  • Hero Member
  • *****
  • Posts: 3783
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Compare two text lines and highlight difference
« Reply #55 on: July 15, 2025, 02:24:46 am »
But if it is really necessary, then I believe it is quite possible to fix it.
Definitely not needed for me AVK, I have a system that works. Its just sometimes Thaddy comes up with quite nice solutions and I wondered if this was such a case. My solution, posted above is a whole lot shorter and easier to read, certainly slower but OK.

(eg, Thaddy convinced me of the superiority of "for in..." over the conventional "for i := 0...." recently. If only he was no quite so rude.)

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018