Skip to content

Instantly share code, notes, and snippets.

View bitsmanent's full-sized avatar

Claudio Alessi bitsmanent

View GitHub Profile
@bitsmanent
bitsmanent / sock.js
Last active January 3, 2019 16:53
Simple websocket API
/* Simple WebSocket API.
* Dependencies: none.
*
* Message fields:
* msgid: server replies must match this
* data: the actual data
*
* Interface in a nutshell:
* - var sd = sock.open("ws://host:port", open_callback)
* - sock.send(sd, data, reply_callback);
@bitsmanent
bitsmanent / wparsef.c
Last active October 15, 2018 17:56
Parse a printf-like format string
#include <wchar.h>
typedef struct {
wchar_t cs;
wchar_t flags[8];
wchar_t mod[3];
int w;
int len;
} Printf;
@bitsmanent
bitsmanent / fib.js
Created October 10, 2018 18:53
fibonacci (the go way)
function fib() {
var a = 0, b = 1;
return function() {
var t = a;
a = b;
b = t + b;
return a;
};
}
@bitsmanent
bitsmanent / trackxhr.js
Last active August 22, 2018 17:08
Track XMLHttpRequest (new)
function trackxhr() {
var oxhr = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, usr, pwd) {
console.log("XHR OUT", url);
this.addEventListener("load", function() {
console.log("XHR IN", this.responseText);
});
return oxhr.apply(this, arguments);
};
@bitsmanent
bitsmanent / utils.js
Last active October 20, 2020 13:05
Modular collection of JS functions
/* Do not use code you have not read first.
* Original file: https://gist.github.com/clamiax/e1db98deaf94bb1ccf8556d993245afb */
(() => {
"use strict";
var eventsmap = []; /* on(), onevent(), off() */
function $(sel, ctx = document) {
return ctx.querySelector(sel);
}
@bitsmanent
bitsmanent / srperm.c
Created June 19, 2018 17:56
Deal with permutations with repetitions of a given string (with callback)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void onperm(char *s, int len);
unsigned powu(unsigned base, unsigned exp);
int srperm(char *s, int len, void (*fn)(char *, int));
void
onperm(char *s, int len) {
@bitsmanent
bitsmanent / sperm.c
Last active June 19, 2018 17:57
Deal with permutations of a given string (with callback)
#include <stdio.h>
#include <string.h>
void swap(char *a, char *b);
void onperm(char *s);
void sperm(char *s, int len, int cur, void (*fn)(char *s));
void
onperm(char *s) {
printf("%s\n", s);
@bitsmanent
bitsmanent / sess.php
Last active October 30, 2017 11:37
Single-routine PHP session handling
define("SESSION_FILE", "/tmp/session.txt");
$session = [];
function sess($k, $v = NULL) {
global $session;
if($session == NULL) {
if(!file_exists(SESSION_FILE))
touch(SESSION_FILE);
$session = file_get_contents(SESSION_FILE);
@bitsmanent
bitsmanent / table.php
Last active October 31, 2017 16:47
Prints a tabular representation of an hash
/*
* For example, the following code:
*
* table(["id", "word"], [
* ["id" => 1, "word" => "Hello"],
* ["id" => 2, "word" => "world"]
* ]);
*
* prints this table:
*
@bitsmanent
bitsmanent / html2zc.js
Last active June 9, 2017 16:53
Build an emmet string from actual DOM
/* Sample usage: html2zc(document.documentElement); */
(function() {
"use strict";
function parse(node, code) {
var chld = [], i, nc, c;
if(skipnode(node))
return;
code += tozc(node);