Hi, This is my second post here. I am a noob.
So,
The standard lib has the following code in std.ascii.isDigit() as of the version 12/13
pub fn isDigit(c: u8) bool {
return switch (c) {
'0'...'9' => true,
else => false,
};
}
I love it. Simple, to the point. Easy to understand. Clearly it must go. So I rewrote it using bitwise. This is supposed to be faster.
// Written by endlessly_amused from ziggit.dev 20240606
pub fn isDigit_new(c: u8) bool {
switch (c>>3) {
6 => return true, // returns on 0,1,2,3,4,5,6,7
7 => if (c&6==0) {return true;} else {return false;}, // 8,9
else => return false,
} // end of switch
} // end of fn
Functionally it does the same thing. But it only has 2 prongs as apposed to the previous 10. The first prong takes out 80% of the expected results.
It works because all digits start 00110xxx, or 0011100x
I am guessing there is some sort of release documentation to put it in as a candidate. If so, where can I get a copy of it? I am looking for the following guidance.
Q1) Is the code actually any better? And if so, What’s more important speed or readability?
Q2) is it actually any faster? Does it give better execution code? Seems to run slightly faster. With my test. But is there some sort of formal test? Or review process?
Q3) Do we care? It saves at best a few clock cycles.
Q4) I have plastered my name in the code. Is this the done thing? Or is this a no no.
So tell me what you think. How does this process work? Is this code any good? Or am I having delusions of adequacy?
I suppose I am just trying to help out. Is there a jobs board anywhere? ie a list of outstanding jobs?
regards
P.s.
I wrote some test for it
test "isDigit_new"
{
// sanity check
try std.testing.expect(isDigit_new('/') == false); // character just above and below the numbers
try std.testing.expect(isDigit_new('0') == true);
try std.testing.expect(isDigit_new('1') == true);
try std.testing.expect(isDigit_new('2') == true);
try std.testing.expect(isDigit_new('3') == true);
try std.testing.expect(isDigit_new('4') == true);
try std.testing.expect(isDigit_new('5') == true);
try std.testing.expect(isDigit_new('6') == true);
try std.testing.expect(isDigit_new('7') == true);
try std.testing.expect(isDigit_new('8') == true);
try std.testing.expect(isDigit_new('9') == true);
try std.testing.expect(isDigit_new(':') == false); // character just above and below the numbers
// run through all possible values of u8 and check that they are the same for new code
for (0..255) |i| { try std.testing.expect(isDigit(@intCast(i))==isDigit_new(@intCast(i))); }
} // end of test