Skip to content

Instantly share code, notes, and snippets.

View CAFxX's full-sized avatar

Carlo Alberto Ferraris CAFxX

View GitHub Profile
@CAFxX
CAFxX / utf8.rl
Last active May 4, 2022 05:16
Ragel machine definition for valid UTF-8 encodings
%%{
utf8 =
( 0..127 | 192..223 128..191 | 224..239 128..191 128..191 | 240..247 128..191 128..191 128..191 )
- ( 244 144..191 any any | 245..247 any any any ) # over U+10FFFF
- ( 0xC0..0xC1 any ) # overlong 2-byte encodings
- ( 224 128..159 any ) # overlong 3-byte encodings
- ( 240 128..143 any any ) # overlong 4-byte encodings
- ( 237 160..191 any ) # invalid utf-16 surrogate pairs
;
main := utf8* ;
@CAFxX
CAFxX / fastdiv.cpp
Created May 2, 2022 09:04
libdivide runtime cache
// libdivide.h - Optimized integer division
// https://libdivide.com
//
// Copyright (C) 2010 - 2021 ridiculous_fish, <libdivide@ridiculousfish.com>
// Copyright (C) 2016 - 2021 Kim Walisch, <kim.walisch@gmail.com>
//
// libdivide is dual-licensed under the Boost or zlib licenses.
// You may use libdivide under the terms of either of these.
// See LICENSE.txt for more details.
@CAFxX
CAFxX / re2struct.go
Last active April 28, 2022 01:51
Parse regular expressions into structs (Golang)
package main
import (
"encoding"
"fmt"
"reflect"
"regexp"
"strconv"
"time"
)
@CAFxX
CAFxX / bigquery_prevent_concurrent_runs.sql
Last active April 23, 2022 07:30
Ensure at most a single BigQuery scheduled query is running at one time
DECLARE check_running_jobs DEFAULT (
SELECT IF(COUNT(*) > 1, ERROR('query already running'), NULL)
FROM `region-name`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE state <> 'DONE'
);
@CAFxX
CAFxX / custom_key_map.go
Created April 22, 2022 09:40
Golang map with custom-compare/hash keys
package main
import (
"encoding/binary"
"fmt"
"hash/maphash"
"math/rand"
)
type Comparable[T any] interface {
@CAFxX
CAFxX / example_test.go
Created April 22, 2022 08:24
reflect.SliceHeader and reflect.StringHeader examples
package reflect_test
// Extracted from https://go-review.googlesource.com/c/go/+/401434
import (
"fmt"
"reflect"
"unsafe"
)
package shortstring
import (
"reflect"
"runtime"
"unsafe"
)
type shortstring struct {
ptr unsafe.Pointer
@CAFxX
CAFxX / LUT.c
Created January 18, 2022 01:17
#include <immintrin.h>
static int lut32x(int32_t *inout, __m512i lutk[], __m512i lutv[], int nlut) {
__m512i bin = _mm512_set1_epi32(*inout);
for (int i=0; i<nlut; i++) {
__mmask16 mask = _mm512_cmpeq_epi32_mask(bin, lutk[i]);
if (mask == 0) continue;
__m512i bout = _mm512_maskz_compress_epi32(mask, lutv[i]);
*inout = _mm_extract_epi32(_mm512_extracti32x4_epi32(bout, 0), 0);
package container
type BijectiveMap[A, B comparable] struct {
m1 map[A]B
m2 map[B]A
}
func (m *BijectiveMap[A, B]) GetB(a A) (B, bool) {
if m.m1 == nil {
var zero B
// ==UserScript==
// @name La Stampa/Repubblica Adblock Modal Remover
// @name:it La Stampa/Repubblica Elimina Messaggio Adblock
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Remove the adblock modal on lastampa.it and repubblica.it
// @description:it Elimina il messaggio modale che impedisce l'uso dei siti di La Stampa (lastampa.it) e Repubblica (repubblica.it) in caso di presenza di adblocker
// @author CAFxX
// @match https://*.lastampa.it/*
// @match https://*.repubblica.it/*