refactor: replace Split in loops with more efficient SplitSeq

Signed-off-by: tinyfoolish <tinyfoolish@outlook.com>
This commit is contained in:
tinyfoolish 2025-11-10 16:00:22 +08:00
parent ebc7dc9e37
commit 7f5b0920d9
11 changed files with 24 additions and 24 deletions

View file

@ -1188,8 +1188,8 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) {
// SplitAndTrim splits input separated by a comma // SplitAndTrim splits input separated by a comma
// and trims excessive white space from the substrings. // and trims excessive white space from the substrings.
func SplitAndTrim(input string) (ret []string) { func SplitAndTrim(input string) (ret []string) {
l := strings.Split(input, ",") l := strings.SplitSeq(input, ",")
for _, r := range l { for r := range l {
if r = strings.TrimSpace(r); r != "" { if r = strings.TrimSpace(r); r != "" {
ret = append(ret, r) ret = append(ret, r)
} }
@ -1498,8 +1498,8 @@ func setGPO(ctx *cli.Context, cfg *gasprice.Config) {
func setTxPool(ctx *cli.Context, cfg *legacypool.Config) { func setTxPool(ctx *cli.Context, cfg *legacypool.Config) {
if ctx.IsSet(TxPoolLocalsFlag.Name) { if ctx.IsSet(TxPoolLocalsFlag.Name) {
locals := strings.Split(ctx.String(TxPoolLocalsFlag.Name), ",") locals := strings.SplitSeq(ctx.String(TxPoolLocalsFlag.Name), ",")
for _, account := range locals { for account := range locals {
if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) { if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) {
Fatalf("Invalid account in --txpool.locals: %s", trimmed) Fatalf("Invalid account in --txpool.locals: %s", trimmed)
} else { } else {
@ -1584,7 +1584,7 @@ func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
} }
} }
cfg.RequiredBlocks = make(map[uint64]common.Hash) cfg.RequiredBlocks = make(map[uint64]common.Hash)
for _, entry := range strings.Split(requiredBlocks, ",") { for entry := range strings.SplitSeq(requiredBlocks, ",") {
parts := strings.Split(entry, "=") parts := strings.Split(entry, "=")
if len(parts) != 2 { if len(parts) != 2 {
Fatalf("Invalid required block entry: %s", entry) Fatalf("Invalid required block entry: %s", entry)
@ -2348,7 +2348,7 @@ func MakeConsolePreloads(ctx *cli.Context) []string {
// Otherwise resolve absolute paths and return them // Otherwise resolve absolute paths and return them
var preloads []string var preloads []string
for _, file := range strings.Split(ctx.String(PreloadJSFlag.Name), ",") { for file := range strings.SplitSeq(ctx.String(PreloadJSFlag.Name), ",") {
preloads = append(preloads, strings.TrimSpace(file)) preloads = append(preloads, strings.TrimSpace(file))
} }
return preloads return preloads

View file

@ -365,7 +365,7 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine,
} }
log.Info("") log.Info("")
log.Info(strings.Repeat("-", 153)) log.Info(strings.Repeat("-", 153))
for _, line := range strings.Split(chainConfig.Description(), "\n") { for line := range strings.SplitSeq(chainConfig.Description(), "\n") {
log.Info(line) log.Info(line)
} }
log.Info(strings.Repeat("-", 153)) log.Info(strings.Repeat("-", 153))

View file

@ -255,8 +255,8 @@ type testlogger struct {
} }
func (tl *testlogger) Write(b []byte) (n int, err error) { func (tl *testlogger) Write(b []byte) (n int, err error) {
lines := bytes.Split(b, []byte("\n")) lines := bytes.SplitSeq(b, []byte("\n"))
for _, line := range lines { for line := range lines {
if len(line) > 0 { if len(line) > 0 {
tl.t.Logf("(stderr:%v) %s", tl.name, line) tl.t.Logf("(stderr:%v) %s", tl.name, line)
} }

View file

@ -217,7 +217,7 @@ func (*HandlerT) Stacks(filter *string) string {
dump := buf.String() dump := buf.String()
buf.Reset() buf.Reset()
for _, trace := range strings.Split(dump, "\n\n") { for trace := range strings.SplitSeq(dump, "\n\n") {
if ok, _ := expr.Evaluate(map[string]string{"Value": trace}); ok { if ok, _ := expr.Evaluate(map[string]string{"Value": trace}); ok {
buf.WriteString(trace) buf.WriteString(trace)
buf.WriteString("\n\n") buf.WriteString("\n\n")

View file

@ -86,7 +86,7 @@ func (h *GlogHandler) Verbosity(level slog.Level) {
// sets V to 3 in all files of any packages whose import path contains "foo" // sets V to 3 in all files of any packages whose import path contains "foo"
func (h *GlogHandler) Vmodule(ruleset string) error { func (h *GlogHandler) Vmodule(ruleset string) error {
var filter []pattern var filter []pattern
for _, rule := range strings.Split(ruleset, ",") { for rule := range strings.SplitSeq(ruleset, ",") {
// Empty strings such as from a trailing comma can be ignored // Empty strings such as from a trailing comma can be ignored
if len(rule) == 0 { if len(rule) == 0 {
continue continue
@ -113,7 +113,7 @@ func (h *GlogHandler) Vmodule(ruleset string) error {
} }
// Compile the rule pattern into a regular expression // Compile the rule pattern into a regular expression
matcher := ".*" matcher := ".*"
for _, comp := range strings.Split(parts[0], "/") { for comp := range strings.SplitSeq(parts[0], "/") {
if comp == "*" { if comp == "*" {
matcher += "(/.*)?" matcher += "(/.*)?"
} else if comp != "" { } else if comp != "" {

View file

@ -185,19 +185,19 @@ func (api *adminAPI) StartHTTP(host *string, port *int, cors *string, apis *stri
} }
if cors != nil { if cors != nil {
config.CorsAllowedOrigins = nil config.CorsAllowedOrigins = nil
for _, origin := range strings.Split(*cors, ",") { for origin := range strings.SplitSeq(*cors, ",") {
config.CorsAllowedOrigins = append(config.CorsAllowedOrigins, strings.TrimSpace(origin)) config.CorsAllowedOrigins = append(config.CorsAllowedOrigins, strings.TrimSpace(origin))
} }
} }
if vhosts != nil { if vhosts != nil {
config.Vhosts = nil config.Vhosts = nil
for _, vhost := range strings.Split(*vhosts, ",") { for vhost := range strings.SplitSeq(*vhosts, ",") {
config.Vhosts = append(config.Vhosts, strings.TrimSpace(vhost)) config.Vhosts = append(config.Vhosts, strings.TrimSpace(vhost))
} }
} }
if apis != nil { if apis != nil {
config.Modules = nil config.Modules = nil
for _, m := range strings.Split(*apis, ",") { for m := range strings.SplitSeq(*apis, ",") {
config.Modules = append(config.Modules, strings.TrimSpace(m)) config.Modules = append(config.Modules, strings.TrimSpace(m))
} }
} }
@ -263,13 +263,13 @@ func (api *adminAPI) StartWS(host *string, port *int, allowedOrigins *string, ap
} }
if apis != nil { if apis != nil {
config.Modules = nil config.Modules = nil
for _, m := range strings.Split(*apis, ",") { for m := range strings.SplitSeq(*apis, ",") {
config.Modules = append(config.Modules, strings.TrimSpace(m)) config.Modules = append(config.Modules, strings.TrimSpace(m))
} }
} }
if allowedOrigins != nil { if allowedOrigins != nil {
config.Origins = nil config.Origins = nil
for _, origin := range strings.Split(*allowedOrigins, ",") { for origin := range strings.SplitSeq(*allowedOrigins, ",") {
config.Origins = append(config.Origins, strings.TrimSpace(origin)) config.Origins = append(config.Origins, strings.TrimSpace(origin))
} }
} }

View file

@ -74,8 +74,8 @@ type originTest struct {
// and trims excessive white space from the substrings. // and trims excessive white space from the substrings.
// Copied over from flags.go // Copied over from flags.go
func splitAndTrim(input string) (ret []string) { func splitAndTrim(input string) (ret []string) {
l := strings.Split(input, ",") l := strings.SplitSeq(input, ",")
for _, r := range l { for r := range l {
r = strings.TrimSpace(r) r = strings.TrimSpace(r)
if len(r) > 0 { if len(r) > 0 {
ret = append(ret, r) ret = append(ret, r)

View file

@ -620,7 +620,7 @@ func hexFile(file string) []byte {
// Gather hex data, ignore comments. // Gather hex data, ignore comments.
var text []byte var text []byte
for _, line := range bytes.Split(fileContent, []byte("\n")) { for line := range bytes.SplitSeq(fileContent, []byte("\n")) {
line = bytes.TrimSpace(line) line = bytes.TrimSpace(line)
if len(line) > 0 && line[0] == '#' { if len(line) > 0 && line[0] == '#' {
continue continue
@ -648,7 +648,7 @@ func writeTestVector(file, comment string, data []byte) {
defer fd.Close() defer fd.Close()
if len(comment) > 0 { if len(comment) > 0 {
for _, line := range strings.Split(strings.TrimSpace(comment), "\n") { for line := range strings.SplitSeq(strings.TrimSpace(comment), "\n") {
fmt.Fprintf(fd, "# %s\n", line) fmt.Fprintf(fd, "# %s\n", line)
} }
fmt.Fprintln(fd) fmt.Fprintln(fd)

View file

@ -367,7 +367,7 @@ func parseBranch(e string) (entry, error) {
return &branchEntry{}, nil // empty entry is OK return &branchEntry{}, nil // empty entry is OK
} }
hashes := make([]string, 0, strings.Count(e, ",")) hashes := make([]string, 0, strings.Count(e, ","))
for _, c := range strings.Split(e, ",") { for c := range strings.SplitSeq(e, ",") {
if !isValidHash(c) { if !isValidHash(c) {
return nil, entryError{"branch", errInvalidChild} return nil, entryError{"branch", errInvalidChild}
} }

View file

@ -148,7 +148,7 @@ func parseTag(field Field, lastPublic int) (Tags, error) {
name := field.Name name := field.Name
tag := reflect.StructTag(field.Tag) tag := reflect.StructTag(field.Tag)
var ts Tags var ts Tags
for _, t := range strings.Split(tag.Get("rlp"), ",") { for t := range strings.SplitSeq(tag.Get("rlp"), ",") {
switch t = strings.TrimSpace(t); t { switch t = strings.TrimSpace(t); t {
case "": case "":
// empty tag is allowed for some reason // empty tag is allowed for some reason

View file

@ -92,7 +92,7 @@ func runTestScript(t *testing.T, file string) {
defer clientConn.Close() defer clientConn.Close()
go server.ServeCodec(NewCodec(serverConn), 0) go server.ServeCodec(NewCodec(serverConn), 0)
readbuf := bufio.NewReader(clientConn) readbuf := bufio.NewReader(clientConn)
for _, line := range strings.Split(string(content), "\n") { for line := range strings.SplitSeq(string(content), "\n") {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
switch { switch {
case len(line) == 0 || strings.HasPrefix(line, "//"): case len(line) == 0 || strings.HasPrefix(line, "//"):