Documentation
¶
Overview ¶
Package util provides shared, allocation-minimising string and byte- conversion helpers used across multiple packages in cake-stats.
Design constraints:
- Every exported function is a pure transform (no package-level mutable state) and is therefore safe for concurrent use without synchronisation.
- Zero-allocation helpers here use unsafe.String / unsafe.Slice (Go 1.20+). Read the safety contracts on each function before use.
Index ¶
- func AfterColon(s string) string
- func AfterSlash(s string) string
- func BytesToString(b []byte) string
- func Fields(s string) []string
- func ParseBytesStr(s string) uint64
- func ParseDelayMs(s string) float64
- func ParseDelayUsec(s string) float64
- func ParseUint64(s string) uint64
- func Split(s, sep string) []string
- func SplitLines(s string) []string
- func SplitN(s, sep string, n int) []string
- func StringToBytes(s string) []byte
- func TrimPrefix(s, prefix string) string
- func TrimRight(s, cutset string) string
- func TrimSpace(s string) string
- func TrimSuffix(s, suffix string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AfterColon ¶
AfterColon returns the whitespace-trimmed substring that follows the first ':' in s. Returns "" when no ':' is present or when the remainder is entirely whitespace.
This pattern is extremely common in tc output lines, for example:
"capacity estimate: 100Mbit" → "100Mbit" "average network hdr offset: 14" → "14" "memory used: 14Kb of 4Mb" → "14Kb of 4Mb"
func AfterSlash ¶
AfterSlash returns the whitespace-trimmed substring that follows the first '/' in s. Returns "" when no '/' is present.
Used to extract the high half of "min/max" range lines such as "min/max network layer size: 28 / 1500".
func BytesToString ¶
BytesToString converts a byte slice to a string without a heap copy.
Safety contract: the returned string MUST NOT be used after the source slice is modified. The GC keeps the backing array alive as long as strings derived from it are reachable, so the risk is mutation, not premature collection.
Intended use: passing exec.Command.Output() bytes to a parser that holds no string references past its own call stack (e.g. CollectStats in the parser package).
func Fields ¶
Fields splits s around runs of whitespace and returns a slice of substrings, identical to strings.Fields.
Centralised here so that all whitespace-splitting in the hot parsing path flows through one place, enabling a future replacement with a lower- allocation implementation (e.g. iterator-style or arena-backed) without touching any call site.
func ParseBytesStr ¶
ParseBytesStr parses a tc-style byte-count string (e.g. "238656b", "4Kb", "32Mb", "1Gb") and returns the absolute byte count as uint64. Only the exact case-sensitive suffixes that iproute2 emits are recognised:
"Gb" → ×1 GiB, "Mb" → ×1 MiB, "Kb" → ×1 KiB, "b" → ×1
Returns 0 for empty or unrecognised input.
func ParseDelayMs ¶
ParseDelayMs converts a tc delay string to float64 milliseconds.
Recognised suffixes: "us" (microseconds), "ms" (milliseconds), "s" (seconds). The function handles decimal values such as "1.5ms". Returns 0 for empty input, the bare string "0", or unrecognised suffixes.
This is the single canonical implementation; it replaces the duplicate cakeParseDelayUsec (parser package) and parseDelayMs (history package) that previously drifted apart.
func ParseDelayUsec ¶
ParseDelayUsec is like ParseDelayMs but returns microseconds. Provided for callers that need usec precision (e.g. "worst-case delay" comparisons inside the parser's tier-aggregation path).
func ParseUint64 ¶
ParseUint64 converts a numeric string to uint64, stripping any trailing byte/packet unit suffix characters (b, B, k, K, m, M, g, G, p, P) that tc/iproute2 appends to counter values (e.g. "1024b", "42p", "5Mb"). Returns 0 for empty or non-numeric input; never panics.
func Split ¶
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators. Delegates to strings.Split.
func SplitLines ¶
SplitLines splits s on newline boundaries and trims a single trailing empty element that strings.Split would produce when s ends with '\n'. This avoids an off-by-one when iterating over the output of commands like `tc -s qdisc`.
func SplitN ¶
SplitN slices s into at most n substrings separated by sep. Delegates to strings.SplitN.
func StringToBytes ¶
StringToBytes returns the backing memory of s as a byte slice without allocation. The caller MUST NOT write to the returned slice; doing so violates Go's string immutability guarantee and causes undefined behaviour.
Intended use: passing a string literal or an already-owned string to a write-only sink (hash, length check, etc.) where a copy is not needed.
func TrimPrefix ¶
TrimPrefix returns s without the leading prefix p. If s does not start with p, s is returned unchanged.
func TrimSpace ¶
TrimSpace returns s without leading and trailing Unicode whitespace. Delegates to strings.TrimSpace; centralised here so callers import only this package for string utilities and to allow future SIMD-backed swaps.
func TrimSuffix ¶
TrimSuffix returns s without the trailing suffix. If s does not end with suffix, s is returned unchanged.
Types ¶
This section is empty.