Skip to content

Instantly share code, notes, and snippets.

View ahamez's full-sized avatar
🦉

Alexandre Hamez ahamez

🦉
View GitHub Profile
@ahamez
ahamez / create_structs_bench.exs
Created February 17, 2017 13:49
Benchmarks different struct creations
defmodule Struct do
defstruct [:a, :b, :c, :d, :e, :f, :g, :h]
end
defmodule CreateStruct do
def create1() do
struct(Struct, create1_fields())
end
@ahamez
ahamez / big_endian.java
Last active July 28, 2017 14:30
Read big endian in Java
// Convert 4 unsigned big endian bytes to int
int x = java.nio.ByteBuffer.wrap(bytes).getInt();
@ahamez
ahamez / hmac.cc
Created March 21, 2018 15:25
HMAC with OpenSSL
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <openssl/hmac.h>
int main()
{
// https://tools.ietf.org/html/rfc2104#section-3
@ahamez
ahamez / c++17.cc
Created July 20, 2018 13:22
Some C++17
// g++-8 -std=c++17 -Wall -Wextra -fsanitize=address c++17.cc
#include <array>
#include <cstddef>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <tuple>
@ahamez
ahamez / deduction_guide.cc
Created July 21, 2018 07:44
C++17 deduction guides
#include <functional>
#include <iostream>
#include <string>
#include <variant>
template <typename... Ts>
struct make_visitor
: Ts...
{
using Ts::operator()...;
@ahamez
ahamez / padding_ub.cc
Last active July 22, 2018 15:34
Padding and undefined behavior
// When compiled (macOS, 64 bits) with g++-8 -O3 -std=c++14 ub.cc -Wall -Wextra:
// 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
// Notice the '1' coming from nowhere :-)
// Note that clang++ produces:
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#include <cstdint>
@ahamez
ahamez / double_portable_serialization.cc
Last active December 28, 2024 22:34
Portable serialization of floats using frexp and ldexpr. C++17
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>
// IEEE 754 does not specify endianess (https://en.wikipedia.org/wiki/Endianness#Floating_point).
// So, to have a portable representation, we can "decompose" the floating-point value using frexp().
@ahamez
ahamez / yocto_merge_manifest_licenses.cc
Created August 24, 2018 09:55
Merge FOSS licenses from Yoco MANIFEST files.
// clang++ -I. -std=c++17 -Wall -Wextra yocto_merge_manifest_licenses.cc
#include <fstream>
#include <iostream>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <boost/algorithm/string/replace.hpp>
@ahamez
ahamez / pointer_member.cc
Last active August 24, 2018 18:38
Pointer to member variable as template parameter
// clang++ -std=c++17 -Wall -Wextra pointer_member.cc
#include <iostream>
struct foo
{
int a;
int b;
};
# install aws cli first and configure it with credentials and default region
# the script will iterate over all regions of AWS
#! /bin/sh
for region in `aws ec2 describe-regions --output text | cut -f4`
do
echo -e "\nListing Instances in region:'$region'..."
# aws ec2 describe-instances --query "Reservations[*].Instances[*].{IP:PublicIpAddress,ID:InstanceId,Type:InstanceType,State:State.Name,Name:Tags[0].Value}" --output=table --region $region