summaryrefslogtreecommitdiff
path: root/internal/stats
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-16 03:54:28 +0200
committerPaul Buetow <paul@buetow.org>2026-03-16 03:54:28 +0200
commit93dfe3798e03e74766b229418cde364a5ef29ae9 (patch)
tree4438058be9bee9a9b8682148a1f8417a530fac2f /internal/stats
parent8d9d903852a2cd28034e00e116a7bfcc14783108 (diff)
Replace custom stringsTrim with strings.TrimSpace
Remove duplicate stringsTrim implementations in stats and tmux packages, replacing all call sites with the standard library strings.TrimSpace. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/stats')
-rw-r--r--internal/stats/stats.go19
-rw-r--r--internal/stats/stats_test.go11
2 files changed, 8 insertions, 22 deletions
diff --git a/internal/stats/stats.go b/internal/stats/stats.go
index 4b05617..939e1aa 100644
--- a/internal/stats/stats.go
+++ b/internal/stats/stats.go
@@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"strconv"
+ "strings"
"sync/atomic"
"time"
)
@@ -264,7 +265,7 @@ func TakeSnapshot() (Snapshot, error) {
// CacheDir resolves the cache directory for stats.
func CacheDir() (string, error) {
- if x := os.Getenv("XDG_CACHE_HOME"); stringsTrim(x) != "" {
+ if x := os.Getenv("XDG_CACHE_HOME"); strings.TrimSpace(x) != "" {
return filepath.Join(x, "hexai"), nil
}
home, err := os.UserHomeDir()
@@ -274,22 +275,6 @@ func CacheDir() (string, error) {
return filepath.Join(home, ".local", "hexai", "cache"), nil
}
-// stringsTrim is a tiny helper to avoid importing strings everywhere here.
-func stringsTrim(s string) string {
- i := 0
- j := len(s)
- for i < j && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') {
- i++
- }
- for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') {
- j--
- }
- if i == 0 && j == len(s) {
- return s
- }
- return s[i:j]
-}
-
// DebugString returns a compact single-line view of a snapshot (useful for logs).
func (s Snapshot) DebugString() string {
return "Σ reqs=" + strconv.FormatInt(s.Global.Reqs, 10) + " rpm=" + fmt.Sprintf("%.2f", s.RPM)
diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go
index 75c1c5b..45f9e2a 100644
--- a/internal/stats/stats_test.go
+++ b/internal/stats/stats_test.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path/filepath"
+ "strings"
"sync"
"testing"
"time"
@@ -102,7 +103,7 @@ func TestCacheDir_FallbackHome(t *testing.T) {
}
// TestCacheDir_WhitespaceXDG covers the branch where XDG_CACHE_HOME contains
-// only whitespace, which stringsTrim reduces to "" so the fallback is used.
+// only whitespace, which strings.TrimSpace reduces to "" so the fallback is used.
func TestCacheDir_WhitespaceXDG(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", " \t\n ")
got, err := CacheDir()
@@ -140,7 +141,7 @@ func TestSetWindow_ClampHigh(t *testing.T) {
// has no leading or trailing whitespace, so the original string is returned.
func TestStringsTrim_NoTrimNeeded(t *testing.T) {
in := "hello"
- got := stringsTrim(in)
+ got := strings.TrimSpace(in)
if got != "hello" {
t.Fatalf("expected %q, got %q", "hello", got)
}
@@ -148,7 +149,7 @@ func TestStringsTrim_NoTrimNeeded(t *testing.T) {
// TestStringsTrim_AllWhitespace covers trimming a string that is entirely whitespace.
func TestStringsTrim_AllWhitespace(t *testing.T) {
- got := stringsTrim(" \t\r\n ")
+ got := strings.TrimSpace(" \t\r\n ")
if got != "" {
t.Fatalf("expected empty, got %q", got)
}
@@ -156,7 +157,7 @@ func TestStringsTrim_AllWhitespace(t *testing.T) {
// TestStringsTrim_LeadingAndTrailing covers trimming from both ends.
func TestStringsTrim_LeadingAndTrailing(t *testing.T) {
- got := stringsTrim(" abc ")
+ got := strings.TrimSpace(" abc ")
if got != "abc" {
t.Fatalf("expected %q, got %q", "abc", got)
}
@@ -164,7 +165,7 @@ func TestStringsTrim_LeadingAndTrailing(t *testing.T) {
// TestStringsTrim_Empty covers the empty string edge case.
func TestStringsTrim_Empty(t *testing.T) {
- got := stringsTrim("")
+ got := strings.TrimSpace("")
if got != "" {
t.Fatalf("expected empty, got %q", got)
}