From fc0cab93bcebea54be22a01516ec970d968f2c11 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 21 Oct 2024 17:36:40 -0400 Subject: [PATCH] format::desktop_entry: Add untested parsing for integer values --- format/desktop_entry/error.ha | 6 ++++++ format/desktop_entry/value.ha | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/format/desktop_entry/error.ha b/format/desktop_entry/error.ha index f3921a4..53e0f72 100644 --- a/format/desktop_entry/error.ha +++ b/format/desktop_entry/error.ha @@ -12,6 +12,7 @@ export type syntaxerr = !( invalid_escape | invalid_boolean | invalid_numeric | + invalid_integer | expected_single); // Returned when a malformed group header is encountered while parsing. @@ -35,6 +36,10 @@ export type invalid_boolean = !void; // was given. export type invalid_numeric = !void; +// Returned when a numeric value was expected while parsing, but something else +// was given. +export type invalid_integer = !void; + // Returned when a singular value was expected while parsing, but multiple // values were given. export type expected_single = !void; @@ -48,6 +53,7 @@ case invalid_ascii => yield "invalid ascii"; case invalid_escape => yield "invalid escape"; case invalid_boolean => yield "invalid boolean"; case invalid_numeric => yield "invalid numeric"; +case invalid_integer => yield "invalid integer"; case expected_single => yield "expected single"; case let err: io::error => yield io::strerror(err); case let err: utf8::invalid => yield utf8::strerror(err); diff --git a/format/desktop_entry/value.ha b/format/desktop_entry/value.ha index 64726f6..265fa7d 100644 --- a/format/desktop_entry/value.ha +++ b/format/desktop_entry/value.ha @@ -2,7 +2,7 @@ use ascii; use strconv; use strings; -// TODO icon_theme references an "integer" data type. support that here. +// TODO numeric should really be f32 // Parses a string value. It may contain all ASCII characters except for control // characters. The memory must be freed by the caller. @@ -56,6 +56,15 @@ case => yield invalid_numeric; }; +// Parses an integer value. While there is no documentation on this data type, +// it is referred to by the Icon Theme Specification, §4, tables 2-3. +export fn parse_integer(in: str) (int | error) = match(strconv::stoi(in)) { +case let integer: int => yield integer; +case => + if (strings::contains(in, ";")) return expected_single; + yield invalid_integer; +}; + // Parses multiple string values. See [[parse_string]] for more information. The // memory must be freed by the caller. // Specification: §4 @@ -125,6 +134,23 @@ export fn parse_numerics(in: str) ([]f64 | error) = { return result; }; +// Parses multiple integer values. See [[parse_integer]] for more information. +// The memory must be freed by the caller. +export fn parse_integers(in: str) ([]int | error) = { + let splitte = split(in); + let result: []int = alloc([], 0); + for (let in => splitter_next(&splitte)) { + match (parse_integer(in)) { + case let number: int => + append(result, number); + case let err: error => + free(result); + return err; + }; + }; + return result; +}; + type string_escaper = []u8; fn escape_string(in: str) string_escaper = strings::toutf8(in): string_escaper;