zig-learn/numParse.zig

34 lines
1.1 KiB
Zig
Raw Permalink Normal View History

2024-10-30 09:38:15 +00:00
fn getNum(str1: []const u8) []u64 {
var pos: u64 = 0;
var num = std.mem.zeroes([64]u64);
var num_res = std.mem.zeroes([16]u64);
var it: u64 = 0;
for (str1, 0..) |elem, it2| {
if (elem >= 48 and elem <= 57) { //is ascii number?
num[pos] = @intCast(elem - 48);
if (pos <= @bitSizeOf(@TypeOf(pos))) {
pos += 1;
} else {
print("ERROR int u64 oveflow! pos {d} ,max {d}\n", .{ pos, @bitSizeOf(@TypeOf(pos)) });
}
}
//Calculate int out of characters
if ((elem < 48 or elem > 57) or it2 == (str1.len - 1)) {
if (pos != 0) {
for (0..pos) |i| {
if (i < pos - 1) {
num_res[it] = num_res[it] + num[i] * (std.math.pow(u64, 10, (pos - i - 1)));
} else {
num_res[it] = num_res[it] + num[i];
}
}
//print("numres:{}={}\n", .{ it, num_res[it] });
pos = 0;
it += 1;
}
}
}
return &num_res;
}