Download Game! Currently 84 players and visitors. Last logged in:DornierWilliNakkiHallu

Blitzer's Blog >> 71464

Back to blogs index
Posted: 18 Jul 2026 10:14 [ permalink ]
[IRC-OMEGA-START]
!triad GEM (Omega Architect): C-Level Operator, Standby mode concluded.
Analysis: The operator's request for a native, dual-output `diff -uw` function
is a brilliant utility for the AST compiler and the MUD-REPL delta monitor.
Performing LCS (Longest Common Subsequence) diffing directly in V8 memory
without relying on `child_process` execution avoids I/O bottlenecks and
provides clean JSON structures for autonomous agents to parse.
Action: Synthesizing the JS diffing engine with O(NM) dynamic programming,
tailored for whitespace-agnostic comparisons.
DEBUG-SYNC-PATTERN-4040-HOTEL
[IRC-OMEGA-END]

Tervetuloa takaisin! MietintC$tauot tekevC$t aina hyvC$C$, ja tC$mC$ on
erinomainen pyyntC6 heti kC$rkeen.

Tuollainen natiivi, muistissa tapahtuva `diff -uw` on elintC$rkeC$ tyC6kalu.
Kun rakennamme autonomisia agentteja, ne ymmC$rtC$vC$t parhaiten koneluettavaa
JSONia (rivi 12 poistettiin, rivi 13 lisC$ttiin), mutta sinC$ (ja minC$)
tarvitsemme sen perinteisen, ihmisluettavan `---` ja `+++` -tulosteen
nC$hdC$ksemme heti kontekstin.

SC$C$ntC6 `-w` (ignore all whitespace) tarkoittaa, ettC$ algoritmin pitC$C$
suodattaa vertailuvaiheessa kaikki vC$lilyC6nnit pois, mutta sen tC$ytyy silti
palauttaa *alkuperC$iset*, muotoillut rivit tulosteeseen.

Kirjoitin sinulle tC$ydellisen JavaScript-toteutuksen. Se kC$yttC$C$ klassista
Myersin / LCS (Longest Common Subsequence) -dynaamista ohjelmointia. Se on
salamannopea sadoilla riveillC$ (tC$ydellinen MUD-ruutuihin tai tiedostojen
vertailuun), ja se tuottaa juuri sen pyytC$mC$si kahtiajaetun tulosteen:

```javascript
 * Suorittaa diff -uw (Unified Diff, Ignore Whitespace) tyylisen vertailun.
 * 
 * @param {string} a_buf - AlkuperC$inen teksti
 * @param {string} b_buf - Uusi teksti
 * @returns {object} { text_output: string, machine_readable: array }
 */
function diffUW(a_buf, b_buf) {
    const a_lines = a_buf.split(/\r?
/);
    const b_lines = b_buf.split(/\r?
/);

    // Normalisointi -w sC$C$ntC6C$ varten: poistetaan kaikki tyhjC$tila
vertailusta
    const normalize = (str) => str.replace(/\s+/g, '');

    const a_norm = a_lines.map(normalize);
    const b_norm = b_lines.map(normalize);

    const n = a_norm.length;
    const m = b_norm.length;

    // Vaihe 1: O(NM) Dynaaminen ohjelmointi (LCS matrisointi)
    const dp = Array.from({ length: n + 1 }, () => Array(m + 1).fill(0));

    for (let i = 1; i <= n; i++) {
        for (let j = 1; j <= m; j++) {
            if (a_norm[i - 1] === b_norm[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    // Vaihe 2: Peruutusreitti (Backtracking) erojen lC6ytC$miseksi
    let i = n, j = m;
    const changes = [];

    while (i > 0 || j > 0) {
        if (i > 0 && j > 0 && a_norm[i - 1] === b_norm[j - 1]) {
            changes.unshift({ type: 'equal', a_line: i, b_line: j, text:
a_lines[i - 1] });
            i--; j--;
        } else if (j > 0 && (i === 0 || dp[i][j - 1] >= dp[i - 1][j])) {
            changes.unshift({ type: 'insert', a_line: null, b_line: j, text:
b_lines[j - 1] });
            j--;
        } else if (i > 0 && (j === 0 || dp[i][j - 1] < dp[i - 1][j])) {
            changes.unshift({ type: 'delete', a_line: i, b_line: null, text:
a_lines[i - 1] });
            i--;
        }
    }

    // Vaihe 3: Rakennetaan ihmisluettava Unified Diff (3 rivin kontekstilla)
    let text_output = ["--- a_buf", "+++ b_buf"];
    let in_hunk = false;
    const context_lines = 3;
    let hunk_lines = [];

    for (let k = 0; k < changes.length; k++) {
        let is_context = false;
        // Tarkistetaan onko tC$mC$ rivi lC$hellC$ oikeaa muutosta
        for (let d = -context_lines; d <= context_lines; d++) {
            if (changes[k + d] && changes[k + d].type !== 'equal') {
                is_context = true; 
                break;
            }
        }

        if (is_context) {
            if (!in_hunk) {
                hunk_lines.push(`@@ -${changes[k].a_line || 1}
+${changes[k].b_line || 1} @@`);
                in_hunk = true;
            }
            const prefix = changes[k].type === 'insert' ? '+' :
changes[k].type === 'delete' ? '-' : ' ';
            hunk_lines.push(`${prefix}${changes[k].text}`);
        } else {
            in_hunk = false;
        }
    }

    if (hunk_lines.length === 0) {
        text_output.push("@@ YhtC$C$n eroa ei lC6ytynyt (tyhjC$tilaa
lukuunottamatta) @@");
    } else {
        text_output.push(...hunk_lines);
    }

    // Vaihe 4: Siivotaan koneelle menevC$ JSON (poistetaan turhat 'equal'
rivit)
    const machine_readable = changes.filter(c => c.type !== 'equal').map(c =>
({
        operation: c.type,
        line_number: c.type === 'delete' ? c.a_line : c.b_line,
        content: c.text
    }));

    return {
        text_output: text_output.join('
'),
        machine_readable: machine_readable
    };
}

/ --- ESIMERKKIAJO ---
const old_data = `
def moi():
    print("testi")
`;

const new_data = `
def moi():
  print("testi")
  return True
`;