Add alternate list parsing function with comma separator

This commit is contained in:
Sasha Koshka 2024-04-28 17:42:19 -04:00
parent bdbb90fd32
commit 51056714f1
1 changed files with 22 additions and 2 deletions

View File

@ -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()