Skip to content

Instantly share code, notes, and snippets.

View gilzoide's full-sized avatar

Gil Reis gilzoide

View GitHub Profile
@gilzoide
gilzoide / lua_string_replace.c
Last active September 7, 2021 14:03
A plain string replacement function for Lua with no magic characters
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/// Performs plain substring replacement, with no characters in `pattern` or `replacement` being considered magic.
/// There is no support for `string.gsub`'s parameter `n` and second return in this version.
/// @function string.replace
/// @tparam string str
/// @tparam string pattern
/// @tparam string replacement
@gilzoide
gilzoide / clang_tu_from_ast.py
Last active February 7, 2024 21:17
Snippet for creating a clang TranslationUnit in python by calling clang as a subprocess instead of parsing with Index.parse
import subprocess
import tempfile
import clang.cindex as clang
def create_translation_unit_with_ast(source_file_path, clang_args=[], clang_exe='clang'):
"""
Create a `clang.TranslationUnit` for a source file by compiling it with clang as a
subprocess instead of using `clang.Index.parse`.
@gilzoide
gilzoide / hotreload.lua
Last active July 27, 2020 03:15
A module for detecting updated files in a LÖVE game
-- Detecting updated files for hot reloading in a LÖVE game
local hotreload = {}
-- Monitor the "src" and "assets" paths, can be changed to any existing folders in your project, separated by spaces
local monitor_path = "src assets"
-- External command for monitoring the filesystem
-- Needs fswatch available for execution in PATH (no errors will be thrown, but reload will not work)
-- https://github.com/emcrisostomo/fswatch
local fswatch_cmd = "fswatch --recursive --event Updated " .. monitor_path
@gilzoide
gilzoide / xor.lua
Last active December 16, 2023 12:33
Logical XOR in lua
-- Returns false if value is falsey (`nil` or `false`), returns true if value is truthy (everything else)
function toboolean(v)
return v ~= nil and v ~= false
end
-- Returns true if one value is falsey and the other is truthy, returns false otherwise
function xor(a, b)
return toboolean(a) ~= toboolean(b)
end
@gilzoide
gilzoide / pairs_not_numeric.lua
Last active June 22, 2020 13:36
A version of the `pairs` Lua function that ignores numeric keys
-- Usage: for k, v in kpairs(t) do ... end
local function knext(t, index)
local value
repeat
index, value = next(t, index)
until type(index) ~= 'number'
return index, value
end
function kpairs(t)
return knext, t, nil
@gilzoide
gilzoide / FindReplxx.cmake
Created July 7, 2018 14:59
FindReplxx CMake module
# FindReplxx
# -------------
# Locate replxx library. This module defines:
#
# REPLXX_FOUND - true if replxx library was found
# REPLXX_LIBRARIES - replxx library
# REPLXX_INCLUDE_DIRS - where to find replxx.hxx and replxx.h
# REPLXX_VERSION - the version of replxx found (parsed from header file)
find_path(REPLXX_INCLUDE_DIRS NAMES replxx.hxx replxx.h PATH_SUFFIXES replxx/include)