v0.1.3516 made gateway's and redact's patterns lazy. The other 299 package-level regexp.MustCompile calls, in 148 files across agent, provider, gui, library, codexcat, sessions, usage, plugin and the rest, now go through lazyre.New too, so a launch that never reaches them (`magpie statusline`, run by Claude Code on every message) doesn't compile them. Fields, maps and params that held them are *lazyre.RE. Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build (t42) against this one (t43): - GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765 - `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M, peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30 changed packages ok; GOOS=linux go vet ok except ., internal/cli and internal/gui, which stop in wails v3's cgo files when cross-compiled (the same at main). New TestAllLazyPatternsCompile at the root compiles all 574 registered patterns (ok), so a bad pattern fails a test rather than a first use. Not run before push: the changed packages' own test suites; they run on mote after it.
126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
package edit
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/lazyre"
|
|
)
|
|
|
|
// TOML and top-level YAML entries are located by their parsers (toml.go and
|
|
// yaml_top.go), then edited without reformatting the rest of the file.
|
|
|
|
var (
|
|
// tomlTable and tomlKV only read a TOML file its parser refuses; see
|
|
// GetTOMLTop.
|
|
tomlTable = lazyre.New(`^\s*\[`)
|
|
tomlKV = lazyre.New(`^\s*([A-Za-z0-9_.-]+|"[^"]*")\s*=\s*(.*?)\s*$`)
|
|
)
|
|
|
|
// setLine replaces the line whose key matches, or inserts newLine after the
|
|
// last key line in the header section (before the first line matching stop).
|
|
func setLine(lines []string, key, newLine string, stop *regexp.Regexp, keyOf func(string) (string, bool)) []string {
|
|
lastKey, same := -1, -1
|
|
for i, line := range lines {
|
|
if stop != nil && stop.MatchString(line) {
|
|
break
|
|
}
|
|
k, ok := keyOf(line)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if k == key {
|
|
same = i
|
|
}
|
|
lastKey = i
|
|
}
|
|
if same >= 0 {
|
|
// The last line for a key that is set twice is the one a reader uses.
|
|
lines[same] = newLine
|
|
return lines
|
|
}
|
|
at := lastKey + 1
|
|
out := make([]string, 0, len(lines)+1)
|
|
out = append(out, lines[:at]...)
|
|
out = append(out, newLine)
|
|
out = append(out, lines[at:]...)
|
|
return out
|
|
}
|
|
|
|
// unquote strips a leading quoted scalar (and anything after it, such as an
|
|
// inline comment); ok is false when v does not start with a quote.
|
|
func unquote(v string) (string, bool) {
|
|
if v == "" || (v[0] != '"' && v[0] != '\'') {
|
|
return "", false
|
|
}
|
|
q := v[0]
|
|
for i := 1; i < len(v); i++ {
|
|
if q == '"' && v[i] == '\\' {
|
|
i++
|
|
continue
|
|
}
|
|
if v[i] == q {
|
|
if q == '"' {
|
|
if s, err := strconv.Unquote(v[:i+1]); err == nil {
|
|
return s, true
|
|
}
|
|
}
|
|
return v[1:i], true
|
|
}
|
|
}
|
|
return v[1:], true
|
|
}
|
|
|
|
func tomlValue(v string) string {
|
|
if s, ok := unquote(v); ok {
|
|
return s
|
|
}
|
|
if i := strings.Index(v, "#"); i >= 0 {
|
|
v = strings.TrimSpace(v[:i])
|
|
}
|
|
return v
|
|
}
|
|
|
|
func toString(v any) string {
|
|
switch x := v.(type) {
|
|
case string:
|
|
return x
|
|
case bool:
|
|
return strconv.FormatBool(x)
|
|
case int:
|
|
return strconv.Itoa(x)
|
|
}
|
|
return fmt.Sprint(v)
|
|
}
|
|
|
|
// splitLines splits on '\n' but keeps a trailing newline as an empty final
|
|
// element so joinLines can restore the file exactly.
|
|
func splitLines(s string) []string {
|
|
if s == "" {
|
|
return []string{""}
|
|
}
|
|
return strings.Split(s, "\n")
|
|
}
|
|
|
|
// joinLinesLike is joinLines for lines edited out of orig: when orig used
|
|
// \r\n for every line break, the lines added or replaced get \r\n too, so the
|
|
// file never ends up with mixed endings. Untouched lines already carry their
|
|
// \r, and a file that mixed endings is left as it was.
|
|
func joinLinesLike(lines []string, orig string) string {
|
|
s := joinLines(lines)
|
|
if n := strings.Count(orig, "\n"); n > 0 && strings.Count(orig, "\r\n") == n {
|
|
s = strings.ReplaceAll(strings.ReplaceAll(s, "\r\n", "\n"), "\n", "\r\n")
|
|
}
|
|
return s
|
|
}
|
|
|
|
func joinLines(lines []string) string {
|
|
s := strings.Join(lines, "\n")
|
|
if !strings.HasSuffix(s, "\n") {
|
|
s += "\n"
|
|
}
|
|
return s
|
|
}
|