/* The MIT License (MIT) Copyright (c) 2026 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath erase.go */ package main import ( "bufio" "io" "os" "regexp" ) const info = ` erase [options...] [regexes...] Ignore/remove all occurrences of all regex matches along lines read from the standard input. The regular-expression mode used is "re2", which is a superset of the commonly-used "extended-mode". Regexes always avoid matching any ANSI-style sequences, to avoid messing those up. Each regex erases all its occurrences on the current line in the order given among the arguments, so regex-order matters. The options are, available both in single and double-dash versions -h show this help message -help show this help message -i match regexes case-insensitively -ins match regexes case-insensitively ` func main() { args := os.Args[1:] buffered := false insensitive := false out: for len(args) > 0 { switch args[0] { case `-b`, `--b`, `-buffered`, `--buffered`: buffered = true args = args[1:] case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return case `-i`, `--i`, `-ins`, `--ins`: insensitive = true args = args[1:] default: break out } } if len(args) > 0 && args[0] == `--` { args = args[1:] } exprs := make([]*regexp.Regexp, 0, len(args)) for _, s := range args { var err error var exp *regexp.Regexp if insensitive { exp, err = regexp.Compile(`(?i)` + s) } else { exp, err = regexp.Compile(s) } if err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") continue } exprs = append(exprs, exp) } // quit right away when given invalid regexes if len(exprs) < len(args) { os.Exit(1) } liveLines := !buffered if !buffered { if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { liveLines = false } } if err := run(os.Stdout, os.Stdin, exprs, liveLines); err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") os.Exit(1) } } func run(w io.Writer, r io.Reader, exprs []*regexp.Regexp, live bool) error { sc := bufio.NewScanner(r) sc.Buffer(nil, 8*1024*1024*1024) bw := bufio.NewWriter(w) defer bw.Flush() var src []byte var dst []byte for i := 0; sc.Scan(); i++ { s := sc.Bytes() if i == 0 && len(s) > 2 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf { s = s[3:] } src = append(src[:0], s...) for _, e := range exprs { dst = erase(dst[:0], src, e) src = append(src[:0], dst...) } bw.Write(dst) if err := bw.WriteByte('\n'); err != nil { return nil } if !live { continue } if err := bw.Flush(); err != nil { return nil } } return sc.Err() } func erase(dst []byte, src []byte, with *regexp.Regexp) []byte { for len(src) > 0 { i, j := indexEscapeSequence(src) if i < 0 { dst = handleLineChunk(dst, src, with) break } if j < 0 { j = len(src) } dst = handleLineChunk(dst, src[:i], with) dst = append(dst, src[i:j]...) src = src[j:] } return dst } func handleLineChunk(dst []byte, src []byte, with *regexp.Regexp) []byte { for len(src) > 0 { span := with.FindIndex(src) // also ignore empty regex matches to avoid infinite outer loops, // as skipping empty slices isn't advancing at all, leaving the // string stuck to being empty-matched forever by the same regex if len(span) != 2 || span[0] == span[1] { return append(dst, src...) } start := span[0] end := span[1] dst = append(dst, src[:start]...) // avoid infinite loops caused by empty regex matches if start == end && end < len(src) { dst = append(dst, src[end]) end++ } src = src[end:] } return dst } // indexEscapeSequence finds the first ANSI-style escape-sequence, which is // the multi-byte sequences starting with ESC[; the result is a pair of slice // indices which can be independently negative when either the start/end of // a sequence isn't found; given their fairly-common use, even the hyperlink // ESC]8 sequences are supported func indexEscapeSequence(s []byte) (int, int) { var prev byte for i, b := range s { if prev == '\x1b' && b == '[' { j := indexLetter(s[i+1:]) if j < 0 { return i, -1 } return i - 1, i + 1 + j + 1 } if prev == '\x1b' && b == ']' && i+1 < len(s) && s[i+1] == '8' { j := indexPair(s[i+1:], '\x1b', '\\') if j < 0 { return i, -1 } return i - 1, i + 1 + j + 2 } prev = b } return -1, -1 } func indexLetter(s []byte) int { for i, b := range s { upper := b &^ 32 if 'A' <= upper && upper <= 'Z' { return i } } return -1 } func indexPair(s []byte, x byte, y byte) int { var prev byte for i, b := range s { if prev == x && b == y { return i } prev = b } return -1 }