Skip to content

Instantly share code, notes, and snippets.

View BastienClement's full-sized avatar

Bastien Clément BastienClement

View GitHub Profile
@BastienClement
BastienClement / async.js
Last active August 29, 2015 13:58
ES6 async / yield
/*
Copyright (c) 2014 Bastien Clément
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@BastienClement
BastienClement / gist:1074644
Created July 10, 2011 15:58
Magic Quote Killer
<?php
// Turn off magic_quotes_runtime
if (get_magic_quotes_runtime())
@ini_set('magic_quotes_runtime', false);
// Strip slashes from GET/POST/COOKIE (if magic_quotes_gpc is enabled)
if (get_magic_quotes_gpc())
{
function stripslashes_array($array)
{
@BastienClement
BastienClement / gist:1278620
Created October 11, 2011 16:35
GPS, format meters
function format_meters(m) {
if(m > 1000)
return (Math.round(m)/1000)+"km";
else if(m < 1)
return (Math.round(m*1000*1000)/1000)+"cm";
else
return (Math.round(m*1000)/1000)+"m";
}
@BastienClement
BastienClement / gist:1286687
Created October 14, 2011 09:36
Konami Code JS
function konami(cb) {
var code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
var pos = 0;
document.onkeydown = function(e) {
if(code[pos] === e.which) {
if(++pos >= code.length) {
if(typeof cb == String.fromCharCode(102,117,110,99,116,105,111,110))
cb();
pos = 0;
@BastienClement
BastienClement / gist:1373027
Created November 17, 2011 12:25
iban.coffee
`
// BigInt.js - Arbitrary size integer math package for JavaScript
// Copyright (C) 2000 Masanao Izumo <iz@onicos.co.jp>
// Version: 1.0.1
// Licence: GPL
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
@BastienClement
BastienClement / gist:2412198
Created April 18, 2012 09:04
Authenticator token generation
/// <summary>
/// 10^0..10^8 (NumDigits used in the HOTP)
/// </summary>
private static readonly int[] DigitsPower = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
private static int DynamicTruncate(byte[] hash)
{
int offset = hash[hash.Length - 1] & 15;
return ((hash[offset] & 127) << 24) + ((hash[offset + 1] & 255) << 16) + ((hash[offset + 2] & 255) << 8) +
@BastienClement
BastienClement / guild.lua
Created December 17, 2012 00:36
Modified version of the Guild Datatext from ElvUI
local E, L, V, P, G, _ = unpack(select(2, ...)); --Inport: Engine, Locales, PrivateDB, ProfileDB, GlobalDB, Localize Underscore
local DT = E:GetModule('DataTexts')
-- localized references for global functions (about 50% faster)
local join = string.join
local format = string.format
local find = string.find
local gsub = string.gsub
local sort = table.sort
local ceil = math.ceil
@BastienClement
BastienClement / hw2_ex3.lua
Last active December 14, 2015 12:59
DS Homework 2, ex 3
local D0 = {9,6,1,3,0,4,5,7,8,2}
local function D_next(D)
local Dn = {}
for i = 1, #D do
Dn[i] = (D[i] + 1) % 10
end
return Dn
end
@BastienClement
BastienClement / preg_replace_safe.php
Last active January 16, 2016 16:21
Smarter preg_replace with support for callback functions and the removed /e modifier.
<?php
function preg_replace_safe($search, $replace, $subject, $evaluator = NULL) {
if (is_null($evaluator)) {
// A custom evaluator
// function($c) { return eval($c); }
// is required if the replacement code uses the $this reference.
$evaluator = function($code) { return eval($code); };
}
@BastienClement
BastienClement / blocklist.js
Created June 9, 2014 18:02
Blocklist experiments
var ip = require('ip');
console.time("load");
var list = require("./bt_level1.js").map(function(block) {
try {
block.start = ip.toLong(block.start);
block.end = ip.toLong(block.end);
} catch(e) {
console.log(e, block);
process.exit(0);