Documentation
¶
Index ¶
- Constants
- func AtoI64(num string) (int64, error)
- func AtoU64(num string) (uint64, error)
- func Btoi(buffer []byte) (uint64, int)
- func EscapeJSON(text string) string
- func GetNumBase(num string) (string, int)
- func IndexN(s, sep string, n int) int
- func IsInt(s string) bool
- func IsJSON(data []byte) (bool, error, *json.RawMessage)
- func ItoLen(number uint64) int
- func Itob(number uint64, buffer []byte) error
- func Itobe(number uint64, buffer []byte) error
- func JoinStringers(a []fmt.Stringer, sep string) string
- func LastIndexN(s, sep string, n int) int
- func TrimToNumber(text string) string
- func Unescape(text string) string
- type ByteRef
- type ParsedFunc
- type ParserFlag
- type ParserStateID
- type StringRef
- type Transition
- type TransitionDirective
- type TransitionParser
- func (parser *TransitionParser) Add(stateName string, token string, nextStateName string, flags ParserFlag, ...)
- func (parser *TransitionParser) AddDirectives(directives []TransitionDirective)
- func (parser *TransitionParser) AddTransition(stateName string, newTrans Transition, token string)
- func (parser *TransitionParser) GetStateID(stateName string) ParserStateID
- func (parser *TransitionParser) GetStateName(id ParserStateID) string
- func (parser *TransitionParser) Parse(data []byte, state string) ([]byte, ParserStateID)
- func (parser *TransitionParser) Stop(stateName string, token string, flags ParserFlag, callback ParsedFunc)
Constants ¶
const ( // ParserFlagContinue continues parsing at the position of the match. // By default the matched token will be skipped. This flag prevents the // default behavior. In addition to that the parser will add the parsed // token to the value of the next match. ParserFlagContinue = ParserFlag(1 << iota) // ParserFlagAppend causes the parser to keep the current value for the next // match. By default a value will be restarted after each match. This flag // prevents the default behavior. ParserFlagAppend = ParserFlag(1 << iota) // ParserFlagInclude includes the matched token in the read value. // By default the value does not contain the matched token. ParserFlagInclude = ParserFlag(1 << iota) // ParserFlagPush pushes the current state on the stack before making the // transition. ParserFlagPush = ParserFlag(1 << iota) // ParserFlagPop pops the stack and uses the returned state as the next // state. If no state is on the stack the regular next state is used. ParserFlagPop = ParserFlag(1 << iota) // ParserStateStop defines a state that causes the parsing to stop when // transitioned into. ParserStateStop = ParserStateID(0xFFFFFFFF) )
Variables ¶
This section is empty.
Functions ¶
func AtoI64 ¶
AtoI64 converts a numeric string to an int64, using GetNumBase to detect the numeric base for conversion.
func AtoU64 ¶
AtoU64 converts a numeric string to an uint64, using GetNumBase to detect the numeric base for conversion.
func Btoi ¶
Btoi is a fast byte buffer to unsigned int parser that reads until the first non-number character is found. It returns the number value as well as the length of the number string encountered. If a number could not be parsed the returned length will be 0
func EscapeJSON ¶
EscapeJSON replaces occurrences of \ and " with escaped versions.
func GetNumBase ¶
GetNumBase scans the given string for its numeric base. If num starts with '0x' base 16 is assumed. Just '0' assumes base 8.
func IndexN ¶
IndexN returns the nth occurrences of sep in s or -1 if there is no nth occurrence of sep.
func IsInt ¶
IsInt returns true if the given string can be converted to an integer. The string must contain no whitespaces.
func IsJSON ¶
func IsJSON(data []byte) (bool, error, *json.RawMessage)
IsJSON returns true if the given byte slice contains valid JSON data. You can access the results by utilizing the RawMessage returned.
func JoinStringers ¶
JoinStringers works like strings.Join but takes an array of fmt.Stringer.
func LastIndexN ¶
LastIndexN returns the nth occurrence of sep in s or -1 if there is no nth occurrence of sep. Searching is going from the end of the string to the start.
func TrimToNumber ¶
TrimToNumber removes all characters from the left and right of the string that are not in the set [\-0-9] on the left or [0-9] on the right
Types ¶
type ByteRef ¶
type ByteRef []byte
ByteRef is a standard byte slice that is referencing a string. This type is considered unsafe and should only be used for fast conversion in case of read-only string access via []byte.
func NewByteRef ¶
NewByteRef creates an unsafe byte slice reference to the given string. The referenced data is not guaranteed to stay valid if no reference to the original string is held anymore. The returned ByteRef does not count as reference. Writing to the returned ByteRef will result in a segfault.
type ParsedFunc ¶
type ParsedFunc func([]byte, ParserStateID)
ParsedFunc defines a function that a token has been matched
type ParserFlag ¶
type ParserFlag int
ParserFlag is an enum type for flags used in parser transitions.
type ParserStateID ¶
type ParserStateID uint32
ParserStateID is used as an integer-based reference to a specific parser state. You can use any number (e.g. a hash) as a parser state representative.
type StringRef ¶
type StringRef string
StringRef is a standard string that is referencing the data of a byte slice. The string changes if the referenced byte slice changes. If the referenced byte slice changes size this data might get invalid and result in segfaults. As of that, this type is to be considered unsafe and should only be used in special, performance critical situations.
func NewStringRef ¶
NewStringRef creates a unsafe string reference to to the given byte slice. The referenced data is not guaranteed to stay valid if no reference to the original byte slice is held anymore. The returned StringRef does not count as reference. Changing the size of the underlying byte slice will result in a segfault.
type Transition ¶
type Transition struct {
// contains filtered or unexported fields
}
Transition defines a token based state change
func NewTransition ¶
func NewTransition(nextState ParserStateID, flags ParserFlag, callback ParsedFunc) Transition
NewTransition creates a new transition to a given state.
type TransitionDirective ¶
type TransitionDirective struct {
State string
Token string
NextState string
Flags ParserFlag
Callback ParsedFunc
}
TransitionDirective contains a transition description that can be passed to the AddDirectives functions.
func ParseTransitionDirective ¶
func ParseTransitionDirective(config string, callbacks map[string]ParsedFunc) (TransitionDirective, error)
ParseTransitionDirective creates a transition directive from a string. This string must be of the following format:
State:Token:NextState:Flag,Flag,...:Function
Spaces will be stripped from all fields but Token. If a fields requires a colon it has to be escaped with a backslash. Other escape characters supported are \n, \r and \t. Flag can be a set of the following flags (input will be converted to lowercase):
- continue -> ParserFlagContinue
- append -> ParserFlagAppend
- include -> ParserFlagInclude
- push -> ParserFlagPush
- pop -> ParserFlagPop
The name passed to the function token must be in the callbacks map. If it is not the data of the token will not be written. I.e. in empty string equals "do not write" here. An empty NextState will be translated to the "Stop" state as ususal.
type TransitionParser ¶
type TransitionParser struct {
// contains filtered or unexported fields
}
TransitionParser defines the behavior of a parser by storing transitions from one state to another.
func NewTransitionParser ¶
func NewTransitionParser() TransitionParser
NewTransitionParser creates a new transition based parser
func (*TransitionParser) Add ¶
func (parser *TransitionParser) Add(stateName string, token string, nextStateName string, flags ParserFlag, callback ParsedFunc)
Add adds a new transition to a given parser state.
func (*TransitionParser) AddDirectives ¶
func (parser *TransitionParser) AddDirectives(directives []TransitionDirective)
AddDirectives is a convenience function to add multiple transitions in as a batch.
func (*TransitionParser) AddTransition ¶
func (parser *TransitionParser) AddTransition(stateName string, newTrans Transition, token string)
AddTransition adds a transition from a given state to the map
func (*TransitionParser) GetStateID ¶
func (parser *TransitionParser) GetStateID(stateName string) ParserStateID
GetStateID creates a hash from the given state name. Empty state names will be translated to ParserStateStop.
func (*TransitionParser) GetStateName ¶
func (parser *TransitionParser) GetStateName(id ParserStateID) string
GetStateName returns the name for the given state id or an empty string if the id could not be found.
func (*TransitionParser) Parse ¶
func (parser *TransitionParser) Parse(data []byte, state string) ([]byte, ParserStateID)
Parse starts parsing at a given stateID. This function returns the remaining parts of data that did not match a transition as well as the last state the parser has been set to.
func (*TransitionParser) Stop ¶
func (parser *TransitionParser) Stop(stateName string, token string, flags ParserFlag, callback ParsedFunc)
Stop adds a stop transition to a given parser state.