From ca90ebdfa8789654766c5d7969baa7afacd9ebd2 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Mon, 16 Feb 2026 16:31:54 -0500 Subject: initial commit --- common/infostring/infostring.go | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 common/infostring/infostring.go (limited to 'common/infostring/infostring.go') diff --git a/common/infostring/infostring.go b/common/infostring/infostring.go new file mode 100644 index 0000000..114058a --- /dev/null +++ b/common/infostring/infostring.go @@ -0,0 +1,72 @@ +package infostring + +import ( + "strings" + + "github.com/osm/quake/common/buffer" +) + +type InfoString struct { + Info []Info +} + +type Info struct { + Key string + Value string +} + +func New(opts ...Option) *InfoString { + var infoString InfoString + + for _, opt := range opts { + opt(&infoString) + } + + return &infoString +} + +func (is *InfoString) Bytes() []byte { + buf := buffer.New() + + buf.PutByte(byte('"')) + + for i := 0; i < len(is.Info); i++ { + buf.PutBytes([]byte("\\" + is.Info[i].Key)) + buf.PutBytes([]byte("\\" + is.Info[i].Value)) + } + + buf.PutByte(byte('"')) + + return buf.Bytes() +} + +func Parse(input string) *InfoString { + var ret InfoString + + trimmed := strings.Trim(input, "\"") + parts := strings.Split(trimmed, "\\") + + for i := 1; i < len(parts)-1; i += 2 { + ret.Info = append(ret.Info, Info{parts[i], parts[i+1]}) + } + + return &ret +} + +func (is *InfoString) Get(key string) string { + for i := 0; i < len(is.Info); i++ { + if is.Info[i].Key == key { + return is.Info[i].Value + } + } + + return "" +} + +func (is *InfoString) Set(key, value string) { + for i := 0; i < len(is.Info); i++ { + if is.Info[i].Key == key { + is.Info[i].Value = value + } + } +} -- cgit v1.2.3-59-g8ed1b