{"record":{"id":"e941f1d130ab1785","repo":"gchq/CyberChef","slug":"error-you-can-only-calculate-the-edit-distance-be","errorCode":null,"errorMessage":"Error: You can only calculate the edit distance between 2 strings. Please ensure exactly two inputs are provided, separated by the specified delimiter.","messagePattern":"Error: You can only calculate the edit distance between 2 strings\\. Please ensure exactly two inputs are provided, separated by the specified delimiter\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/HammingDistance.mjs","lineNumber":60,"sourceCode":"                \"type\": \"option\",\n                \"value\": [\"Raw string\", \"Hex\"]\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const delim = args[0],\n            byByte = args[1] === \"Byte\",\n            inputType = args[2],\n            samples = input.split(delim);\n\n        if (samples.length !== 2) {\n            throw new OperationError(\"Error: You can only calculate the edit distance between 2 strings. Please ensure exactly two inputs are provided, separated by the specified delimiter.\");\n        }\n\n        if (samples[0].length !== samples[1].length) {\n            throw new OperationError(\"Error: Both inputs must be of the same length.\");\n        }\n\n        if (inputType === \"Hex\") {\n            samples[0] = fromHex(samples[0]);\n            samples[1] = fromHex(samples[1]);\n        } else {\n            samples[0] = new Uint8Array(Utils.strToArrayBuffer(samples[0]));\n            samples[1] = new Uint8Array(Utils.strToArrayBuffer(samples[1]));\n        }\n\n        let dist = 0;\n\n        for (let i = 0; i < samples[0].length; i++) {\n            const lhs = samples[0][i],","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/HammingDistance.mjs#L42-L78","documentation":"Thrown by Hamming Distance when the input split by the delimiter does not yield exactly two samples. Hamming distance is defined pairwise, so any count other than two is rejected before length comparison.","triggerScenarios":"Input contains 0, 1, 3+ delimiter occurrences; delimiter argument does not match the actual separator in the input (so nothing splits, or splits too many times); default delimiter is a literal '\\n\\n' but input uses a single newline.","commonSituations":"Delimiter mismatch (the arg is a binaryShortString like '\\n\\n' but data uses ',' or '\\n'); pasting only one string; trailing delimiter producing an empty third segment; using Bit/Hex mode on text that lacks the expected separator.","solutions":["Set the Delimiter argument to exactly the separator between your two strings.","Ensure the input contains precisely two strings separated once by that delimiter.","Strip any trailing delimiters or whitespace that would create extra empty segments.","If comparing many pairs, run the operation per-pair rather than batch."],"exampleFix":"// before: input uses a single newline but delimiter defaults to \"\\n\\n\"\nconst input = \"foo\\nbar\";\nrun(input, [\"\\n\\n\", \"Byte\", \"Raw string\"]);\n// after: match the delimiter to the data\nrun(\"foo\\nbar\", [\"\\n\", \"Byte\", \"Raw string\"]);","handlingStrategy":"validation","validationCode":"function splitHammingInput(input, delim) {\n  const samples = input.split(delim);\n  if (samples.length !== 2) {\n    throw new Error(`Expected exactly 2 samples, got ${samples.length}. Delimiter was ${JSON.stringify(delim)}`);\n  }\n  return samples;\n}","typeGuard":"function hasExactlyTwoSamples(input, delim) {\n  return input.split(delim).length === 2;\n}","tryCatchPattern":"try {\n  result = hamming.run(input, [delim, unit, type]);\n} catch (e) {\n  if (e instanceof OperationError && /exactly two inputs/i.test(e.message)) {\n    // delimiter mismatch - try common separators\n    for (const d of ['\\n', '\\n\\n', ',']) {\n      if (input.split(d).length === 2) { delim = d; break; }\n    }\n    result = hamming.run(input, [delim, unit, type]);\n  } else throw e;\n}","preventionTips":["Set the Delimiter argument to exactly the separator in your input.","Strip trailing delimiters that would create empty third segments.","Run the operation once per pair rather than batching many."],"tags":["hamming-distance","delimiter","argument-validation","string-metric"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}