go-consume is a Go library for consuming strings with various matchers. It provides flexible consumers to scan input strings, making it easy to parse paths, commands, or structured text.
go get github.com/arran4/go-consumeUntilConsumer scans an input string for configured separators.
package main
import (
"fmt"
"github.com/arran4/go-consume"
"github.com/arran4/go-consume/strconsume"
)
func main() {
// Create a new UntilConsumer with separators "/"
cu := strconsume.NewUntilConsumer("/")
input := "path/to/resource"
// Consume until the first separator
matched, separator, remaining, found := cu.Consume(input)
if found {
fmt.Printf("Matched: %s\n", matched) // Output: path
fmt.Printf("Separator: %s\n", separator) // Output: /
fmt.Printf("Remaining: %s\n", remaining) // Output: to/resource
}
}PrefixConsumer checks if the input string starts with any of the configured prefixes.
package main
import (
"fmt"
"github.com/arran4/go-consume"
"github.com/arran4/go-consume/strconsume"
)
func main() {
// Create a new PrefixConsumer
pc := strconsume.NewPrefixConsumer("foo", "bar")
input := "foobar"
// Consume prefix
matched, remaining, found := pc.Consume(input)
if found {
fmt.Printf("Matched: %s\n", matched) // Output: foo
fmt.Printf("Remaining: %s\n", remaining) // Output: bar
}
}The Consume methods accept optional arguments to control behavior.
consume.Inclusive(true): Include the separator in the returnedmatchedstring. Theremainingstring will start after the separator.consume.StartOffset(n): Start scanning from indexn.consume.Ignore0PositionMatch(true): Ignore matches at the very beginning of the string (index 0).consume.CaseInsensitive(true): Match separators case-insensitively.
// Example with Inclusive(true)
matched, separator, remaining, found := cu.Consume("path/to/resource", consume.Inclusive(true))
// matched: "path/"
// remaining: "to/resource"consume.CaseInsensitive(true): Match prefixes case-insensitively.
// Example with CaseInsensitive(true)
pc := strconsume.NewPrefixConsumer("Foo")
matched, remaining, found := pc.Consume("foobar", consume.CaseInsensitive(true))
// matched: "foo" (returns the matched part from input)
// remaining: "bar"BSD 3-Clause License. See LICENSE for details.