File: ierase.go
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 Single-file source-code for ierase.
  27 
  28 To compile a smaller-sized command-line app, you can use the `go` command as
  29 follows:
  30 
  31 go build -ldflags "-s -w" -trimpath ierase.go
  32 */
  33 
  34 package main
  35 
  36 import (
  37     "bufio"
  38     "bytes"
  39     "os"
  40     "regexp"
  41 )
  42 
  43 func main() {
  44     nerr := 0
  45     args := os.Args[1:]
  46     if len(args) == 0 {
  47         args = []string{`[^\r]`}
  48     }
  49 
  50     exprs := make([]*regexp.Regexp, 0, len(args))
  51     for _, s := range args {
  52         e, err := regexp.Compile(`(?i)` + s)
  53         if err != nil {
  54             os.Stderr.WriteString("\x1b[31m")
  55             os.Stderr.WriteString(err.Error())
  56             os.Stderr.WriteString("\x1b[0m\n")
  57             nerr++
  58         }
  59         exprs = append(exprs, e)
  60     }
  61 
  62     if nerr > 0 {
  63         os.Exit(1)
  64     }
  65 
  66     sc := bufio.NewScanner(os.Stdin)
  67     sc.Buffer(nil, 8*1024*1024*1024)
  68     bw := bufio.NewWriter(os.Stdout)
  69 
  70     var srcbuf []byte
  71     var dstbuf []byte
  72     src := srcbuf[:0]
  73     dst := dstbuf[:0]
  74 
  75     for sc.Scan() {
  76         src = append(src[:0], sc.Bytes()...)
  77         for _, e := range exprs {
  78             dst = erase(dst[:0], src, e)
  79             src = append(src[:0], dst...)
  80         }
  81 
  82         bw.Write(dst)
  83         bw.WriteByte('\n')
  84         if err := bw.Flush(); err != nil {
  85             return
  86         }
  87     }
  88 }
  89 
  90 func findANSI(s []byte) (int, int) {
  91     var prev byte
  92 
  93     for i, b := range s {
  94         if prev != '\x1b' {
  95             prev = b
  96             continue
  97         }
  98 
  99         if b == '[' {
 100             for j, b := range s[i+1:] {
 101                 if ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') {
 102                     return i - 1, i + 1 + j + 1
 103                 }
 104             }
 105             return i - 1, len(s)
 106         }
 107 
 108         if b == ']' {
 109             j := bytes.IndexByte(s[i+1:], '\a')
 110             if j < 0 {
 111                 return i - 1, len(s)
 112             }
 113             return i - 1, i + 1 + j + 1
 114         }
 115 
 116         prev = b
 117     }
 118 
 119     return -1, -1
 120 }
 121 
 122 func erase(dst []byte, src []byte, with *regexp.Regexp) []byte {
 123     for len(src) > 0 {
 124         i, j := findANSI(src)
 125         if i < 0 {
 126             return handleLineChunk(dst, src, with)
 127         }
 128 
 129         dst = handleLineChunk(dst, src[:i], with)
 130         dst = append(dst, src[i:j]...)
 131         src = src[j:]
 132     }
 133 
 134     return dst
 135 }
 136 
 137 func handleLineChunk(dst []byte, src []byte, with *regexp.Regexp) []byte {
 138     for len(src) > 0 {
 139         span := with.FindIndex(src)
 140         if span == nil {
 141             return append(dst, src...)
 142         }
 143 
 144         start := span[0]
 145         end := span[1]
 146         dst = append(dst, src[:start]...)
 147         src = src[end:]
 148     }
 149 
 150     return dst
 151 }