Loading…

Text Diff Engine

The naive way to diff two texts is comparing them index by index: line 1 of "before" against line 1 of "after", line 2 against line 2, and so on. That approach is quietly wrong the instant a single line is inserted or removed, because every line after the insertion point then sits at the wrong index and looks different even though nothing about it changed. Diffing {A,B,C} against {A,X,B,C} with an index-by-index comparison reports lines 2, 3 and 4 as all different (B vs X, C vs B, nothing vs C) - three false changes caused by one real insertion. This library computes a real Longest Common Subsequence between the two line arrays via the standard O(n*m) dynamic-programming table, then backtracks through that table to produce a genuine edit script: a sequence of EQUAL / INSERT / DELETE operations that correctly recognises B and C as unchanged and reports only the single insertion of X. This is the same underlying algorithm family used by the Unix `diff` tool and by every "compare two versions" feature in a source-control system. Typical uses: diffing two versions of an EA's saved parameter dump to see exactly what changed between optimisation runs, comparing a config file before and after an edit, or diffing two logged report snippets to highlight what a code change actually altered. The DP table is O(lineCountA * lineCountB) in both time and memory, flattened into one 1-D array since MQL4 can only ArrayResize an array's first dimension. This is intentionally scoped for the config files, parameter dumps and short report snippets this library targets - it is not meant for diffing multi-megabyte log files, where the table itself would become the memory bottleneck. Notes and limitations - This is a LINE-based diff, not a character/word-based one - a line that changes even by a single character is reported as one DELETE and one INSERT (the old line removed, the new line added), not as a highlighted in-line change. - Line comparison is an exact string match; two lines differing only by trailing whitespace are treated as genuinely different lines. - O(n*m) time and memory scales quadratically with input size - see FULL DESCRIPTION for the intended scale (config files and short reports, not large logs). - When the DP table has a tie between an insert-first and delete-first backtrack path, this implementation consistently prefers reporting the insert before the delete; both are equally valid minimal edit scripts, this just fixes which one is reported so demo output is deterministic.

Script · MT5 · v1.0 · Free

About Text Diff Engine

Text Diff Engine is a script for MetaTrader 5. MQL source code included.

Files in the download: TextDiffEngine.mq5.mqh, TextDiffEngine.txt, TextDiffEngineDemo.ex5, TextDiffEngineDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria