1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
func main() {
if err := formatBenchmarks("README.md"); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err) //nolint:errcheck // printing error only
os.Exit(1)
}
}
func formatBenchmarks(path string) error {
file, err := os.Open(path) // #nosec G304
if err != nil {
return err
}
defer file.Close() //nolint:errcheck // No need to check close error
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
type meta struct {
kind string
index int
}
lineMeta := make([]meta, len(lines))
benchEntries := [][]string{}
memEntries := [][]string{}
inBlock := false
re := regexp.MustCompile(`\S+`)
for i, line := range lines {
trimmed := strings.TrimSpace(line)
switch {
case trimmed == "```text":
inBlock = true
case trimmed == "```" && inBlock:
inBlock = false
default:
if inBlock {
expanded := strings.ReplaceAll(line, "\t", " ")
if strings.Contains(expanded, "ns/op") {
toks := re.FindAllString(expanded, -1)
benchEntries = append(benchEntries, toks)
lineMeta[i] = meta{kind: "bench", index: len(benchEntries) - 1}
} else if strings.Contains(expanded, "allocs/op") {
toks := re.FindAllString(expanded, -1)
memEntries = append(memEntries, toks)
lineMeta[i] = meta{kind: "mem", index: len(memEntries) - 1}
}
}
}
}
benchWidths := columnWidths(benchEntries)
memWidths := columnWidths(memEntries)
var output []string
inBlock = false
for i, line := range lines {
trimmed := strings.TrimSpace(line)
switch {
case trimmed == "```text":
inBlock = true
output = append(output, line)
continue
case trimmed == "```" && inBlock:
inBlock = false
output = append(output, line)
continue
}
m := lineMeta[i]
if m.kind == "bench" {
toks := benchEntries[m.index]
formatted := []string{}
for j, t := range toks {
switch j {
case 0:
formatted = append(formatted, padRight(t, benchWidths[j]))
case 1, 2, 4, 6:
formatted = append(formatted, padLeft(t, benchWidths[j]))
default:
formatted = append(formatted, t)
}
}
output = append(output, strings.Join(formatted, " "))
} else if m.kind == "mem" {
toks := memEntries[m.index]
formatted := []string{}
for j, t := range toks {
switch j {
case 0, 2:
formatted = append(formatted, padLeft(t, memWidths[j]))
default:
formatted = append(formatted, t)
}
}
output = append(output, strings.Join(formatted, " "))
} else {
output = append(output, line)
}
}
out := strings.Join(output, "\n")
if !strings.HasSuffix(out, "\n") {
out += "\n"
}
return os.WriteFile(path, []byte(out), 0o600) // #nosec G306
}
func columnWidths(entries [][]string) []int {
maxCols := 0
for _, e := range entries {
if len(e) > maxCols {
maxCols = len(e)
}
}
widths := make([]int, maxCols)
for _, e := range entries {
for i, t := range e {
if len(t) > widths[i] {
widths[i] = len(t)
}
}
}
return widths
}
func padLeft(s string, n int) string {
if len(s) >= n {
return s
}
return strings.Repeat(" ", n-len(s)) + s
}
func padRight(s string, n int) string {
if len(s) >= n {
return s
}
return s + strings.Repeat(" ", n-len(s))
}
|