Skip to content

Instantly share code, notes, and snippets.

@killwing
killwing / bit-hack.cpp
Last active August 29, 2015 14:14
useful bit hacks
// round down to multiple of 1024
size_t roundDown(size_t s) {
return s & ~1023;
}
// round up to multiple of 1024
size_t roundUp(size_t s) {
return (s + 1023) & ~1023;
}
@killwing
killwing / hackertyper.js
Created June 9, 2011 06:48
[hackertyper] type codes in your shell...
var util = require('util');
var fs = require('fs');
var type = function(data, completer) {
var lines = data.split(' ');
var i = 0;
var timer = setInterval(function() {
var s = lines[i++];
while (lines[i] == '') {
@killwing
killwing / flickrshortener.html
Created June 9, 2011 08:54
[flickrshortener] make flickr link short
<html>
<head>
<script type="text/javascript">
function shorten() {
var s = document.getElementById("url").value.match(/^https?:\/\/[^/]*\bflickr\.com\/(photos\/[^/]+\/(\d+))/i);
if (s && s.length && s[2]) {
var num = s[2];
if (typeof num !== 'number') {
num=parseInt(num);
}
@killwing
killwing / algorithm.js
Created June 30, 2011 02:52
[algorithm] various algorithm samples
#!/usr/local/bin/node
var assert = require('assert');
// utils
Array.prototype.swap = function(a, b) {
if (a == b) {
return;
}
var tmp = this[a];
@killwing
killwing / mdcontentgen.js
Created December 29, 2011 11:24
[mdcontentgen] generate content list for markdown
#!/usr/local/bin/node
var fs = require('fs');
var file = process.argv[2];
if (!file) throw 'no input file';
var seq = false;
if (file == '-s') {
if (!process.argv[3]) throw 'no input file';
@killwing
killwing / seqgen.cfg
Created March 15, 2012 07:04
[seqgen] generate ascii sequence
Alice|Bob|Carol
1|1->2|INVITE(hello)
2|2->3|INVITE
-|3|waiting...
3|2<-3|200OK
4|1<-2|200OK
@killwing
killwing / tw2md.js
Created September 22, 2012 10:25
[tw2md] convert tiddler of tiddlywiki to strapdownjs
#!/usr/local/bin/node
var fs = require('fs');
Function.prototype.mlstr = function() {
var lines = new String(this);
return lines.substring(lines.indexOf("/*") + 2, lines.lastIndexOf("*/"));
};
String.prototype.dup = function(n) {
var sb = '';
@killwing
killwing / Registry.js
Created November 19, 2012 08:00
[Registry] a simple registry without moving elements
#!/usr/local/bin/node
var Registry = function() {
this.r = [null];
};
Registry.prototype.register = function(object) {
var id = this.r[0];
if (id === null) {
this.r.push(object);
@killwing
killwing / escapeXml.cpp
Created November 20, 2012 07:48
[escapeXml] escape XML special chars
#include <iostream>
#include <string>
using namespace std;
std::string escapeXml(const std::string& s) {
std::string ret;
std::string::size_type i = 0;
std::string::size_type pos = 0;
for (; i != s.size(); ++i) {
std::string rep;
@killwing
killwing / Expected.hpp
Created February 6, 2013 08:29
[Expected] Make exceptions be the error codes! from @andrei
#ifndef EXPECTED_H
#define EXPECTED_H
#include <exception>
#include <typeinfo>
// Make exceptions be the error codes!
template <typename T>
class Expected {
public: