This commit is contained in:
jonathan santis 2024-10-14 08:52:08 +02:00
commit 01d5110f36

72
main.zig Normal file
View File

@ -0,0 +1,72 @@
const std = @import("std");
const expect = std.testing.expect;
const print = std.debug.print;
const getenv = std.posix.getenv;
pub fn main() !void {
//const buffer: [:0]u8 = undefined;
std.debug.print("Hello, {s}\n", .{"World"});
try createFile();
if (getenv("TERM")) |buf| {
const buffer = buf;
_ = buffer;
print("Buffer:{s}\n", .{buf});
const slice = getNum("123dsfiauf_123_45t63sadf9875,231,1");
//_ = slice;
print("number1: {d}", .{slice[0]});
//for (slice) |num| {
// print("extracted number:\n{d}\n", .{num});
//}
} else {
print("Error getenv\n", .{});
}
}
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 it: u64 = 0;
for (str1, 0..) |elem, it2| {
// print("Elem: {} ", .{elem});
if (elem >= 48 and elem <= 57) { //is ascii number
num[pos] = @intCast(elem - 48);
pos += 1;
}
if ((elem < 48 or elem > 57) or it2 == (str1.len - 1)) { //add case when if last element create te int
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;
}
fn createFile() !void {
const file = try std.fs.cwd().openFile("junk-file.txt", .{});
defer file.close();
// const bytes_written = try file.writeAll("hello world!");
// //_ = bytes_written;
// std.debug.print("files written:{any}, {any}", .{ bytes_written, @TypeOf(bytes_written) });
var buffer: [100]u8 = undefined;
try file.seekTo(0);
const bytes_read = try file.readAll(&buffer);
print("buffer: {s} check: {s}\n", .{ buffer[0..bytes_read], "hello world!" });
if (std.mem.eql(u8, buffer[0 .. bytes_read - 1], "hello world!") == true) {
print("equal\n", .{});
} else {
print("not equal\n", .{});
}
}