ReSub Compression
Key Required VerifiedUniversal

ReSub Compression

Universal Scriptvxstyvxsty 338.1K views 184 executesUpdated Oct 1, 2024
1.2/ 5(26 votes)
62023% Positive
Lua Script•124 lines (2701 chars)
-- ReSub Compression

local Map = {}
-- Important Numbers
local CurrentIndex = 0

-- Main Code

local function Compress(Content)
	local Output = {}
	local Variables = {}
	local ContentLength = #Content
	local Prefix = [[]];
	
	local function Legalize(Text)
		for _, Char in next, {"$", "%", "^", "&", "*", "(", ")", "-", "+", "?", ".", "[", "]"} do
			Text = string.gsub(Text, "%" .. Char, "%%" .. Char)
		end
		
		return Text;
	end
	
	local function Normalize(Text)
		return string.gsub(Text, "%%", "")
	end

	local function Replace(longestSubstring)
		local count = 0
		local UpdatedContent = Content

		for _ in string.gmatch(UpdatedContent, longestSubstring) do
			count = count + 1
		end

		if count > 1 then
			UpdatedContent = string.gsub(UpdatedContent, longestSubstring, "b" .. CurrentIndex)
			table.insert(Variables, {CurrentIndex, Normalize(longestSubstring)})
			CurrentIndex = CurrentIndex + 1
		end

		return UpdatedContent
	end

	for length = ContentLength, 1, -1 do
		for start = 1, ContentLength - length + 1 do
			local substring = string.sub(Content, start, start + length - 1)
			local LegalSubstring = Legalize(substring);

			local count = 0
			for _ in string.gmatch(Content, LegalSubstring) do
				count = count + 1
			end

			if count > 1 and #substring > (CurrentIndex + 1) then
				Content = Replace(LegalSubstring)
			end
		end
	end
	
	table.sort(Variables, function(a, b)
		return a[1] < b[1] -- Makes it so the b%d indexing is ascending not descending or random
	end)
	
	for _, Info in next, Variables do
		Prefix ..= "b" .. tostring(Info[1]) .. "=" .. Info[2] .. ";\n"
	end

	
	return "--p" .. Prefix .. "\n--P\n" .. Content;
end

local function Decompress(Content)
	local Variables = {};
	-- Match the variables
	
	local LastSub = [[]];
	local ContentV2 = Content;
	
	for i = 1, #Content do
		local Part = string.sub(Content, i, i)
		if Part == ";" then
			local v1, v2 = string.match(LastSub, "b(%d+)=(.+)")
			
			if v1 and v2 then
				table.insert(Variables, {v1, v2})
				LastSub = [[]];
			end
		else
			LastSub = LastSub .. Part;
		end
	end
	
	for _, Info in next, Variables do
		ContentV2 = string.gsub(ContentV2, "b(%d+)", function(Index)
			for _, Info2 in next, Variables do
				if Info2[1] == Index then
					return Info2[2]
				end
			end
		end)
	end
	
	ContentV2 = string.gsub(ContentV2, "(.+)=(.+);\n?", function(a, b)
	 local A = string.gsub(a, "\n", "")
	 local B = string.gsub(b, "\n", "")
	 
	 A = string.split(A, ";")[1]
	 
	 local C = string.split(A, "=")
	 
	 if C[1] == C[2] then
	  return "";
	 end
	end)
	
	ContentV2 = string.gsub(ContentV2, "--p.+--P\n", "")
	
	return ContentV2;
end

return {
	Compress = Compress,
	Decompress = Decompress
};

Features & Information

ReSub Algorithm is a project I've worked on for fun, While I was bored I decided to make something that is slightly helpful for some people. FAQ: What is ReSub? ReSub is a text compression algorithm that finds duplicate substrings used in a file and replaces those with symbols (b%d) Why use ReSub? No real reason to use it other than reducing file sizes by up to 20%. ReSub is gonna be maintained by ME & ONLY ME. Currently in beta, please report any bugs to my dms (libary.hello or ballsman3761 on discord) Known bugs: Substrings that have ; might break the script, Substrings with b%d=... might also break the script.
#Compression#Advanced#Luau#Verified#Algorithm

Community Comments (4)

galaxyfoundedgalaxyfoundedOct 1, 2024(edited)

Good

KagasadKagasadOct 1, 2024(edited)

What the hell I don't understand at all because I'm not scripter 🪦

WhatsHisIdentifierWhatsHisIdentifierOct 2, 2024(edited)

@Kagaforgotpassword ReSub is a compression algorithm (obvious). It looks for repeated substrings (substrings are smaller strings that came from a large string) in your code, and compresses the text by replacing duplicate substrings with b%d, where %d is an increasing index.

KagasadKagasadOct 2, 2024(edited)

@MaybeJohnnyLol Okay I will try to understand it

More Scripts for Universal Scripts