Skip to content

Instantly share code, notes, and snippets.

View tylerneylon's full-sized avatar

Tyler Neylon tylerneylon

View GitHub Profile
tylerneylon / utf8.c
Last active May 27, 2024 16:21
C utf-8 encoder/decoder
// This macro tests if a char is a continuation byte in utf8.
#define IS_CONT(x) (((x) & 0xc0) == 0x80)
// This returns the code point encoded at **s and advances *s to point to the
// next character. Thus it can easily be used in a loop.
int decode_code_point(char **s) {
int k = **s ? __builtin_clz(~(**s << 24)) : 0; // Count # of leading 1 bits.
int mask = (1 << (8 - k)) - 1; // All 1s with k leading 0s.
int value = **s & mask;
// k = 0 for one-byte code points; otherwise, k = #total bytes.
tylerneylon / copy.lua
Last active May 18, 2024 16:41
How to deep copy Lua values.
-- copy.lua
-- Lua functions of varying complexity to deep copy tables.
-- 1. The Problem.
-- Here's an example to see why deep copies are useful. Let's
-- say function f receives a table parameter t, and it wants to
tylerneylon / learn.lua
Last active May 16, 2024 05:47
Learn Lua quickly with this short yet comprehensive and friendly script. It's written as both an introduction and a quick reference. It's also a valid Lua script so you can verify that the code does what it says, and learn more by modifying and running this script in your Lua interpreter.
-- Two dashes start a one-line comment.
Adding two ['s and ]'s makes it a
multi-line comment.
-- 1. Variables and flow control.
tylerneylon / json.lua
Last active May 15, 2024 12:57
Pure Lua json library.
--[[ json.lua
A compact pure-Lua JSON library.
The main functions are: json.stringify, json.parse.
## json.stringify:
This expects the following to be true of any tables being encoded:
* They only have string or number keys. Number keys must be represented as
strings in json; this is part of the json spec.
tylerneylon / .block
Last active May 3, 2024 01:21
Quick js code to draw math functions in an SVG element.
license: mit
tylerneylon /
Last active May 2, 2024 12:46
A simple read-write lock implementation in Python.
# -*- coding: utf-8 -*-
A class to implement read-write locks on top of the standard threading
This is implemented with two mutexes (threading.Lock instances) as per this
wikipedia pseudocode:
tylerneylon / call_graph.awk
Created October 7, 2014 01:08
Parse Lua code and draw its call graph.
#!/usr/bin/awk -f
# call_graph.awk
# Usage:
# ./call_graph.awk my_program.lua | dot -Tpng > call_graph.png
# This is a script that generates a visual call graph
# for a Lua file. This script only shows calls made
# to functions defined within the input Lua file; that is,
tylerneylon / Makefile
Created December 3, 2015 23:56
Skeleton Lua C module for teaching.
rm *.o *.so demo.o printstack.o
cc -bundle -undefined dynamic_lookup -o demo.o printstack.o
demo.o: demo.c
cc -fPIC -o $@ -c $< -Ilua_src
tylerneylon /
Created March 3, 2024 20:36
A script to help with the Split Decisions word puzzles in the New York Times
#!/usr/bin/env python3
# coding: utf-8
./splits .in,cr..
This will print out all known words that match both patterns with fixed
letters per dot. In the above example, one answer pair would be:
tylerneylon / depth_first_traverse.js
Created August 24, 2023 18:56
A general depth-first traversal utility function in JavaScript
// This is a depth-first traversal function with a few nice features.
// * Call this function as depthFirstTraverse(root, tree, fn)
// `tree` is an object whose properties are nodes in a tree; the
// values are arrays of that node's child nodes.
// `root` is the starting point for the traversal; a property in `tree`.
// `fn` is called as in fn(node, depth, childNum) for each node.
// childNum is the index of node as a child of its parent;
// as a special case, the childNum of `root` is undefined.
// `depth` is 0 for the root, and in general indicates how