Slice compared with a pattern
s[0..p.len] == p: s.starts_with(p); s[s.len - p.len..] == p: s.ends_with(p)
let s: Str = read()
let p: Str = read()
let i: Int = read()
print(yes(p.len <= s.len and s[0..p.len] == p))
print(yes(s[s.len - p.len..] == p))
print(yes(i + p.len <= s.len and s[i..i + p.len] == p))let s: Str = read()
let p: Str = read()
let i: Int = read()
print(yes(s.starts_with(p)))
print(yes(s.ends_with(p)))
print(yes(i + p.len <= s.len and s.starts_with(p, at: i)))Why
Python's s[:len(p)] == p and C++'s s.substr(0, p.size()) == p test a prefix by cutting a slice and comparing it. CP Lang names the test: s.starts_with(p), s.ends_with(p), and s.starts_with(p, at: i) for a match at position i. They work on Str and List alike, cost with no copy, and are false when the pattern runs past the end, where the slice traps, so the p.len <= s.len guard goes away too. The fix rewrites the comparison, guard included; with at: i it keeps an i + p.len <= s.len guard, since at outside 0..=s.len traps. It fires only on Str and List: a [D]T slice takes keys while starts_with counts positions.
The practice it serves
A hand-written pattern the language or the standard library already says in one step.
Not flagged
A near miss: close to the pattern, but not it.
let s: Str = read()
let p: Str = read()
let k: Int = read()
print(yes(s[0..k] == p))
print(yes(s[1..p.len] == p))
let a: [1..=3]Int = read()
let b = [a[1], a[2]]
print(yes(a[1..3] == b))Turn it off
For one block, wrap it in with lint(manual_starts_with: allow):. For a project, add manual_starts_with = "allow" under [lints] in cp.toml.
Generated from spec/lints/manual_starts_with, the same record the linter is built from.