This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const io = std.io; | |
const math = std.math; | |
// TODO: this is a massive hack that relies on implementation details | |
// and **WILL** break when BufferedReader or SeekableStream change, | |
// however, I do not see any other way, so this is fine for now. | |
/// An amalgam of `std.io.BufferedReader` and `std.io.SeekableStream`, such that they work together. | |
/// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
fn splice(src_rd: anytype, dst_wr: anytype, comptime buf_len: usize, lim: usize) !void { | |
var buf: [buf_len]u8 = undefined; | |
var left = lim; | |
while (true) { | |
const len = try src_rd.read(buf[0..@min(left, buf.len)]); | |
if (len == 0) break; | |
left = try std.math.sub(usize, left, len); | |
try dst_wr.writeAll(buf[0..len]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
extern fn not_inlined(ptr: [*]usize, len: usize) usize; | |
extern fn inlined(ptr: [*]usize, len: usize) usize; | |
extern fn builtin(ptr: [*]usize, len: usize) usize; | |
extern fn builtin_no_destructure(ptr: [*]usize, len: usize) usize; | |
extern fn sum_not_inlined(ptr: [*]usize, len: usize) usize; | |
extern fn sum_inlined(ptr: [*]usize, len: usize) usize; | |
extern fn sum_builtin(ptr: [*]usize, len: usize) usize; | |
extern fn sum_builtin_no_destructure(ptr: [*]usize, len: usize) usize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: 0BSD | |
// Copyright (C) 2024 by cancername <[email protected]> | |
// | |
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without | |
// fee is hereby granted. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS | |
// SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE | |
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub const Event = struct { | |
event: ?[]const u8, | |
data: ?std.ArrayListUnmanaged(u8), | |
id: ?[]const u8, | |
retry: ?u64, | |
pub fn deinit(event: Event, ally: std.mem.Allocator) void { | |
inline for (.{ event.event, event.id }) |mx| if (mx) |x| ally.free(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn main() !void { | |
var rd_state = std.io.bufferedReader(std.io.getStdIn().reader()); | |
const rd = rd_state.reader(); | |
var wr_state = std.io.bufferedWriter(std.io.getStdOut().writer()); | |
const wr = wr_state.writer(); | |
var buf: [64]u8 = undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub const Rc4 = struct { | |
s: [256]u8 = undefined, | |
i: u8 = 0, | |
j: u8 = 0, | |
pub fn init(key: []const u8) Rc4 { | |
var self = Rc4{}; | |
for(&self.s, 0..) |*c, i| c.* = @intCast(i); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn sizeReport(comptime T: type, comptime indent: []const u8) []const u8 { | |
comptime { | |
var result: []const u8 = ""; | |
result = result ++ std.fmt.comptimePrint("{s}size of {s} is {d} bytes ({d} bits)\n", .{indent, @typeName(T), @sizeOf(T), @bitSizeOf(T)}); | |
switch (@typeInfo(T)) { | |
.Struct => |s| { | |
result = result ++ std.fmt.comptimePrint("{s}field report ({s}):\n", .{indent, @tagName(s.layout)}); | |
for (s.fields) |field| { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const delimiter = '\n'; | |
const default_bits = 64; | |
const Wordlist = struct { | |
bytes: std.ArrayListUnmanaged(u8), | |
word_indices: std.ArrayListUnmanaged(usize), | |
allocator: std.mem.Allocator, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct random_bit_generator { | |
uint32_t word; | |
uint8_t bits_remaining; | |
}; | |
static bool next_bit(struct random_bit_generator *rbg) { | |
if (rbg->bits_remaining == 0) { | |
rbg->word = arc4random(); | |
rbg->bits_remaining = 32; | |
} |
OlderNewer