Skip to content

Instantly share code, notes, and snippets.

@karlseguin
karlseguin / bench.zig
Created November 13, 2023 01:27
streamUntilDelimiter Buffered vs Not
const std = @import("std");
const LOOPS = 1000;
pub fn main() !void {
var in: [10000]u8 = undefined;
try std.os.getrandom(&in);
var fbs = std.io.fixedBufferStream(&in);
var out: [10000]u8 = undefined;
@karlseguin
karlseguin / test_runner.zig
Last active October 1, 2024 01:45
Custom Zig Test Runner, better ouput, timing display, and support for special "tests:beforeAll" and "tests:afterAll" tests
// in your build.zig, you can specify a custom test runner:
// const tests = b.addTest(.{
// .target = target,
// .optimize = optimize,
// .test_runner = "test_runner.zig", // add this line
// .root_source_file = b.path("src/main.zig"),
// });
const std = @import("std");
const builtin = @import("builtin");
@karlseguin
karlseguin / bench.zig
Last active October 8, 2023 15:04
zig std.indexOf benchmark
const std = @import("std");
const ITERATIONS = 1_000_000;
pub fn main() !void {
benchmark("needle.len == 1, haystack.len == 11 ", "hello world", "w", 6);
benchmark("needle.len == 1, haystack.len == 3091", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet erat elit. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris cursus quis eros quis vestibulum. Vestibulum in convallis nulla. Nulla mauris velit, tempus id varius sit amet, sodales et magna. Aenean vel augue vitae erat luctus maximus. Nam dignissim diam felis, id fermentum neque dapibus quis. Quisque volutpat ac nibh a blandit. Aenean id felis aliquam, aliquet lorem vel, lobortis ligula. Morbi lacinia risus ac dictum accumsan. Maecenas dapibus elit in magna porta tempus. Proin luctus neque sed erat sagittis gravida. Praesent at lacinia libero.Curabitur sagittis risus ut suscipit cursus. Maecenas at gravida turpis, id malesuada felis. Praesent velit dui, pulvinar eget vehicula sed, semper
@karlseguin
karlseguin / utf8_validate.zig
Last active September 30, 2023 06:00
Zig's utf8ValidateSlice vs Go's implementation
const std = @import("std");
const mem = std.mem;
const ITERATIONS = 20_000;
pub fn main() !void {
try benchmark("empty", "", true);
try benchmark("short.ascii", "it's over 9000!!", true);
try benchmark("long.ascii", "Lorem ipsum dolor sit amet, pri legere suscipit repudiare at, cu vix aliquip pertinacia quaerendum, in eam dicat cetero eripuit. Duo odio nonumy viderer at, et sea vidit dolor animal, has persius conceptam id. Vide idque copiosae ut qui, ut lorem atomorum mei, in eos suscipit eleifend. Illud principes no eos, pri dolorem singulis ne, ex pri malorum argumentum scribentur. Et altera regione per. Disputando contentiones no mei, et vel discere meliore constituam. Te quo legere nostrum deleniti, ad eros putent tritani per.Repudiandae mediocritatem ut est, ex sea dicit evertitur. Per tritani detraxit an. Pro no fastidii tractatos, per purto affert accusata no, affert consequat mel ea. Cum ex postulant gubergren, has quot vidit argumentum no.Eu est esse feugait, mea no augue debet constituam. Sea s
@karlseguin
karlseguin / db.ex
Created February 5, 2022 03:39
Elixir without Ecto, A thin Postgrex wrapper
# see https://www.openmymind.net/Elixir-Without-Ecto/
defmodule A.DB do
defmacro __using__(_) do
quote location: :keep do
@name __MODULE__
def child_spec(opts) do
%{
id: __MODULE__,
@karlseguin
karlseguin / basic-tcp-chat.zig
Last active April 29, 2024 14:15 — forked from andrewrk/basic-tcp-chat.zig
Basic TCP Chat Server for Zig 0.12
// For Zig 0.12
const std = @import("std");
const net = std.net;
const ArenaAllocator = std.heap.ArenaAllocator;
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
@karlseguin
karlseguin / waf.conf
Created October 22, 2021 13:02
basic nginx waf
# WAFs are a waste of time. False positives negatively impact actual users.
# And for what? Protection against vulnerabilities that, if you still have
# in this day in age, you're in more trouble than you think.
# Still, to keep my logs clean, I like to run something like the follwoing:
location ~ \.php$ { access_log off; return 444; }
location ~ \.asp$ { access_log off; return 444; }
location ~ \.aspx$ { access_log off; return 444; }
location ~ /Admincenter/ { access_log off; return 444; }
location /console/ { access_log off; return 444; }
@karlseguin
karlseguin / util.go
Created August 13, 2020 02:05
Dummy modules in go
var Util util
type util struct{}
func (_ util) PadLeft(s string, n int) string {
//...
}
// ...
@karlseguin
karlseguin / no_return.md
Created February 25, 2020 03:36
elixir needs a return statement

This works most of the time

def handle(data) do
	with {:ok, product} <- Jason.decode!(data),
	     {:ok, category} <- Category.load(data.category_id)
	do
		# todo
	end
end
@karlseguin
karlseguin / logs.csv
Created October 18, 2019 09:53
1 months worth of probes to an open 80 and 443
timestamp method uri ip
Oct 18, 2019 @ 09:41:02.000 - - 61.219.11.153
Oct 18, 2019 @ 09:34:37.000 GET / 164.52.24.162
Oct 18, 2019 @ 09:33:12.000 - - 61.219.11.153
Oct 18, 2019 @ 09:32:28.000 - - 61.219.11.153
Oct 18, 2019 @ 09:26:41.000 GET /otsmobile/app/mgs/mgw.htm 110.105.80.110
Oct 18, 2019 @ 09:23:42.000 GET /ftpsync.settings 185.234.216.20
Oct 18, 2019 @ 09:23:41.000 GET /deployment-config.json 185.234.216.20
Oct 18, 2019 @ 09:23:40.000 GET /.vscode/ftp-sync.json 185.234.216.20
Oct 18, 2019 @ 09:23:40.000 GET /.vscode/sftp.json 185.234.216.20