← All lints

hand_rolled_string_algo

L0516Idiomswarn

Hand-rolled string algorithm

manual prefix-function, Z-function, polynomial hash, or trie loop: use std.strings

Flagged
let s = "ababa"
var pi = List.filled(s.len, 0)
for i in 1..s.len:
    var j = pi[i - 1]
    while j > 0 and s[i] != s[j]: j = pi[j - 1]
    if s[i] == s[j]: j += 1
    pi[i] = j
print(pi)
Write instead
let s = "ababa"
print(prefix_function(s))

Why

Hand-rolling a KMP prefix function, Z-function, polynomial rolling hash, or trie insertion loop duplicates tested, linear-time std.strings primitives (prefix_function, z_function, RollingHash, Trie, AhoCorasick).

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: S.strings.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let s = "ababa"
var run = 0
for i in 1..s.len:
    if s[i] != s[i - 1]: run += 1
print(run)

Turn it off

For one block, wrap it in with lint(hand_rolled_string_algo: allow):. For a project, add hand_rolled_string_algo = "allow" under [lints] in cp.toml.

Generated from spec/lints/hand_rolled_string_algo, the same record the linter is built from.