Skip to content

[Repo Assist] refactor: replace strings.SplitN(x, sep, 2) with strings.Cut#7628

Merged
lpcox merged 1 commit into
mainfrom
repo-assist/improve-strings-cut-consistency-e89d22bc1a797254
Jun 16, 2026
Merged

[Repo Assist] refactor: replace strings.SplitN(x, sep, 2) with strings.Cut#7628
lpcox merged 1 commit into
mainfrom
repo-assist/improve-strings-cut-consistency-e89d22bc1a797254

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This PR is from Repo Assist, an automated AI assistant for this repository.

Summary

Three call-sites across the codebase used the pattern:

parts := strings.SplitN(x, sep, 2)
if len(parts) == 2 {
    // use parts[0] and parts[1]
}

The idiomatic Go 1.18+ equivalent is strings.Cut, which already appears in 5+ other files in this codebase (server/session.go, proxy/proxy.go, tracing/). This PR applies it consistently in the remaining three locations.

Changes

File Function Before After
internal/sanitize/sanitize.go SanitizeArgs strings.Contains + strings.SplitN + index access strings.Cut + ok check + continue pattern
internal/envutil/envfile.go LoadEnvFile strings.SplitN(line, "=", 2) + len(parts) != 2 strings.Cut(line, "=") + !ok
internal/logger/logger.go computeEnabled strings.SplitN(pattern, "*", 2) + len(parts) == 2 strings.Cut(pattern, "*") + ok

All three are semantically identical: strings.Cut(s, sep) returns (before, after string, found bool), splitting on the first occurrence of sep — exactly matching the SplitN(s, sep, 2) + len == 2 guard pattern. Additionally, strings.Cut avoids an intermediate slice allocation.

Rationale

  • Consistency: the project already uses strings.Cut widely; these were the remaining outliers
  • Readability: named return values (before, after, ok) are more expressive than parts[0], parts[1], len(parts) == 2
  • Minor efficiency: no intermediate []string allocation

No behaviour changes. No new dependencies.

Test Status

⚠️ Infrastructure note: proxy.golang.org is blocked by the environment firewall for this workflow run, preventing go build/go test from resolving modules. This is a pre-existing infrastructure constraint affecting all Repo Assist runs — see prior PRs for context. The changes are pure refactors (same semantics, no logic changes) and are syntactically verified: grep confirms all three strings.Cut replacements are in place and strings.SplitN is no longer present at any of these three call-sites.

Warning

Firewall blocked 6 domains

The following domains were blocked by the firewall during workflow execution:

  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Repo Assist · 2.6K AIC · ⊞ 50.2K ·
Comment /repo-assist to run again

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

Three call-sites used strings.SplitN(x, sep, 2) followed by a
len(parts) == 2 guard — the idiomatic Go 1.18+ equivalent is
strings.Cut, which already appears in several other files in this
codebase (server/session.go, proxy/proxy.go, tracing/).

Changes:
- internal/sanitize/sanitize.go (SanitizeArgs): replace
  strings.Contains+SplitN with strings.Cut; restructure to use
  continue so the else branch is eliminated
- internal/envutil/envfile.go (LoadEnvFile): replace SplitN+len
  guard with strings.Cut+ok check; reassign key/value vars in place
- internal/logger/logger.go (computeEnabled): replace SplitN+len
  guard with strings.Cut+ok for middle-wildcard pattern handling

All three are semantically identical — strings.Cut splits at the
first occurrence of the separator and returns (before, after, found),
exactly matching SplitN(x, sep, 2) / len(parts)==2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review June 16, 2026 17:05
Copilot AI review requested due to automatic review settings June 16, 2026 17:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors three existing string-splitting call sites to use strings.Cut instead of strings.SplitN(..., 2) + length checks, aligning these spots with the codebase’s existing idiomatic usage and avoiding intermediate slice allocations.

Changes:

  • Updated SanitizeArgs to use strings.Cut for parsing VAR=VALUE after -e.
  • Updated .env parsing in LoadEnvFile to use strings.Cut for KEY=VALUE.
  • Updated debug-pattern wildcard parsing in matchPattern to use strings.Cut for middle-wildcard patterns.
Show a summary per file
File Description
internal/sanitize/sanitize.go Refactors -e VAR=VALUE parsing to strings.Cut with early-continue.
internal/envutil/envfile.go Refactors KEY=VALUE parsing to strings.Cut while preserving first-= semantics.
internal/logger/logger.go Refactors middle-wildcard (prefix*suffix) parsing to strings.Cut.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 321e7d6 into main Jun 16, 2026
24 checks passed
@lpcox lpcox deleted the repo-assist/improve-strings-cut-consistency-e89d22bc1a797254 branch June 16, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants