Documentation
¶
Overview ¶
Package wfp2 generates WFP2 (token-aware) fingerprint minutiae: a language-aware winnowing that detects the source language, strips comments and normalizes the source to a byte stream (identifiers, type-runs, reserved words and punctuation each mapped to fixed codes) before winnowing it. Cosmetic edits — renamed identifiers, reformatting, changed comments — leave the fingerprint unchanged, which is what lets wfp2 match snippets wfp1 would miss.
This is a faithful port of the reference implementation (scanoss-go pkg/fingerprint/wfp2), which the engine mirrors byte-for-byte in C (engine src/wfp2.c) and which the mined KB was generated with. The output MUST stay byte-identical to both, so the algorithm here — the normalization tables, GRAM/WINDOW, the CRC32C winnowing and the little-endian min-hash re-hash — is reproduced verbatim and must not be "improved". The sole deliberate departure is a deterministic language tie-break; see DetectLanguage.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DetectLanguage ¶
DetectLanguage returns the best-scoring language's key, or "" when nothing matches (which leaves the source uncommented-stripped — see RemoveCommentsFromSource). Scoring counts every (token, keyword) hit where the token equals or is prefixed by the keyword, exactly as the reference does.
func Minutiae ¶
Minutiae computes the wfp2 minutiae of a source file: a map from line number to the CRC32C minutiae recorded on that line. It runs the full pipeline — DetectLanguage → RemoveCommentsFromSource → Normalizex → winnowing — then, per window, hashes the little-endian bytes of the window's minimum gram-hash and records it when it differs from the previous one (consecutive duplicates are dropped).
The returned map matches the reference's line→hashes map exactly; callers format it as the "N=hash,hash,..." minutiae lines of the WFP (see pkg/wfp).
func Normalizex ¶
Normalizex maps source text to the wfp2 normalized byte stream, line by line:
- a reserved word or a single punctuation char → its elementsMap byte,
- a type keyword → 0x00 (its identity is dropped; the lastWasType guard suppresses a repeated 0x00 only for an immediately-adjacent type, which separators prevent),
- any other identifier → 0xff,
- characters not in elementsMap (whitespace and the like) → dropped,
- newlines preserved between lines (so winnowing can number lines).
elementsMap is consulted before typesMap, so a token that is both takes its element code. Input is lower-cased first, exactly as the reference does.
func RemoveCommentsFromSource ¶
RemoveCommentsFromSource strips comments from code for the given language, preserving the line count: a line that becomes empty is kept as an empty line, so line numbers in the fingerprint still line up with the source. An unknown language returns the code unchanged. The algorithm is a line-by-line port of the reference and must track its trimming exactly.
func SortedLines ¶
SortedLines returns the line numbers of a minutiae map in ascending order — the order the minutiae must be emitted in.
Types ¶
This section is empty.