Lua Template Engine Yet Again

Introduction After writing a template engine in Lua, I found some deficiencies and wrote a few more iterations. After having used the template engine for quite a while, I’ve made a few enhancements and some fixes. This is hopefully the final version of the template engine. Newest Engine This engine is an iteration of the previous template_engine3.lua. The differences are: Bug fixes for escaping routines. An empty string will no longer cause a failure....

August 28, 2022 · John

Lua For Loop Scope

Introduction One odd thing about Lua when compared to other programming languages is, everything is global by default. Take this for example: function func_a() q_var = 7 end func_a() print(q_var) Once func_a is called q_var is available and set to 7. Even outside of func_a. You can mark a variable or function as local so it’s not global and pretty much anything written in Lua that’s mildly complex will use local a lot....

May 20, 2019 · John

Lua-stater a Lua State Machine

At work I use state machines extensively in one of our applications. I’ve found state machines to be very powerful when working with messaging. They are especially useful when combined with event based processing, where an event comes in, the current state can process and move to the next state based on the data received. People have made a few different Lua state machines but, they don’t work quite like I’d like....

March 4, 2019 · John

Lua Ternary

Lua doesn’t have true ternary operator but there is a way to mimic the behavior. This is one of the biggest issues people who aren’t used to Lua have when reading Lua code. For the most part Lua is simple and straight forward to understand. Assuming a blocky and easy to understand style is used. However, the pseudo ternary is often used and trips up people. So let’s look at it in some detail....

February 20, 2019 · John

Lua Case Insenstive Table

Introduction This is something I wrote for PenLight but alas it wasn’t merged. The problem they had is the use if __pairs which isn’t present in Lua 5.1. The project wants to maintain compatibility with 5.1 and LuaJIT which targets 5.1. All the Lua projects I deal with are Lua 5.3 and there isn’t a clean way to make a case insensitive table without using __pairs. So I’m posting this here because I find it useful....

February 12, 2019 · John

Lua-randxoxoxo a Lua Pseudo Random Number Generator

I spent a lot of time writing a number library in Lua just so I could write a hash library in Lua. Since I have this nice little number library I decided I should do more things with it. With that said, I’d like to introduce lua-randxoxoxo! lua-randxoxoxo is a pseudo random number generator. It’s pure Lua and allows you to have different random number generators with different seeds. Internally I’m using David Blackman and Sebastiano Vigna xoroshiro128+ algorithm....

November 23, 2016 · John

Lua-perforator a Lua Profiling Module

When I wrote the fixed width integer part of my Lua number library there were some performance issues. Even though I kept it simple and I knew a it wasn’t going to be quick, I did find there were a few places that needed to be optimized. To this end I wrote a bit of code to do some tracing and give me some statistics about what was happening. For example, the reduction code that handles wrapping was a major bottleneck....

November 11, 2016 · John

Lua-hashings a Lua Hash Library

A while back I wrote lua-nums and said it was working on a project that needed its features. Well, this is that project! lua-hashings is a pure Lua cryptographic hash library. This all started with crc32 and a work project. I was working with a third party API and they provided the ability to verify files using crc32 (only). At that time I wrote a crc32 calculation script. This got me thinking about Lua and hashing so I looked around to see what was out there....

October 20, 2016 · John

Lua-nums a Lua Number Library

One area that I’ve always found problematic in Lua is its number type. It’s a one size fits all solution and there are things I want to do that doesn’t really work with it. I’m mainly a C programmer and I’ve found unsigned integer wrapping to be useful in many situations. Especially when dealing with crypto algorithms. Another lacking feature of Lua’s number type is its range. Lua 5.3 internally uses a signed 64 bit type which provides a decent range but it’s not always enough....

September 14, 2016 · John

Binary Headers

Introduction Earlier in the year I wrote a Lua implementation of xxd -i (it’s at the bottom under the “Embedding” section). I wrote this because a few platforms I was working with didn’t support xxd. I didn’t want to attempt compiling and installing it. I had Lua installed on these platforms so it was easier to write a Lua script to do the same thing. I only needed -i output so implementing all of xxd wasn’t necessary....

October 23, 2015 · John