Skip to content

Instantly share code, notes, and snippets.

<?php
$pattern = '/(?>\.no-touch\s+)?((?>[^,{;}\s][^,{;}]*?:hover)(?>[^,{;}]+)?)(?=[{,])/i';
$replacement = ".no-touch $1";
$subject = <<<CSS
a
{
/**
* Adapted from <http://neilk.net/blog/2000/06/01/abigails-regex-to-test-for-prime-numbers/>.
*/
function isPrime(n) {
return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'));
}
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(1); // false
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(2); // true
(function (n){return !/^#?$|^(##+?)\1+$/.test(Array(n+1).join('#'))})(7); // true
@matthieuprat
matthieuprat / flush_dns_cache.sh
Last active August 29, 2015 14:09
Flush DNS cache on Mac OS X
#! /bin/sh
# Flushes the DNS cache on Mac OS X
#
# From https://www.madboa.com/blog/2014/10/24/mac-dns-cache/
#
# Only tested on Yosemite.
MAC_OS_X_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}')
@matthieuprat
matthieuprat / git-rsync
Last active August 18, 2021 05:01
Sync a local Git work tree with a remote one
#!/bin/sh
#
# Author: Matthieu Prat <matthieuprat@gmail.com>
# Date: 01/22/2015
#
# Sync a local work tree with a remote one.
# It's rsync on steroids within large Git repositories.
USAGE='<repository>'
LONG_USAGE='Sync a local work tree with a remote one.'
@matthieuprat
matthieuprat / timer
Last active January 22, 2016 08:10
Minimal timer
data:text/html,<script>var t = 3605</script><body style="display: flex; align-items: center; justify-content: center; font-family: sans-serif; font-size: 40vh;"><div></div><script>var e = document.getElementsByTagName('div')[0]; var tid = setInterval(() => {e.innerText = [~~(t/3600), ~~(t/60) % 60, t % 60].reduce((a, t) => ((a.length || t) && a.push(t), a), []).map(t => ('0' + t).slice(-2)).join(':') || '00'; --t < 0 && clearInterval(tid)}, 1000)</script></body>
@matthieuprat
matthieuprat / random-token.js
Last active August 29, 2015 14:23
Generate a random token (in a fucked-up way)
Array.apply(null, Array(parseInt(process.argv[1]) || 40))
.map(Math.random)
.map(function (n) {return n*62})
.map(Math.floor)
.map(String.prototype.charAt.bind('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'))
.join('')
@matthieuprat
matthieuprat / mkdir-p-thread-safeness
Last active December 15, 2015 12:19
PHP's recursive mkdir() is not thread-safe
$ cd $(mktemp -d) && echo -n 1:2 | xargs -d: -n1 -P2 php --no-php-ini -r 'mkdir(str_repeat("dir/", 100) . $argv[1], 0777, true) or exit(1);' && echo OK
Warning: mkdir(): File exists in Command line code on line 1
$ cd $(mktemp -d) && echo -n 1:2 | xargs -d: -n1 -P2 php --no-php-ini -r 'system(sprintf("mkdir -p %s", str_repeat("dir/", 100) . $argv[1]), $r); exit($r);' && echo OK
OK
$ cd $(mktemp -d) && echo -n 1:2 | xargs -d: -n1 -P2 python -c 'import os, sys; os.makedirs('dir/' * 100 + sys.argv[1]);' && echo 'OK'
OK
@matthieuprat
matthieuprat / random-token.sh
Last active August 29, 2015 14:24
Generate an alpha-numeric random token
#!/bin/sh
until openssl rand -base64 40 | grep -v '+\|/'; do :; done
#! /bin/bash
while read -r f
do
if $(git diff-files --quiet -- ":(literal)$f")
then
# Check the file in the worktree since there are no unstaged changes.
flake8 "$f" || err=1
else
# Pipe staged changes to flake8 and replace 'stdin:<error>' with
/**
* Returns a promise that is fulfilled with an array of promise state
* snapshots, but only after all the original promises have settled, i.e.
* become either fulfilled or rejected.
*
* See https://github.com/kriskowal/q/wiki/API-Reference#promiseallsettled
*/
allSettled = (promises) => {
let resolve, outer = new Promise((r) => { resolve = r })
let snapshots = [], settledCount = 0