Skip to content

Instantly share code, notes, and snippets.

View starwing's full-sized avatar

Xavier Wang starwing

  • Chengdu
View GitHub Profile
@starwing
starwing / findup.rs
Created July 17, 2023 09:39
手机9键双拼方案搜索程序
use std::{
collections::{HashMap, HashSet},
fmt,
fmt::{Display, Formatter},
hash::Hash,
iter::FromIterator,
sync::OnceLock,
};
#[derive(Debug, Hash, Clone, Copy, Eq, PartialEq)]
@starwing
starwing / pathfinding.c
Created January 19, 2022 07:34
A* Path finding in Lua
/*
* AStarfinding
*
* local path_find = require "pathfinding"
*
* local function neighbors(add, x, y, dist)
* add(x+1, y, hint, 1)
* end
*
* local r = path_find(sx, sy, tx, ty, neighbors)
@starwing
starwing / promise.lua
Last active November 5, 2021 06:26
Promise for Lua
local assert = assert
local type = type
local getmetatable = getmetatable
local setmetatable = setmetatable
local xpcall = xpcall
local debug_traceback = debug.traceback
local Promise = {} do
local Fulfilled = "Fulfilled"
local Rejected = "Rejected"
@starwing
starwing / lbase58.c
Last active October 19, 2021 02:12
Base58 encoding in Lua.
#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#include <assert.h>
#include <string.h>
static size_t b58_posrelatI(lua_Integer pos, size_t len) {
if (pos > 0)
return (size_t)pos;
@starwing
starwing / lbase58.c
Created November 20, 2020 13:28
Lua Base58
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#define BASE 58
#define BYTE (1<<CHAR_BIT)
#define BASE58_ALPHABET "base58.Alphabet"
@starwing
starwing / funcidx.lua
Created June 14, 2020 08:14 — forked from Oberon00/funcidx.lua
Lua C API function HTML reference table data & generator (http://oberon00.github.io/lua-cfuncidx/index.html)
-- Helpers {{{1
local function fail_on_missing_tbl(t, missing_name)
missing_name = missing_name or 'table entry'
return setmetatable(t, {__index = function(t, k)
error(('No %s with key "%s" (in %s).'):format(missing_name, k, t))
end
})
end
fail_on_missing_tbl(_G, 'global');
@starwing
starwing / gen_area_code.lua
Last active January 16, 2020 11:59
Generate ID Code for some conditions
io.input "../../Downloads/area_code_2018.csv"
io.output "area_code.py"
local map = {}
for l in io.lines() do
local code = l:match "^%d%d%d%d%d%d"
if tonumber(code) % 1000 > 0 and not map[code] then
map[#map+1] = code
@starwing
starwing / list_add.cxx
Last active January 8, 2020 03:55
Linked List addition in C++
#include <string>
class List {
struct Node {
Node* next;
int val;
Node(int val, Node* next = nullptr)
: next(next), val(val) {}
};
Node* head_;
@starwing
starwing / iterm2-zmodem.sh
Last active December 16, 2019 08:06
Single file to use zmodem in iTerm2
#!/bin/bash
# vim: ft=sh nu et sw=2 fdc=2
select_one() {
local cmd; read -d '' cmd <<EOF
tell application "iTerm2" to activate
tell application "iTerm2" to set thefile to choose $1
do shell script "echo " & (quoted form of POSIX path of thefile as Unicode text)
EOF
osascript -e "$cmd" 2>/dev/null
@starwing
starwing / lmd5.c
Last active November 23, 2019 03:28
A simple MD5 module for Lua
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS
# define _CRT_NONSTDC_NO_WARNINGS
#endif
#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#if LUA_VERSION_NUM < 502