String Arithmetic Library
Disclaim: This will crash the default shell. You must use an alternative shell
This library allows you to preform arithmetic on strings by using metatables to overload operators
SCPaste: rUwGtDf3Cr (https://p.sc3.io/rUwGtDf3Cr)
Install:
pastebin get rUwGtDf3Cr str_math.lua
or
wget https://p.sc3.io/api/v1/pastes/rUwGtDf3Cr/raw str_math.lua
Usage:
#!str_math
—- Concatenation
—- Works with types: string, number, boolean, nil
local greeting = "Hello" + ", World!" —- greeting == "Hello, World!"
—- Substring
—- If left operand is a number subtract from beginning of string
—- If right operand is a number subtract from end of string
local greeting = 1-"Hello, World!"-8 —- greeting == "ello"
-- Repetition
local greeting = "Hi"*4 -- greeting == "HiHiHiHi"
—- Splitting
—- If right operand is a number split string into equal length pieces
—- If right operand is a string split string by delimiter
local tbl = "Hello, World!" / 4 —- tbl == {"Hell", "o, W", "orld"}
local tbl = "Hello, World!" / ", " —- tbl == {"Hello", "World!"}
—- Modulo returns remaining characters not equal to length
local str = "Hello, World!" % 4 —- str == "!"
-- Unary Minus reverses string
local greeting = -"Hello, World!" -- greeting == "!dlroW ,olleH"
p.s. This is probably the worst thing I've ever made
I've updated it with a "fix" for the default shell crash.
Example
local old_mt = dofile("str_math.lua")
-- your code
debug.setmetatable("", old_mt)
Updated with a better fix for crashes
How to use:
In script
#!str_math
-- Must be first line in file
or from shell
str_math <script>
Added unary minus operator to reverse strings
Example:
local greeting = -"Hello, World!" -- greeting == "!dlroW ,olleH"