Skip to content

Instantly share code, notes, and snippets.

@sudo-give-me-coffee
Last active May 27, 2022 14:32
Show Gist options
  • Save sudo-give-me-coffee/3e1234b55b418e741a92bcb24ce51038 to your computer and use it in GitHub Desktop.
Save sudo-give-me-coffee/3e1234b55b418e741a92bcb24ce51038 to your computer and use it in GitHub Desktop.
string_builder = {}
string_builder.__index = string_builder
function string_builder.new()
local self = setmetatable({}, string_builder)
self.__data = {}
self.__count = 0
self.__lenght = 0
return self
end
function string_builder.append(self, str)
if type(self) == "string" then
local o = string_builder:new():append(self)
return o
end
self.__data["_"..(self.__count+1)] = tostring(str)
self.__count = self.__count+1
self.__lenght = self.__lenght+1
return self
end
function string_builder.remove(self,idx)
if self.__data["_"..tonumber(idx)] == nil then return end
self.__data["_"..tonumber(idx)] = nil
if self.__lenght > 0 then
self.__lenght = self.__lenght-1
end
return self
end
function string_builder.value(self,idx)
return self.__data["_"..tonumber(idx)]
end
function string_builder.__tostring(self)
local s = ""
local i = 1
while i<self.__count+1 do
if not (self.__data["_"..i] == nil) then
s = s..self.__data["_"..i]
end
i=i+1
end
return s
end
function string_builder.__type(self)
return "string_builder"
end
function string_builder.__len(self)
return self.__lenght
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment