Skip to content

Instantly share code, notes, and snippets.

View dk949's full-sized avatar

David K dk949

View GitHub Profile
@dk949
dk949 / typecheck.py
Created September 13, 2024 20:23
Python decorator to do runtime typechecking based on function type annotations. See docstring for detaills
def typecheck(
func=None,
*,
check_return=False,
):
"""
Perform a runtime type check of the function arguments and (optionally) its
return type.
All annotated arguments and keyword arguments will be typechecked. Known
@dk949
dk949 / assert.hpp
Last active February 25, 2025 10:50
Various assertion macros and functions
#ifndef UT_ASSERT_
#define UT_ASSERT_
/** Usage:
* std >= C++20 (C++23)
*
* UT_ASSUME(cond)
* Low level primitive. Invokes undefined behaviour if `cond` is false.
* Uses [[assume]] if available. Compiler extensions otherwise.
*
* ut::unreachable()
@dk949
dk949 / trim.hpp
Last active February 25, 2025 10:50
Trim string_view. Using whitespace by default
#ifndef UT_TRIM
#define UT_TRIM
/** Usage:
* std >= C++20
* Simple string_stream trimming (trims whitespace by default)
*
* #include <iostream>
*
* int main() {
@dk949
dk949 / get_notes.awk
Created May 23, 2024 18:17
Extract speaker notes from a pandoc markdown document
# Extract speaker notes (with related headings) from a pandoc markdown document
# Produces valid markdown which can be compiled with pandoc
# Headings are also separated by hrules
# Example usage:
# awk -f get_notes.awk your_source.md | pandoc -f markdown -o your_notes.pdf
## Find the closing div for the notes
@dk949
dk949 / pair.hpp
Last active February 25, 2025 10:50
std::pair alternative with extra features and no constructors
#ifndef UT_PAIR_HPP
#define UT_PAIR_HPP
// A std::pair replacement with no implicit (or any!) constructors.
// All conversions are done through either member functions or static functions
// Also you can use it in a range-based-for loop if the types of `first` and `second` are the same
/**Usage:
*
* int main() {
@dk949
dk949 / realloc_unique_ptr.hpp
Last active February 25, 2025 10:50
realloc for unique_ptr
#ifndef UT_REALLOC_UNIQUE_PTR_HPP
#define UT_REALLOC_UNIQUE_PTR_HPP
#include <memory>
#include <utility>
/**Usage:
* std >= c++17
*
* #include "realloc_unique_ptr.hpp"
* #include <cstdio>
@dk949
dk949 / sv_to_num.hpp
Last active February 25, 2025 10:50
Convenient string_view to number conversions
#ifndef UT_SV_TO_NUM_HPP
#define UT_SV_TO_NUM_HPP
#include <charconv>
#include <concepts>
#include <optional>
#include <string_view>
/** Usage:
* int main() {
* for (std::string_view sv : {"123", "12.3", "12e3", "abc"}) {
@dk949
dk949 / pack_loops.hpp
Last active February 25, 2025 10:50
for loop, break, continue and indexing for template parameter packs
#ifndef UT_PACK_LOOPS_HPP
#define UT_PACK_LOOPS_HPP
/* Usage:
* std >= c++17
*
* XXX: **DO NOT RETURN FROM UT_PACK_FOR** it uses a lambda so you would only return from that lambda.
* If you do want to break the loop, use UT_PACK_BREAK instead of returning.
*
* template<std::size_t... sizes>
@dk949
dk949 / get_conf_key.awk
Created September 29, 2023 18:53
Parse config file with awk. Get all lines listed under a key.
# When given a conf file, will look for key "[key]" and print the non-blank,
# lines until the next key or until the end of the file.
#
# All comments are removed as well as leading and trailing spaces
#
# Expects an input variable 'key' (can be passed using `awk -v key="your key here"`)
#
#
# Example:
#
@dk949
dk949 / defer.hpp
Last active February 25, 2025 10:50
`defer` statement for C++ (like the ones avaliable in Go and Zig)
#ifndef UT_DEFER_HPP
#define UT_DEFER_HPP
/* Usage:
* std >= c++17
*
* Provides the `UT_DEFER` macro, which acts similarly to `defer` in Zig and `scope` in D.
*
* If `UT_DEFER_NO_PREFIX` is defined, `defer` can be used instead of `UT_DEFER`.
*