From 51056714f17795db1db28f3137d0c362e288e99a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 28 Apr 2024 17:42:19 -0400 Subject: [PATCH] Add alternate list parsing function with comma separator --- key-value/key-value.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/key-value/key-value.go b/key-value/key-value.go index 2d2e4be..3a87967 100644 --- a/key-value/key-value.go +++ b/key-value/key-value.go @@ -314,6 +314,15 @@ func ParseBoolean (value string) (bool, error) { return false, ErrBooleanNotTrueOrFalse } +// ParseInteger parses a value of type integer. +// The geniuses at freedesktop never explained this type at all, or how it +// should be parsed. +func ParseInteger (value string) (int, error) { + // TODO ensure this is compliant + integer, err := strconv.ParseInt(value, 10, 64) + return int(integer), err +} + // ParseNumeric parses a value of type numeric. // Values of type numeric must be a valid floating point number as recognized by // the %f specifier for scanf in the C locale. @@ -328,6 +337,17 @@ func ParseNumeric (value string) (float64, error) { // strings must always be terminated with a semicolon. Semicolons in these // values need to be escaped using \;. func ParseMultiple[T any] (parser func (string) (T, error), value string) ([]T, error) { + return parseMultiple(parser, value, ';') +} + +// ParseMultipleComma is like ParseMultiple, but uses a comma as a separator +// instead of a semicolon. This is used to parse icon theme files because the +// freedesktop people haven't yet learned the word "consistency". +func ParseMultipleComma[T any] (parser func (string) (T, error), value string) ([]T, error) { + return parseMultiple(parser, value, ',') +} + +func parseMultiple[T any] (parser func (string) (T, error), value string, sep rune) ([]T, error) { values := []T { } builder := strings.Builder { } @@ -344,9 +364,9 @@ func ParseMultiple[T any] (parser func (string) (T, error), value string) ([]T, switch char { case '\\': backslash = true - case ';': + case sep: if backslash { - builder.WriteRune(';') + builder.WriteRune(sep) backslash = false } else { err := newValue()