Skip to content

Instantly share code, notes, and snippets.

View maddouri's full-sized avatar

Yassine MADDOURI maddouri

View GitHub Profile
@maddouri
maddouri / wsl-msg.sh
Last active March 28, 2024 02:42
WSL alternative to the Linux notify-send command implemented as a Bash function that uses the Windows msg.exe command
# Usage Examples:
# wsl-msg hello world
# ./some_script && wsl-msg "Task Done"
wsl-msg() {
tput bel
# C:\Windows\System32\msg.exe from Windows 10 Pro
msg.exe '*' /V /W "$@"
}
@maddouri
maddouri / subl.sh
Last active December 28, 2021 09:54
Bash function for calling Windows' Sublime Text from WSL2 (supports command line arguments and path translation)
#!/usr/bin/env bash
# Calls Windows' Sublime Text from WSL2
#
# Usage: subl [arguments] [files] Edit the given files
# or: subl [arguments] [directories] Open the given directories
# or: subl [arguments] -- [files] Edit files that may start with '-'
# or: subl [arguments] - Edit stdin
# or: subl [arguments] - >out Edit stdin and write the edit to stdout
#
@maddouri
maddouri / sizeof_as_compiler_error.h
Last active December 31, 2022 14:52
Trick to print `sizeof(type)` as a compiler error
#pragma once
#define sizeof_as_compiler_error(type) struct sizeof__##type##__is* (*sizeof__##type)[sizeof(type)] = -1;
@maddouri
maddouri / remove_cxx_comments.sh
Created May 19, 2020 22:23
Remove comments from C and C++ files (Requires GCC)
#/usr/bin/env bash
set -eu
USAGE="\nUSAGE: $0 -i INPUT_C_CXX_FILE -o OUTPUT_C_CXX_FILE [ -c G++_COMPILER ]\n"
INPUT_FILE=''
OUTPUT_FILE=''
COMPILER='g++'
while [[ "$#" -gt 0 ]] ; do
@maddouri
maddouri / cd_without_writing_cd.bashrc.sh
Created September 10, 2019 17:45
cd just by typing the directory's path in bash 🔥
# https://unix.stackexchange.com/a/37182/140618
shopt -s autocd
@maddouri
maddouri / ifnotnull.hpp
Last active August 20, 2019 21:32
Dereference a pointer only if it is not null
#pragma once
// Dereference a pointer only if it is not null
#define ifnotnull(p) for (auto* p__ = p; p__ != nullptr; p__ = nullptr) (*p__)
@maddouri
maddouri / latency.txt
Created August 1, 2019 20:39 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@maddouri
maddouri / manually-installed-packages.sh
Last active July 27, 2019 19:51
Generating a list of manually-installed packages in Ubuntu
#!/usr/bin/env bash
set -eux
# From
# * Generating list of manually installed packages and querying individual packages
# * https://askubuntu.com/a/492343/543097
# Tested on Ubuntu 18.04
comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
@maddouri
maddouri / on_scope_exit.hpp
Last active July 27, 2019 19:56
Neat C++11 scope guard inspired by Andrei Alexandrescu's CppCon 2015 talk about declarative control flow
#pragma once
#include <cstdint>
#include <exception>
#include <type_traits>
#include <utility>
// Usage
// ON_SCOPE_EXIT <callable> ;
// ON_SCOPE_EXIT_FAIL <callable> ;
@maddouri
maddouri / binary_processing.sh
Last active October 11, 2018 21:30
Fun with binary/hex data
# https://stackoverflow.com/a/2615112/865719
# https://stackoverflow.com/a/7826789/865719
# https://stackoverflow.com/q/2764051/865719
# echo -n 'Hello' | tohex
# tohex INPUT_FILE
function tohex() { hexdump -ve '1/1 "%.2x"' "$@" ; }
function todec() { hexdump -ve '1/1 "%.3d"' "$@" ; }
function tobin() {
xxd -b "$@" | cut -d" " -f2-7 | tr -d ' ' | tr -d '\n'