Skip to content

Conversation

@milos-lk
Copy link
Contributor

@milos-lk milos-lk commented Sep 8, 2025

Introducing small helper to extract typed values from strings via regex (e.g., Uint64, Int64, Bool, DurationNs, String).
Syntactic sugar.

An example of a function implementation this change:

func parseStatsString(s string) (OpusDecStats, error) {
	var st OpusDecStats

	getU64 := func(k string) (uint64, error) {
		if m := reU64[k].FindStringSubmatch(s); len(m) == 2 {
			return strconv.ParseUint(m[1], 10, 64)
		}
		return 0, fmt.Errorf("missing %s", k)
	}
	getU32 := func(k string) (uint32, error) {
		if m := reU32[k].FindStringSubmatch(s); len(m) == 2 {
			v, _ := strconv.ParseUint(m[1], 10, 32)
			return uint32(v), nil
		}
		return 0, fmt.Errorf("missing %s", k)
	}

	// Required
	if v, err := getU64("num-pushed"); err == nil {
		st.NumPushed = v
	}
	if v, err := getU64("num-gap"); err == nil {
		st.NumGap = v
	}
	return st, nil
}

and after:

func parseStatsString(s string) (OpusDecStats, error) {
	kv := utils.NewKVRegexScanner(serialized)

	var st OpusDecStats
	if v, ok := kv.Uint64(rePushed); ok { st.NumPushed = v }
	if v, ok := kv.Uint64(reGap); ok    { st.NumGap = v }
	if v, ok := kv.Uint64(rePLCs); ok   { st.PlcNumSamples = v }
	if d, ok := kv.DurationNs(rePLCn); ok { st.PlcDuration = d }
	return st, nil
}

@changeset-bot
Copy link

changeset-bot bot commented Sep 8, 2025

⚠️ No Changeset found

Latest commit: e1742e5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "github.com/livekit/protocol" specified in the `fixed` option does not match any package in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.

@milos-lk milos-lk merged commit 3db0798 into main Sep 8, 2025
7 checks passed
@milos-lk milos-lk deleted the kv-regex-scan branch September 8, 2025 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants