Skip to content

Latest commit

 

History

History
72 lines (51 loc) · 1.88 KB

File metadata and controls

72 lines (51 loc) · 1.88 KB

Parsing API

The fmt package provides tools for parsing structured strings, such as Go struct tags and key-value pairs.

Struct Tag Parsing

TagValue

TagValue(key string) (string, bool)

Searches for the value of a specific key in a Go struct tag-like string.

val, found := fmt.Convert(`json:"name" Label:"Nombre"`).TagValue("Label")
// val: "Nombre", found: true

TagPairs

TagPairs(key string) []KeyValue

Parses a Go struct tag-like string and extracts multiple key-value pairs from a specific tag's value. This is useful for tags that contain comma-separated key-value pairs.

  • Pairs are separated by ,.
  • Key and value within a pair are separated by :.
  • If a pair does not contain a :, it is considered invalid and skipped.
pairs := fmt.Convert(`options:"key1:text1,key2:text2"`).TagPairs("options")
// pairs: []KeyValue{{Key: "key1", Value: "text1"}, {Key: "key2", Value: "text2"}}

Key-Value Extraction

ExtractValue

ExtractValue(delimiters ...string) (string, error)

Extracts the value part from a "key:value" string.

// Default delimiter (:)
val, err := fmt.Convert("key:value").ExtractValue()
// val: "value", err: nil

// Custom delimiter
val, err := fmt.Convert("key=value").ExtractValue("=")
// val: "value", err: nil

Types

KeyValue

Represents a simple key-value pair extracted from a string.

type KeyValue struct {
    Key   string // The identifier or key
    Value string // The associated value or text
}

Zero-Alloc Number Parsing from Bytes

For parsing numbers from byte slices without allocations (e.g., JSON parsers):

c := fmt.GetConv()
c.LoadBytes(numBytes)      // load bytes into buffer (0 alloc)
v, err := c.Int64()        // or c.Float64()
c.PutConv()                // return Conv to pool

This bypasses Convert(s ...any) which boxes the string argument. The pool ensures Conv reuse after warmup.