Skip to content

Instantly share code, notes, and snippets.

View ncruces's full-sized avatar

Nuno Cruces ncruces

View GitHub Profile
@ncruces
ncruces / adiantum-coverage.html
Created May 10, 2024 15:49
Adiantum coverage
<!DOCTYPE html>
<!-- saved from url=(0047)file:///tmp/cover1217815835/coverage.html#file3 -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>adiantum: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
@ncruces
ncruces / MonoSingleton.cs
Created October 27, 2022 14:26
A singleton Unity3D MonoBehaviour
using System;
using UnityEngine;
/// <summary>
/// Base class for <see cref="MonoBehaviour"/> of which there should be a single instance.
/// </summary>
/// <threadsafety>This class is <b>not</b> thread safe.</threadsafety>
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
static volatile T instance;
@ncruces
ncruces / zenity
Last active May 5, 2022 14:09
Zenity for Cygwin, MSYS2
#!/bin/sh
exec zenity.exe --unixeol --cygpath "$@"
@ncruces
ncruces / ReaderInputStream.java
Created July 2, 2021 17:51
InputStream that reads from a Reader
package io.github.ncruces.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
@ncruces
ncruces / regexp.js
Created June 5, 2020 12:32
Multi-line RegExp with comments
function regexp(...args) {
function cleanup(string) {
// remove whitespace, single and multi-line comments
return string.replace(/\s+|\/\/.*|\/\*[\s\S]*?\*\//g, '');
}
function escape(string) {
// escape regular expression
return string.replace(/[-.*+?^${}()|[\]\\]/g, '\\$&');
}
@ncruces
ncruces / ipv6.js
Last active July 2, 2021 09:43
Parse IPv6
// https://tools.ietf.org/html/rfc5952
function parseIPv6(addr) {
// does it have an IPv4 suffix
let ipv4 = /^([\s\S]*):(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(addr);
if (ipv4) {
// dot-decimal to ints
let dec = ipv4.slice(2).map(s => parseInt(s, 10));
if (dec.some(i => i > 255)) return null;
// ints to hexs
let hex = dec.map(i => ('0' + i.toString(16)).substr(-2));
@ncruces
ncruces / dns64.js
Last active December 24, 2023 03:18
Cloudflare DNS64 over HTTPS with any NAT64 prefix
// A Cloudflare worker that uses https://dns.google to offer DNS64 over HTTP
// for any NAT64 prefix.
//
// Google's DNS64 service only supports the 64:ff9b::/96 'well know prefix',
// which is not publicly routable over the internet.
// See https://nat64.xyz/ for a list of publicly available NAT64 services and
// their prefixes.
//
// To use this, deploy it to a worker, and then configure your DNS over HTTPS
// resolver with this URL (specify more than one prefix for redundancy):
@ncruces
ncruces / hammersley.js
Last active December 3, 2019 13:36
2D Hammersley low-discrepancy sequence
function hammersley(n, bias) {
if (isNaN(bias)) bias = (1-n)/(n+n);
function phi2(n) {
let r = 0;
let b = n.toString(2);
for (let i = 0; i < b.length; ++i) {
if (Number(b[i])) r += 1/(1 << b.length-i);
}
return r;
@ncruces
ncruces / cloudflare.ps1
Last active September 5, 2019 01:51
Whitelist Cloudflare's IP addresses in a Windows Firewall rule
param(
[Parameter(Mandatory=$true)]
[String]$rule
)
$ipv4 = (Invoke-WebRequest -UseBasicParsing -Uri "https://www.cloudflare.com/ips-v4").Content
$ipv6 = (Invoke-WebRequest -UseBasicParsing -Uri "https://www.cloudflare.com/ips-v6").Content
$ips = ($ipv4 + $ipv6).TrimEnd().Split([Environment]::NewLine)
Set-NetFirewallRule -DisplayName $rule -RemoteAddress $ips
@ncruces
ncruces / AndroidBitmap.java
Last active March 14, 2019 14:54
JNA wrapper for Android NDK bitmap.h
package io.github.ncruces.utils;
import android.graphics.Bitmap;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.sun.jna.JNIEnv;
import com.sun.jna.Native;