Skip to content

Instantly share code, notes, and snippets.

View zigster64's full-sized avatar

Scribe of the Ziggurat zigster64

  • Outback Australia, middle of nowhere
  • 05:48 (UTC +10:00)
View GitHub Profile
@zigster64
zigster64 / money.zig
Last active July 26, 2024 05:17
Zig Money object with print formatting
// Instantiate one of these structs, and use it in print statements to get formatted output
//
// Set the .value if you know the float value, or .string if you have a string value
// Set the currency to override the currency prefix
//
// Will format the number using commas, so 1234.56 -> 1,234.56 for example
//
// If Negative, will wrap the output in a <span class="negative"></span> (which turns it red)
// and places the negative sign outside the currency symbol
// Hack/Change this suit
@zigster64
zigster64 / time.zig
Created March 19, 2024 07:21
Time.zig with iso
// vendored from https://github.com/nektro/zig-time/tree/master
const std = @import("std");
const string = []const u8;
const time = @This();
pub const DateTime = struct {
ms: u16,
seconds: u16,
minutes: u16,
hours: u16,
@zigster64
zigster64 / simple-threadpool-webserver.zig
Last active May 8, 2023 14:44
simple threadpool webserver
const std = @import("std");
const Self = @This();
fn runLoop(self: *Self) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var server = std.http.Server.init(allocator, .{ .reuse_address = true, .kernel_backlog = 64 });
defer server.deinit();
var listen_address = try std.net.Address.resolveIp("0.0.0.0", 8080);
@zigster64
zigster64 / simple-threadpool-webserver.zig
Created May 8, 2023 13:53
simple threadpool webserver
const std = @import("std");
const Self = @This();
fn runLoop(self: *Self) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var server = std.http.Server.init(allocator, .{ .reuse_address = true, .kernel_backlog = 4096 });
defer server.deinit();
var listen_address = try std.net.Address.resolveIp("127.0.0.1", self.config.port);
@zigster64
zigster64 / std.http.server-keepalive-bug.zig
Last active April 24, 2023 15:35
Demonstrate proof of keep alive bug
pub fn main() !void {
var server = Server.init(std.heap.page_allocator, .{ .reuse_address = true });
defer server.deinit();
try server.listen(try net.Address.parseIp("127.0.0.1", 8080));
while (true) {
var buf: [8192]u8 = undefined;
std.log.info("Waiting for a new connection ...", .{});
const res = try server.accept(.{ .static = &buf }); // use a buffer for headers
@zigster64
zigster64 / std.http.server.sendfile.zig
Last active April 24, 2023 15:36
use std.os.sendfile with zig std.http.server
fn sendfile(res: *std.http.Server.Response, filename: []const u8) !void {
var file = std.fs.cwd().openFile(filename, .{}) catch {
// TODO - Add any extra 404 handling code here
res.status = .not_found;
try res.do();
return;
};
defer file.close();
const file_len = try file.getEndPos();
const std = @import("std");
const Stuff = enum { zeroth, first, second, third, somethingElse };
const OtherStuff = enum { zeroth, first, second, third, somethingElse };
const EnumLookupError = error{InvalidName};
// convert a string into a enum field
pub fn stringToEnum(comptime E: type, name: []const u8) EnumLookupError!E {
inline for (std.meta.fields(E)) |e| {
if (std.mem.eql(u8, name, e.name)) {