[Update: CutPrefix was done by https://go.dev/cl/655777; this issue is now about strings.Cut.]
Replace:
if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre)) }
->
if after, ok := strings.CutPrefix(s, pre); ok { use(after) }
Variants:
- bytes package
- CutSuffix
- if rest := strings.TrimPrefix(s, pre); rest != s { use(rest) }
-> ...
cc: @josharian
Comment From: josharian
Also strings.Cut. If there's an idx := strings.Index(s, needle)
, and then the only expressions involving idx
thereafter are idx < 0
, idx >= 0
, s[:idx]
, and s[idx+len(needle:]
(possible with len(needle)
as an int if needle is a constant), then those expressions can be replaced (as appropriate) by before, after, ok := strings.Cut(s, needle)
, ok
, before
, and after
. Also strings.IndexByte. Mutatis mutandis for bytes.Cut.
Comment From: gopherbot
Change https://go.dev/cl/655777 mentions this issue: gopls/internal/analysis/modernize: modernizer to suggest using strings.CutPrefix
Comment From: adonovan
Oops, reopening for strings.Cut.