To avoid acme making spurious edits in future changes,
edited all text files to remove end-of-line spaces and tabs,
remove trailing blank lines, and add missing final newlines,
using this Go program.
package main
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"unicode/utf8"
)
var blank = []byte("\n\n")
func main() {
log.SetPrefix("fixspace: ")
log.SetFlags(0)
re := regexp.MustCompile(`(?m)[ \t]+$`)
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Print(err)
return nil
}
if info.IsDir() {
return nil
}
old, err := os.ReadFile(path)
if err != nil {
return err
}
if !utf8.Valid(old) || bytes.ContainsAny(old, "\x00\r") {
return nil
}
// Remove trailing spaces at ends of lines.
new := re.ReplaceAll(old, nil)
// Remove trailing blank lines.
for len(new) > 0 && bytes.HasSuffix(new, blank) {
new = new[:len(new)-1]
}
if len(new) == 1 && new[0] == '\n' {
new = nil
}
// Add newline at end of line if needed.
if len(new) > 0 && new[len(new)-1] != '\n' {
new = append(new, '\n')
}
if !bytes.Equal(old, new) {
if err := os.WriteFile(path, new, 0666); err != nil {
log.Print(err)
return nil
}
fmt.Printf("%s\n", path)
}
return nil
})
}
2 B
Executable File
2 B
Executable File