local c={}function c.replicate(c,e)local Q=c for e=1,e or 1,1 do Q=Q..c end return Q end function c.reverse(c)local e=""for Q=#c,1,-1 do e=e..string.sub(c,Q,Q)end return e end function c.trim(c)local e=c:gsub("\094\037\115\042\040\046-\041\037\115\042\036","\037\049")return e end function c.charAt(c,e)local Q=string.sub(c,e,e)return Q end function c.charCodeAt(c,e)local Q=string.byte(string.sub(c,e,e))return Q end function c.split(c,e)if#c==0 or not e then return{c}end local Q={}for e=1,#c,1 do Q[#Q+1]=string.sub(c,e,e)end if#e==0 then return Q end local l=1 local k={}local d=1 while d<=#Q do local c=""local O=1 while O<=#e do local b=Q[(d+O)-1]local F=k[l]or""if O>1 and not b then k[l]=F..c break end c=c..b local i=string.sub(e,1,O)if c~=i then k[l]=F..b break end if c==e then k[l]=F l=l+1 end O=O+1 end if d+#c>=#Q and c==e then k[#k+1]=""end d=d+#c end return k end function c.segment(c,e,Q)if type(c)~="\115\116\114\105\110\103"then error("\083\116\114\105\110\103\046\115\101\103\109\101\110\116\058 \096\105\110\112\117\116\096 \109\117\115\116 \098\101 \097 \115\116\114\105\110\103")end if type(e)~="\115\116\114\105\110\103"then error("\083\116\114\105\110\103\046\115\101\103\109\101\110\116\058 \096\100\101\108\105\109\105\116\101\114\096 \109\117\115\116 \098\101 \097 \115\116\114\105\110\103")end if type(Q)~="\110\117\109\098\101\114"then error("\083\116\114\105\110\103\046\115\101\103\109\101\110\116\058 \096\108\101\110\103\116\104\096 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")end local l=""for k=1,#c,1 do if k>1 and(k-1)%Q==0 then l=l..e end l=l..c:sub(k,k)end return l end return c
--------------------------------------------------------------------------------------------------------------------------------
--[[                                             Copyright (C) Inertia Lighting                                             ]]--
--------------------------------------------------------------------------------------------------------------------------------

local String = {}

--------------------------------------------------------------------------------------------------------------------------------

--- Repeats a string a specified number of times
--- @param str string
--- @param num number the number of times to repeat 'str' (default: 1)
--- @returns repeated string
function String.replicate(str, num)
    local repeated = str

    for _ = 1, num or 1, 1 do
        repeated = repeated .. str
    end

    return repeated
end

--- Reverses a string
--- @param str string
--- @returns reversed string
function String.reverse(str)
    local reversed = ''

    for index = #str, 1, -1 do
        reversed = reversed .. string.sub(str, index, index)
    end

    return reversed
end

--- Removes all whitespace from the beginning and end of a string
--- @param str string
--- @returns trimmed string
function String.trim(str)
    local trimmed = str:gsub("^%s*(.-)%s*$", "%1")

    return trimmed
end

--- Returns the character found at the specified index
--- @param str string
--- @param index number
--- @returns char string
function String.charAt(str, index)
    local char = string.sub(str, index, index)

    return char
end

--- Returns the character code of the character found at the specified index
--- @param str string
--- @param index number
--- @returns charCode number
function String.charCodeAt(str, index)
    local charCode = string.byte(string.sub(str, index, index))

    return charCode
end

--- Splits a string by a separator (example: ',') into a table containing substrings
--- @param str string
--- @param separator string
--- @returns split table
function String.split(str, separator)
    if #str == 0 or not separator then
        return { str }
    end

    local chars = {}
    for index = 1, #str do
        chars[#chars + 1] = string.sub(str, index, index)
    end

    if #separator == 0 then
        return chars
    end

    local currentSplitIndex = 1
    local split = {}

    local charsIndex = 1
    while charsIndex <= #chars do
        local charsSegment = ''

        local separatorIndex = 1
        while separatorIndex <= #separator do
            local char = chars[charsIndex + separatorIndex - 1]

            local currentSplitValue = split[currentSplitIndex] or ''

            if separatorIndex > 1 and not char then
                split[currentSplitIndex] = currentSplitValue .. charsSegment
                break
            end

            charsSegment = charsSegment .. char
            local separatorSegment = string.sub(separator, 1, separatorIndex)

            if charsSegment ~= separatorSegment then
                split[currentSplitIndex] = currentSplitValue .. char
                break
            end

            if charsSegment == separator then
                split[currentSplitIndex] = currentSplitValue
                currentSplitIndex = currentSplitIndex + 1
            end

            separatorIndex = separatorIndex + 1
        end

        if charsIndex + #charsSegment >= #chars and charsSegment == separator then
            split[#split + 1] = ''
        end

        charsIndex = charsIndex + #charsSegment
    end

    return split
end

--- Segments a string using a delimiter at specified intervals
--- @param input string
--- @param delimiter string
--- @param length number
--- @returns output string
function String.segment(input, delimiter, length)
    if type(input) ~= 'string' then
        error('String.segment: `input` must be a string')
    end

    if type(delimiter) ~= 'string' then
        error('String.segment: `delimiter` must be a string')
    end

    if type(length) ~= 'number' then
        error('String.segment: `length` must be a number')
    end

    local output = ''

    for i = 1, #input, 1 do
        if (i > 1) and ((i - 1) % length == 0) then
            output = output .. delimiter
        end

        output = output .. input:sub(i, i)
    end

    return output
end

--------------------------------------------------------------------------------------------------------------------------------

return String
