Skip to content

Instantly share code, notes, and snippets.

@dbremner
dbremner / char_predicates.hpp
Created July 22, 2023 11:58
These avoid issues with out of range values for ctype.h functions
#ifndef CHAR_PREDICATES_HPP
#define CHAR_PREDICATES_HPP
[[nodiscard]] static inline bool
is_digit(int ch)
{
return ((ch >= '0') && (ch <= '9'));
}
[[nodiscard]] static inline bool
@dbremner
dbremner / cast_wrappers.hpp
Created January 9, 2022 03:21
cast wrappers for clang nullability types
/// Cast a nullable pointer to a non-null pointer
/// This is unsafe and should be justified with a comment
/// containing the keyword WHY
/// For example, "WHY - checked"
template <typename T>
[[nodiscard]] constexpr static inline auto
as_live(T *_Nullable value) -> T *_Nonnull
{
return static_cast<T *_Nonnull>(value);
}
@dbremner
dbremner / nullability_typedefs.h
Last active July 13, 2023 23:03
Macro for generating nullability typedefs
#ifndef NULLABILITY_TYPEDEFS_H
#define NULLABILITY_TYPEDEFS_H
#define MAKE_NULLABILITY_TYPEDEFS(type, name) \
typedef const type *const _Nonnull name##_pure_ptr; \
typedef const type *_Nonnull name##_const_ptr; \
typedef type *_Nonnull name##_ptr; \
typedef type *_Nullable name##_null_ptr; \
typedef const type *const _Nullable name##_pure_null_ptr; \
typedef type *_Nonnull *_Nonnull name##_ptr_ptr; \
@dbremner
dbremner / ctime_to_strftime.c
Last active October 28, 2021 13:08
Replacing ctime() with strftime()
// ctime is an obsolete time function that is present in many older Unix programs.
// It's common for programs to call it and then modify the output.
// Here's a strftime format string that should produce the same output.
// dest_len should be >= 26
static void get_time_str(time_t val, char *dest, size_t dest_len)
{
struct tm result;
localtime_r(&val, &result);
// e.g. Wed Oct 27 18:28:32 2021\n
strftime(dest, dest_len, "%a %b %e %H:%M:%S %Y\n", &result);
@dbremner
dbremner / Obfuscated_Fortran.f77
Created July 18, 2021 21:46
Obfuscated Fortran 77 from Adam Rosenberg's A Description of One Programmer’s Programming Style Revisited
integer go to, do 100 if, return
character*12 read
logical go to if
real logical(30)
data read / 12h(1x,i5,f8.1) /
data go to, do 100 if, if 100 do / 2, 1, 30 /
assign 100 to if go to
do 100 if = do 100 if,if 100 do
return = if
logical(if) = return
@dbremner
dbremner / gist:1eebe57e6abcd626396b82b2657d346c
Created March 7, 2020 19:03
File extensions by count descending
find . -type f -iname "*.*" -print | rev | cut -d . -f 1 | rev | sort | uniq -c | sort -nr | less
@dbremner
dbremner / ListModels.Java
Last active July 4, 2019 17:01
Iterable from Swing ListModel
import org.jetbrains.annotations.NotNull;
import javax.swing.ListModel;
import java.util.AbstractList;
public enum ListModels
{
;
public static @NotNull <E> Iterable<E> asIterable(final @NotNull ListModel<E> model)
@dbremner
dbremner / crontab_changes.txt
Last active June 24, 2019 21:07
disable SACK on Ubuntu 16.04 LTS
#Ubuntu 16.04LTS does not apply network settings after boot.
#See https://bugs.launchpad.net/ubuntu/+source/procps/+bug/50093
#Work around this issue with a cron job to disable SACK
@reboot /bin/sleep 5 && /sbin/sysctl -w net.ipv4.tcp_sack=0
@dbremner
dbremner / gist:e7c9e6891b1c230f08d39b38fdf9fb9a
Created March 25, 2019 01:22
Deleting Old Time Machine backups
Problem:
Time Machine backups accumulate until they fill the drive.
Solution:
1. Run "tmutil listbackups" from a shell and pipe the output to a file.
2. Use TextEdit pattern-based search and replace to quote each line of the file.
3. Select a subset of the file with a buffer of several years.
4. Open a terminal and run caffeinate to keep the computer from sleeping.
5. Open an interactive root shell with "sudo -i"
6. Use the following command to delete old backups.
#include<limits.h>
#include<stdlib.h>
#include<stdio.h>
int f1(int num)
{
const int result = abs(num) - num;
return result;
}