Skip to content

Instantly share code, notes, and snippets.

@tylov
tylov / cfmt.h
Created November 26, 2020 12:08
C implementation of {fmt} / std::format -like print function
/* MIT License
*
* Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
@tylov
tylov / coption.h
Last active March 26, 2021 08:07
getopt()-like method for command line argument parsing.
/* MIT License
*
* Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
@tylov
tylov / sso_string.h
Last active July 1, 2021 11:39
SSO String in C99 using all (typical 23) bytes available for the short string optimization (24th is the null terminator)
/* MIT License
*
* Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
@tylov
tylov / sso_test.c
Last active July 1, 2021 11:44
Small test for sso_string.h
#include "sso_string.h"
#include <stdio.h>
void print(string s) {
printf("short: %d, %s: %u %u\n", !string_is_long(&s), string_str(&s), string_size(s), string_capacity(s));
}
int main()
{
string s1 = string_lit("Hello!");
@tylov
tylov / cmacro_overload.h
Last active July 2, 2021 16:46
Macro overloading support in C.
/* MIT License
*
* Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
@tylov
tylov / fmt_print.h
Last active September 11, 2023 17:00
Lightweight fmt_print() function for C11
#ifndef FMT_H_INCLUDED
#define FMT_H_INCLUDED
/*
VER 2.2: NEW API:
void fmt_print(fmt, ...);
void fmt_println(fmt, ...);
void fmt_printd(dest, fmt, ...);
const char* fmt_tm(fmt, struct tm* tp);
void fmt_close(fmt_stream* ss);
@tylov
tylov / cspan.h
Last active June 8, 2024 13:10
C implementation of Python numpy multidimensional arrays
/*
MIT License
*
* Copyright (c) 2023 Tyge Løvset
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is