Skip to content

Instantly share code, notes, and snippets.

@pnck
pnck / create-cert.sh
Last active December 2, 2020 10:23 — forked from sethvargo/create-certs.sh
x509 v3 self signed certificate
# Define where to store the generated certs and metadata.
DIR="$(pwd)"
# Create the openssl configuration file. This is used for both generating
# the certificate as well as for specifying the extensions. It aims in favor
# of automation, so the DN is encoding and not prompted.
cat > "${DIR}/openssl.cnf" << EOF
[req]
default_bits = 2048
@pnck
pnck / printer.hpp
Last active November 27, 2019 09:29
#include <map>
template <typename CT=char, typename=std::enable_if_t<std::is_same_v<CT,char>>>
class Printer {
private:
template<typename T>
class _has_to_string {
template<typename U>
static constexpr bool _help(decltype(&U::to_string)) noexcept { return true; }
template<typename U>
// only works on linux
// c++17 standard required
// g++-8 -g -std=c++17 -Wl,-E foo.cpp -ldl
#include <any>
#include <cxxabi.h>
#include <dlfcn.h>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
@pnck
pnck / variant.hpp
Created April 25, 2019 12:10
simple variant implement for c++11/14
#ifndef UTIL_VARIANT_HPP
#define UTIL_VARIANT_HPP
#include <type_traits>
#include <memory>
#include <tuple>
#if __cplusplus >= 201703L
#include <variant>
@pnck
pnck / translate_scanner.py
Created March 19, 2019 13:36
scan specific tagged string in files and generate a ragel source that replace tag with translations
#!/usr/bin/env python3
import sys
import re
import json
import hashlib
import itertools
import binascii
LANGUAGES = ['zh_CN', 'en_US', ]
@pnck
pnck / currying.cpp
Last active July 23, 2020 07:11
convert std::function into a currying-able object
template <typename F>
struct curry_wrapped {};
template <typename R>
struct curry_wrapped<std::function<R()>> {
std::function<R()> _f;
// Calling with no parameters ==> call original function directly
// 无参调用 ==> 直接调用原函数
inline R operator()() {
return _f();
@pnck
pnck / yield_coro.py
Last active January 22, 2019 08:48
crorotine experiment implemented with yield
class Dispatcher:
def __init__(self):
self.tasks = []
def reg_task(self, t):
self.tasks.append(t)
def run(self):
while len(self.tasks):
for t in self.tasks:
@pnck
pnck / static_set.hpp
Created December 15, 2018 10:38
a compile-time set type with elements unique check
#include <tuple>
using ElementType = size_t;
template<ElementType ... es>
class SetValidator {
protected:
static inline constexpr auto cs_ = std::make_tuple(es...);
template<size_t _i, size_t _j>
static constexpr bool cmp_() {
#include <iostream>
#include <utility>
#include <type_traits>
template<typename T>
struct comparable {
virtual const T value() const = 0;
};
template<typename T>
@pnck
pnck / feature_trait.cpp
Last active November 20, 2018 12:15
test feature trait
//
// Created by pnck on 2018/11/19.
//
#include <type_traits>
#include <iostream>
#include <functional>
#include <random>
#include <list>
#include <deque>