Skip to content

Instantly share code, notes, and snippets.

@brynne8
brynne8 / xlsx_read.lua
Created November 2, 2022 08:32
Lua read xlsx example
require('zip')
require('xml_parse')
local excel = zip_open('excel/test.xlsx')
if not excel then return end
local flag
flag = excel:find('xl/workbook.xml')
if flag then
local str = excel:read('*a')
@brynne8
brynne8 / markdown.lua
Last active December 19, 2021 08:32
Part of a Markdown lexer
local punct_space = lexer.punct + lexer.space
-- Handles flanking delimiters as described in
-- https://github.github.com/gfm/#emphasis-and-strong-emphasis in the cases
-- where simple delimited ranges are not sufficient.
local function flanked_range(s, not_inword)
local fl_char = lexer.any - s - lexer.space
local left_fl = lpeg.B(punct_space - s) * s * #fl_char +
s * #(fl_char - lexer.punct)
local right_fl = lpeg.B(lexer.punct) * s * #(punct_space - s) +
@brynne8
brynne8 / luajit-notes.md
Created October 21, 2021 13:04
LuaJIT的假设、陷阱和技巧

本文翻译自https://luapower.com/luajit-notes

LuaJIT假设

  • LuaJIT将具有常数键的表访问从循环中提取出来,因此不再(那么)需要以局部变量缓存模块函数。
  • LuaJIT从循环中提取常量分支,因此可以在循环中使用 if/elseand/or 来限定循环内核。
  • LuaJIT会将函数内联(除非把 ...select() 与非常量索引一起使用),因此使用复合函数函数限定循环内核是可行的。
  • 乘法和加法比访问内存的开销要小,所以将这些运算的结果存储在临时变量里可能会降低性能(寄存器溢出更多)
  • 使用 if/else 语句和使用and/or 表达式没有区别——它们会产生相同的破坏管道的分支代码(所以要避免在紧密循环中使用非常数and/or 运算符的表达式)。
  • 在x86上除法比乘法慢4倍,所以当除以一个常数的时候,(由于常数表达式是折叠的)将 x / c 转换为 x * (1 / c) ——LuaJIT已经为2的幂常量做了这件事,其语义是等价的。
@brynne8
brynne8 / tricks.md
Created October 20, 2021 13:36
加快Lua的小技巧

本文来自https://luapower.com/lua-tricks

这是一个Lua习语和技巧的集合,对于随意阅读代码的人来说,可能不会立即看到。

逻辑
not a == not b 两者都满足或都不满足
数字
math.min(math.max(x, min), max) 把x限制在[min, max]区间内
x ~= x 一个数是NaN