Skip to content

Instantly share code, notes, and snippets.

View yowcow's full-sized avatar
💮
Job well done

Yoko OYAMA yowcow

💮
Job well done
View GitHub Profile
@yowcow
yowcow / php-cgi.sh
Created March 22, 2016 04:07
php-cgi from CLI
REDIRECT_STATUS=CGI SCRIPT_FILENAME=./index.php php-cgi -d include_path="./inc:$(php -r 'echo get_include_path();')"
@yowcow
yowcow / prove.sh
Last active April 13, 2016 07:51
Color STDERR output
prove -vr t 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
@yowcow
yowcow / bin-dec-hex.p6
Last active May 3, 2018 09:38
Binary/Decimal/Hex in Perl 6
use v6;
# Hex => Dec
:16("ff").base(10); # 255
# Dec => Bin
:10("10").base(2); # 1010
# Bin => Hex
:2("1111").base(16); # F
@yowcow
yowcow / bin-dec-hex.pl
Last active May 7, 2016 10:09
Binary, decimal, and hex in Perl 5
use strict;
use warnings;
use Test::More;
# See `perldoc -f pack` for documentation.
subtest 'Test unsigned char (8-bit)' => sub {
my $packed = pack('B8', '0001'.'1111');
@yowcow
yowcow / Makefile
Created June 11, 2016 08:29
Makefile example for C
.PHONY: clean test nm
CFLAGS=-Wall -Werror -O3 -I. -fPIC
all: build/libexample.so build/libexample.a nm
build:
-mkdir -p build
build/libexample.so: build/example.o
@yowcow
yowcow / quicksort.pl
Created June 26, 2016 10:18
Quicksort
use common::sense;
use Data::Dumper;
sub quicksort {
my ($array, $left, $right) = @_;
return if $left >= $right;
my $pivot = $array->[int(($left + $right) / 2)];
my ($i, $j) = ($left, $right);
@yowcow
yowcow / filter-url.php
Last active September 15, 2016 03:49
Filter URL in PHP
<?php
function filter_url($url) {
$uri = parse_url($url);
$uri['host'] = 'hogefuga.cn';
$new_url = sprintf('%s://%s', $uri['scheme'], $uri['host']);
if (array_key_exists('port', $uri)) {
@yowcow
yowcow / load-fixture.js
Created December 13, 2016 04:02
JavaScript fixture loading into MySQL
const queries = [
["SET FOREIGN_KEY_CHECKS=0", []],
["TRUNCATE TABLE foo", []],
["INSERT INTO foo (id, name) VALUES (?, ?)", [1, "foooo"]],
["TRUNCATE TABLE bar", []],
["INSERT INTO bar (id, name) VALUES (?, ?)", [2, "baaaar"]],
["SET FOREIGN_KEY_CHECKS=1", []]
]
const db = mysql.createConnection(...)
@yowcow
yowcow / stub-http-request.js
Last active December 14, 2016 05:57
Stub HTTP request with Sinon
import expect from 'expect'
import fs from 'fs'
import http from 'http'
import sinon from 'sinon'
import { parseString } from 'xml2js'
const parseXML = (url, callback) => {
parseXML.get(
url,
(err, res, xml) => {
@yowcow
yowcow / express-csrf.test.js
Last active December 19, 2016 11:22
ExpressJS CSRF protection
const bodyParser = require('body-parser')
const csrf = require('csurf')
const expect = require('expect')
const express = require('express')
const request = require('supertest')
const session = require('cookie-session')
const createApp = () => {
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))