Skip to content

Instantly share code, notes, and snippets.

@max1220
max1220 / reverse.lua
Created January 26, 2016 16:59
Simple table reverse with O(n/2)
function reverse(tbl)
for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp
end
end
@max1220
max1220 / ImpliedStringFormat.lua
Last active May 28, 2018 14:48
Adds implied string formats to lua
-- Makes the module operator (%) between a string and a table behave like string.format
-- This works by setting the string metatable __mod function.
-- Example use:
-- str = "Hello %s, %d as hexadecimal is 0x%.2X" % {"world", 127, 127}
local mt = getmetatable("")
assert(mt, "string has no metatable")
mt.__mod = function(a,b)
if (type(a) == "string") and (type(b) == "table") then
return a:format(unpack(b))
@max1220
max1220 / cgi_helpers.lua
Last active July 8, 2016 16:42
helper function common for all CGI scripts
-- helper function common for all CGI
local t_insert = table.insert
local t_concat = table.concat
local t_remove = table.remove
local s_gfind = string.gfind or string.gmatch
local s_gsub = string.gsub
local s_char = string.char
local s_format = string.format
local i_write = io.write
@max1220
max1220 / xml_generate.lua
Last active February 7, 2018 22:32
Simple Lua XML generation library
-- Simple Lua XML generation library.
-- For example usage see test() function below.
-- Does not atempt to escape characters anywhere, so be carefull!
-- released under MIT License.
local insert = table.insert
local concat = table.concat
local function elem_t(name, parms, elems, parms_list)
local elem_t = {
@max1220
max1220 / router.lua
Last active September 10, 2020 03:41
simple openresty dynamic router
return function new_router(get_user)
-- This function returns a router table with some functions for dynamic routing.
local function pack(...)
return {...}
end
local get_user = get_user or function(request_uri, request_method, pattern_args, uri_args)
-- This function is called when a restricted route is accessed.
-- It should return something truethy when the user is authorized to access the resource.
-- The return value is passed to the handler of the route, so you could return a table representing the user.
@max1220
max1220 / debug_num_to_str.lua
Created May 28, 2018 14:36
Debug function that returns a lower n bytes from a numer as hexadecimal, binary and decimal.
local function num_to_str(num, bytes)
local num = bit.band(num, 2^(bytes*8) - 1) -- limit to selected bytes
local bits = {}
for i=bytes*8-1, 0, -1 do -- from highest bit to lowest
local bit = (bit.band(num, 2^i) == 0) and 0 or 1 -- check if the current bit is set
table.insert(bits, bit) -- append to bits table
end
return ("0x%.4X 0b%s 0d%d"):format(num, table.concat(bits), num)
end
return num_to_str
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
void main() {
SDL_Window *window;
SDL_Surface *screen;
@max1220
max1220 / braile.lua
Created April 4, 2019 17:15
Unicode Braile character graphics for Lua!
--[[
This library converts pixels into a sequence of utf8 braile characters,
for displaying higher-resolution graphics on terminal emulators.
It features 3 functions to turn pixel data into braile characters.
All of them return a list of lines, and use the draw_pixel_callback
function internally.
@max1220
max1220 / non_blocking_popen.lua
Last active February 20, 2024 07:26
non-blocking popen for Lua(using ffi)
-- Implements a basic binding for popen that allows non-blocking reads
-- returned "file" table only supports :read(with an optional size argument, no mode etc.) and :close
local function non_blocking_popen(cmd, read_buffer_size)
local ffi = require("ffi")
-- C functions that we need
ffi.cdef([[
void* popen(const char* cmd, const char* mode);
int pclose(void* stream);
int fileno(void* stream);