How to use CRC structs in `std.hash`?

I noticed that in the source code for zig that there are some predefined CRC structs in (zig/lib/std/hash/crc.zig). I wrote a small test program to try to use them, but they don’t seem to be available in the way that I was expecting.

Compiling with 0.17.0-dev.1426+58a94eaae. I don’t really care about GSM, just an example.

const std = @import("std");

pub fn main() void {
    const data = "Hello, GSM!";
    const result = std.hash.Crc3Gsm.hash(data);
    std.debug.print("CRC-3 GSM: {x}\n", .{result});
}
test.zig:5:29: error: root source file struct 'hash' has no member named 'Crc3Gsm'
    const result = std.hash.Crc3Gsm.hash(data);

Use std.hash.crc.@"CRC-3/GSM".

1 Like

The name string identifier syntax. As @rosew0od noted, you must reference it in quotations with @ prefix, e.g. @"identifier with spaces or other non-standard characters".

1 Like

Ok, trying to understand how that trick works.
I did a search for “CRC-3/GSM” in the source, and it only shows up as a test label in zig/lib/std/hash/crc/test.zig. Where in the codebase does “CRC-3/GSM” get attached to that particular struct?

NM, I think it is “zig/tools/crc/catalog.txt” … and something must process that file.

It is in defined in the source code, specifically in std.hash.crc.zig. It happens to be the first thing defined in the file.

Guessing by Andrew’s latest commit message of “restore auto-generated status”, I assume the file is indeed generated at some point, though I haven’t spent any time investigating.

1 Like