This commit is contained in:
jonathan santis 2024-10-14 09:39:31 +02:00
parent 01d5110f36
commit 078ada0f11

View File

@ -12,12 +12,12 @@ pub fn main() !void {
_ = buffer;
print("Buffer:{s}\n", .{buf});
const slice = getNum("123dsfiauf_123_45t63sadf9875,231,1");
const slice = getNum("123dsfi843w653487 38745 631875 61385631856__87234982374924__auf_123_45t63sadf9875,231,1");
//_ = slice;
print("number1: {d}", .{slice[0]});
//for (slice) |num| {
// print("extracted number:\n{d}\n", .{num});
//}
//print("number1: {d}", .{slice[0]});
for (slice) |num| {
print("extracted number:\n{d}\n", .{num});
}
} else {
print("Error getenv\n", .{});
}
@ -27,15 +27,19 @@ fn getNum(str1: []const u8) []u64 {
//_ = str1;
var pos: u64 = 0;
var num = std.mem.zeroes([64]u64);
var num_res = std.mem.zeroes([64]u64);
var num_res = std.mem.zeroes([16]u64);
var it: u64 = 0;
for (str1, 0..) |elem, it2| {
// print("Elem: {} ", .{elem});
if (elem >= 48 and elem <= 57) { //is ascii number
if (elem >= 48 and elem <= 57) { //is ascii number?
num[pos] = @intCast(elem - 48);
pos += 1;
if (pos <= @bitSizeOf(@TypeOf(pos))) {
pos += 1;
} else {
print("ERROR int u64 oveflow! pos {d} ,max {d}\n", .{ pos, @bitSizeOf(@TypeOf(pos)) });
}
}
if ((elem < 48 or elem > 57) or it2 == (str1.len - 1)) { //add case when if last element create te int
//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) {
@ -44,7 +48,7 @@ fn getNum(str1: []const u8) []u64 {
num_res[it] = num_res[it] + num[i];
}
}
print("numres:{}={}\n", .{ it, num_res[it] });
//print("numres:{}={}\n", .{ it, num_res[it] });
pos = 0;
it += 1;
}