79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
|
package streams
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func quote(text string) string {
|
||
|
text = strings.Replace(text, "\\", "\\\\", -1)
|
||
|
text = strings.Replace(text, "'", "\\'", -1)
|
||
|
text = "'" + text + "'"
|
||
|
return text
|
||
|
}
|
||
|
|
||
|
func unquote(text string) string {
|
||
|
if strings.HasPrefix(text, "'") && strings.HasSuffix(text, "'") {
|
||
|
text = text[1 : len(text)-2]
|
||
|
text = strings.Replace(text, "\\'", "'", -1)
|
||
|
text = strings.Replace(text, "\\\\", "\\", -1)
|
||
|
}
|
||
|
return text
|
||
|
}
|
||
|
|
||
|
type Metadata map[string]string
|
||
|
|
||
|
func DecodeMetadataFromBytes(b []byte) {
|
||
|
// TODO
|
||
|
}
|
||
|
|
||
|
func decodeMetadataItem(text string, metadata *map[string]string) (err error) {
|
||
|
parts := strings.SplitN(text, "=", 2)
|
||
|
if len(parts) < 2 {
|
||
|
err = errors.New("expected key=value but only got key")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
parts[1] = unquote(parts[1])
|
||
|
(*metadata)[parts[0]] = parts[1]
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func DecodeMetadata(source string) (meta Metadata, err error) {
|
||
|
// name='value'; name='value';name='value';
|
||
|
meta = make(Metadata)
|
||
|
// TODO
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (meta Metadata) String() string {
|
||
|
return string(meta.Bytes())
|
||
|
}
|
||
|
|
||
|
func (meta Metadata) Bytes() (buf []byte) {
|
||
|
mstr := ""
|
||
|
|
||
|
if meta != nil {
|
||
|
for key, value := range meta {
|
||
|
mstr += fmt.Sprintf("%s=%s;", key, quote(value))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(mstr) > 16*256-1 {
|
||
|
mstr = mstr[0 : 16*256]
|
||
|
}
|
||
|
|
||
|
lengthDiv := int(math.Ceil(float64(len(mstr)) / 16))
|
||
|
lengthByte := byte(lengthDiv)
|
||
|
|
||
|
buf = make([]byte, lengthDiv*16+1)
|
||
|
buf[0] = lengthByte
|
||
|
copy(buf[1:], []byte(mstr))
|
||
|
|
||
|
return
|
||
|
}
|