`std.ascii.isDigit()` - Error message makes no sense

Hello,
Excuse me if I come of as angry, but I’m genuinely annoyed at the compiler.
Here’s the error message:

All 1 tests passed.
/archive/ARCHIVE/sources/zeb/src/template.zig:74:26: error: expected type 'type', found 'bool'
            if (!(isDigit(char) || char == '.')) return false;
                  ~~~~~~~^~~~~~
referenced by:
    parse: /archive/ARCHIVE/sources/zeb/src/template.zig:95:28

error: test...
error: The following command exited with error code 1:
/archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/bin/zig test -freference-trace=256 /archive/ARCHIVE/sources/zeb/src/server.zig -lc --cache-dir /archive/ARCHIVE/sources/zeb/zig-cache --global-cache-dir /home/kamil/.cache/zig --name test --pkg-begin network /archive/ARCHIVE/sources/zeb/zig-network/network.zig --pkg-end --enable-cache
error: the following build command failed with exit code 1:
/archive/ARCHIVE/sources/zeb/zig-cache/o/39542eb356c7023c26f8f9fb9a9fad05/build /archive/ARCHIVE/zig-bootstrap-0.10.1/zig-bootstrap-0.10.1/out/host/bin/zig /archive/ARCHIVE/sources/zeb /archive/ARCHIVE/sources/zeb/zig-cache /home/kamil/.cache/zig test -freference-trace -freference-trace

this makes no sense to me. isDigit is a function from std.ascii.

here’s the full snippet, if any one wants to look at it:

    fn isFloat(text: []const u8) bool {
        for (text) |char| {
            if (!(isDigit(char) || char == '.')) return false;
        }
        return true;
    }


for anyone wondering, why I’m implementing my own isFloat: I couldn’t find a such function in the standard library. std.fmt.parseFloat returns either the result or an error, which is not what I want - I want to do a simple check that yields a boolean.

Thanks for any help

    fn isFloat(text: []const u8) bool {
        for (text) |char| {
            if (!(isDigit(char) or char == '.')) return false;
        }
        return true;
    }

That should work - you are using || instead of the keyword or.

Careful too - that function will return true for a string like “1…2…3…4” - you may want to count the ‘.’ characters.

Also, I take it you don’t want to parse scientific notation like “1e-8” either - just a thought.

2 Likes

Could also do:

fn isFloat(text: []const u8) bool {
    _ = std.fmt.parseFloat(f64, text) catch return false;
    return true;
}
4 Likes