format::desktop_entry: Numerics are f32 now

The specification defines numerics to be anything accepted by the
%f specifier for scanf, which implies a 32 bit float.
This commit is contained in:
Sasha Koshka 2024-10-21 17:44:43 -04:00
parent fc533dbeea
commit 0f5dd78aea
2 changed files with 13 additions and 15 deletions

View File

@ -2,8 +2,6 @@ use ascii;
use strconv; use strconv;
use strings; use strings;
// TODO numeric should really be f32
// Parses a string value. It may contain all ASCII characters except for control // Parses a string value. It may contain all ASCII characters except for control
// characters. The memory must be freed by the caller. // characters. The memory must be freed by the caller.
// Specification: §4 // Specification: §4
@ -49,8 +47,8 @@ export fn parse_boolean(in: str) (bool | error) = {
// Parses a numeric value. It must be a valid floating point number as // Parses a numeric value. It must be a valid floating point number as
// recognized by the %f specifier for scanf in the C locale. // recognized by the %f specifier for scanf in the C locale.
// Specification: §4 // Specification: §4
export fn parse_numeric(in: str) (f64 | error) = match (strconv::stof64(in)) { export fn parse_numeric(in: str) (f32 | error) = match (strconv::stof32(in)) {
case let float: f64 => yield float; case let float: f32 => yield float;
case => case =>
if (strings::contains(in, ";")) return expected_single; if (strings::contains(in, ";")) return expected_single;
yield invalid_numeric; yield invalid_numeric;
@ -119,12 +117,12 @@ export fn parse_booleans(in: str) ([]bool | error) = {
// Parses multiple numeric values. See [[parse_numeric]] for more information. // Parses multiple numeric values. See [[parse_numeric]] for more information.
// The memory must be freed by the caller. // The memory must be freed by the caller.
// Specification: §4 // Specification: §4
export fn parse_numerics(in: str) ([]f64 | error) = { export fn parse_numerics(in: str) ([]f32 | error) = {
let splitte = split(in); let splitte = split(in);
let result: []f64 = alloc([], 0); let result: []f32 = alloc([], 0);
for (let in => splitter_next(&splitte)) { for (let in => splitter_next(&splitte)) {
match (parse_numeric(in)) { match (parse_numeric(in)) {
case let number: f64 => case let number: f32 =>
append(result, number); append(result, number);
case let err: error => case let err: error =>
free(result); free(result);

View File

@ -39,10 +39,10 @@ use strings;
}; };
@test fn parse_numeric() void = { @test fn parse_numeric() void = {
assert(parse_numeric("9")! == 9.0); assert(parse_numeric("9")! == 9.0f32);
assert(parse_numeric("9.0")! == 9.0); assert(parse_numeric("9.0")! == 9.0f32);
assert(parse_numeric("34.93")! == 34.93); assert(parse_numeric("34.93")! == 34.93f32);
assert(parse_numeric("-100.895")! == -100.895); assert(parse_numeric("-100.895")! == -100.895f32);
assert(math::isnan(parse_numeric("NaN")!)); assert(math::isnan(parse_numeric("NaN")!));
assert(parse_numeric("Infinity")! == math::INF); assert(parse_numeric("Infinity")! == math::INF);
assert(parse_numeric("hello") is invalid_numeric); assert(parse_numeric("hello") is invalid_numeric);
@ -122,10 +122,10 @@ use strings;
@test fn parse_numerics() void = { @test fn parse_numerics() void = {
let correct: []f64 = [ let correct: []f64 = [
5.0, 5.0f32,
34.9, 34.9f32,
29.0, 29.0f32,
32498.23784, 32498.23784f32,
]; ];
let got = parse_numerics("5;34.9;29;32498.23784;")!; let got = parse_numerics("5;34.9;29;32498.23784;")!;
defer free(got); defer free(got);