tstrings

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
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

func AtoI64(num string) (int64, error)

AtoI64 converts a numeric string to an int64, using GetNumBase to detect the numeric base for conversion.

func AtoU64

func AtoU64(num string) (uint64, error)

AtoU64 converts a numeric string to an uint64, using GetNumBase to detect the numeric base for conversion.

func Btoi

func Btoi(buffer []byte) (uint64, int)

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

func EscapeJSON(text string) string

EscapeJSON replaces occurrences of \ and " with escaped versions.

func GetNumBase

func GetNumBase(num string) (string, int)

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

func IndexN(s, sep string, n int) int

IndexN returns the nth occurrences of sep in s or -1 if there is no nth occurrence of sep.

func IsInt

func IsInt(s string) bool

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 ItoLen

func ItoLen(number uint64) int

ItoLen returns the length of an unsingned integer when converted to a string

func Itob

func Itob(number uint64, buffer []byte) error

Itob writes an unsigned integer to the start of a given byte buffer.

func Itobe

func Itobe(number uint64, buffer []byte) error

Itobe writes an unsigned integer to the end of a given byte buffer.

func JoinStringers

func JoinStringers(a []fmt.Stringer, sep string) string

JoinStringers works like strings.Join but takes an array of fmt.Stringer.

func LastIndexN

func LastIndexN(s, sep string, n int) int

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

func TrimToNumber(text string) string

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

func Unescape

func Unescape(text string) string

Unescape replaces occurrences of \\n, \\r and \\t with real escape codes.

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

func NewByteRef(a string) ByteRef

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

func NewStringRef(a []byte) StringRef

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.

Jump to

Keyboard shortcuts

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