provider, library: two package-level regexps compile on first use

TestNoEagerPackageRegexps (root package) was red on main: fc615339f
added aliWindowField in internal/provider/alibaba_usage.go and
e174b5515 added envName in internal/library/env.go, both with
regexp.MustCompile at package level. Both now use lazyre.New; each has
one call site (FindStringSubmatch, MatchString), which lazyre has.

Run (WSL magpie-desktop, origin/main 2bb201162 + this):
- red first, unchanged main: TestNoEagerPackageRegexps named
  env.go:57:15 and alibaba_usage.go:374:22.
- after: gofmt -l clean; go vet -tags nogui ./internal/provider
  ./internal/library ok; -run TestNoEagerPackageRegexps . ok;
  ./internal/library ok; -run 'Ali|Alibaba' ./internal/provider ok.
This commit is contained in:
yetone
2026-11-12 02:14:21 +08:00
parent e7980ed955
commit 4847a087fb
2 changed files with 5 additions and 4 deletions
+2 -2
View File
@@ -7,7 +7,6 @@ import (
"maps"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"unicode/utf8"
@@ -16,6 +15,7 @@ import (
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/lazyre"
)
// Environment variables (paopao_baobao on X) are set once in the library
@@ -54,7 +54,7 @@ const (
maxEnvValue = 8192
)
var envName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
var envName = lazyre.New(`^[A-Za-z_][A-Za-z0-9_]*$`)
func (e *EnvVar) check() error {
if !envName.MatchString(e.Name) || len(e.Name) > maxEnvName {
+3 -2
View File
@@ -38,12 +38,13 @@ import (
"io"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/lazyre"
)
const (
@@ -371,7 +372,7 @@ func aliGateway(ctx context.Context, token string, site aliSite) ([]byte, error)
return node, nil
}
var aliWindowField = regexp.MustCompile(`^per(\d+)(Hour|Day|Week|Month)(Percentage|ResetTime)$`)
var aliWindowField = lazyre.New(`^per(\d+)(Hour|Day|Week|Month)(Percentage|ResetTime)$`)
// aliReadWindows reads the per<N><Unit>Percentage and ResetTime pairs of
// the usage data as windows, the shortest first. Percentage is the share