util

package
v1.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AfterColon

func AfterColon(s string) string

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

func AfterSlash(s string) string

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

func BytesToString(b []byte) string

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

func Fields(s string) []string

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

func ParseBytesStr(s string) uint64

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

func ParseDelayMs(s string) float64

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

func ParseDelayUsec(s string) float64

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

func ParseUint64(s string) uint64

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

func Split(s, sep string) []string

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

func SplitLines(s string) []string

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

func SplitN(s, sep string, n int) []string

SplitN slices s into at most n substrings separated by sep. Delegates to strings.SplitN.

func StringToBytes

func StringToBytes(s string) []byte

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

func TrimPrefix(s, prefix string) string

TrimPrefix returns s without the leading prefix p. If s does not start with p, s is returned unchanged.

func TrimRight

func TrimRight(s, cutset string) string

TrimRight returns s with all trailing characters in cutset removed.

func TrimSpace

func TrimSpace(s string) string

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

func TrimSuffix(s, suffix string) string

TrimSuffix returns s without the trailing suffix. If s does not end with suffix, s is returned unchanged.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL