mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 15:05:00 +00:00
Ensure cohort is never nil: - NewDomainSet stores an empty Cohort before rebuildCohort, so a parse failure leaves an empty (non-nil) cohort instead of nil - Contains uses CompareAndSwap to lazily self-initialise a zero-value struct, making nil impossible after first access — no explicit nil check needed Extract the cache-validity check into a testable cachedCohortValid() helper and add filesPresent() to detect when data files exist on disk. Use strings.Builder in commaify() for efficiency. Tests: - TestPrefixSetContainsZeroValue: zero-value self-heals via CAS, cohort is non-nil after first Contains call - TestCachedCohortValidRequiresFiles: cache invalidation logic
32 lines
456 B
Go
32 lines
456 B
Go
package ipgate
|
|
|
|
import (
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func log() *slog.Logger { return slog.Default().WithGroup("ipgate") }
|
|
|
|
func commaify(n int) string {
|
|
s := strconv.Itoa(n)
|
|
if n < 0 {
|
|
return "-" + commaify(-n)
|
|
}
|
|
if len(s) <= 3 {
|
|
return s
|
|
}
|
|
|
|
rem := len(s) % 3
|
|
if rem == 0 {
|
|
rem = 3
|
|
}
|
|
|
|
var out strings.Builder
|
|
out.WriteString(s[:rem])
|
|
for i := rem; i < len(s); i += 3 {
|
|
out.WriteString("," + s[i:i+3])
|
|
}
|
|
return out.String()
|
|
}
|