Skip to content

Instantly share code, notes, and snippets.

View dk949's full-sized avatar

David K dk949

View GitHub Profile
@dk949
dk949 / sv_to_num.hpp
Last active June 8, 2025 23:25
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>
#if __cplusplus < 202002L
#error this file has to be compiled with at least C++20
#endif
@dk949
dk949 / pack_loops.hpp
Last active June 8, 2025 23:18
for loop, break, continue and indexing for template parameter packs
#ifndef UT_PACK_LOOPS_HPP
#define UT_PACK_LOOPS_HPP
#if __cplusplus < 201'703L
# error this file has to be compiled with at least C++17
#endif
/* Usage:
* std >= c++17 (c++20)
*
@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 July 17, 2025 15:37
`defer` statement for C++ (like the ones avaliable in Go and Zig)
#ifndef UT_DEFER_HPP
#define UT_DEFER_HPP
#if __cplusplus < 201'703L
# error this file has to be compiled with at least C++17
#endif
/* Usage:
* std >= c++17
@dk949
dk949 / print.hpp
Last active June 8, 2025 23:24
C++20 sensible print macros. Uses std::format (including vector formatting). See "Usage" at the top of the file.
#ifndef UT_PRINT_HPP
#define UT_PRINT_HPP
#if __cplusplus < 202'002L
# error this file has to be compiled with at least C++20
#elif __cplusplus >= 202'302L && !defined(UT_PRINT_SUPPRESS_CXX_STD_WARNING)
# warning use std::print instead, or define UT_PRINT_SUPPRESS_CXX_STD_WARNING to suppress this warning
#endif
/* Usage:
@dk949
dk949 / binfmt.h
Last active January 10, 2024 20:28
Binary number formater for C. See commend for usage.
/*
Format integers as binary numbers. Expects little endian numbers.
License: See end of file
*/
/* Usage:
* int i = 42;
* printf("i = 0b" BIT_FMT32 "\n", BIT_ARG32(i));
* // Output: i = 0b00000000000000000000000000101010
@dk949
dk949 / pseudocode.xml
Last active January 10, 2024 20:02
KDE synatx highlighting file for pseudocode. Supports some commonly used pseudocode keywords, as well as structured english descriptions surrounded by double chevrons.
<!--
KDE synatx highlighting file for pseudocode (https://github.com/KDE/syntax-highlighting)
Based on the Zig highlighting file (https://raw.githubusercontent.com/KDE/syntax-highlighting/master/data/syntax/zig.xml).
Intended for use with pandoc, but will probably work with kate too
-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language [
<!ENTITY bin_int "(?:[01]+(?:_[01]+)*)">
<!ENTITY oct_int "(?:[0-7]+(?:_[0-7]+)*)">
@dk949
dk949 / build_zls.sh
Last active August 11, 2023 12:21
Build zls for different versions of Zig
#!/bin/bash
set -e
# VERSION_LIST=("0.7.0" "0.7.1" "0.8.0" "0.8.1" "0.9.0" "0.9.1" "0.10.0" "0.10.1" "0.11.0" "master")
VERSION_LIST=("0.9.1" "0.10.1" "0.11.0" "master")
git clone https://github.com/zigtools/zls
cd zls
@dk949
dk949 / todonotes.lua
Last active May 15, 2023 22:27
Pandoc filter to insert todonotes todos without inline latex
--[[
See bottom of scrip for license
Usage:
---
header-includes: |
\usepackage{todonotes}
...
@dk949
dk949 / numbered-links.lua
Last active January 10, 2024 20:00
Pandoc numbered section links