Advertisement
InTesting

MoonSec JunkString Functions

Jun 27th, 2021 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --done LuaU
  2.  
  3. -- note: may not work with strings with some escape sequences
  4.  
  5. --variables
  6. local MoonSec = {}
  7. local StringTks = {
  8.     '\'',
  9.     '"',
  10.     {'[[',']]'}
  11. }
  12.  
  13. --functions
  14. -- gen
  15. function Raw(str)return str:gsub('.',{['['] = '%[';[']'] = '%]'})end
  16. function FlipTable(a)local b={}for c,d in next,a do b[d] = c end return b end
  17. function GetStringTk(a)for i,v in next,a do v = type(v)=='string'and{v,v}or v a[i]=v end return a end
  18.  
  19. -- Moonsec V2 (but it kinda broken rn so lol)
  20. function GetJunkStrings(sourcecode) -- string:source code
  21.     local Results = {}
  22.     local Check = {}
  23.  
  24.     for _,strtokens in next,StringTks do
  25.         local begin,end_ = unpack(strtokens)
  26.         --
  27.         local Pattern = '[^'.. Raw(begin) .. '+]#' .. Raw(begin:sub(1,#begin-1)) .. '%b' ..begin:sub(#begin).. end_:sub(1,1) .. Raw(end_:sub(2))
  28.  
  29.  
  30.         sourcecode:gsub(Pattern,function(phrase)
  31.             phrase = phrase:sub(3 + #begin,#phrase - #end_)
  32.             if Check[phrase]then return end
  33.             table.insert(Results,phrase)
  34.             Check[phrase] = true
  35.         end)
  36.     end
  37.  
  38.     return Results
  39. end
  40.  
  41. function CensorJunkStrings(sourcecode,replacementcharacter,IsCompress) -- string:source code, string:replacement character, Boolean-ish: compresses output text, overrides 2nd arg
  42.     replacementcharacter = replacementcharacter and replacementcharacter:sub(1,1)or' '
  43.  
  44.     local JunkStrings = FlipTable(MoonSec.GetJunkStrings(sourcecode))
  45.  
  46.     for str in next,JunkStrings do
  47.         local len = #str
  48.         JunkStrings[str] = replacementcharacter:rep(len)
  49.     end
  50.  
  51.     for _,strtokens in next,StringTks do
  52.         local begin,end_ = unpack(strtokens)
  53.         local Pattern = '[^'.. Raw(begin) .. '+]#' .. Raw(begin:sub(1,#begin-1)) .. '%b' ..begin:sub(#begin).. end_:sub(1,1) .. Raw(end_:sub(2))
  54.        
  55.        
  56.         sourcecode = sourcecode:gsub(Pattern,function(found)
  57.             local pre,suf = found:sub(1,2 + #begin), found:sub(#found - #end_ + 1)
  58.             local phrase = found:sub(3 + #begin,#found - #end_)
  59.            
  60.             return
  61.                 IsCompress and found:sub(1,1) .. #phrase or -- check if we compress
  62.                 JunkStrings[phrase]and pre .. JunkStrings[phrase] .. suf or -- implement junkstrings if phrase match
  63.                 phrase -- anything else
  64.         end)
  65.     end
  66.     return sourcecode
  67. end
  68.  
  69. --assignments
  70. StringTks = GetStringTk(StringTks)
  71.  
  72. MoonSec.GetJunkStrings = GetJunkStrings
  73. MoonSec.CensorJunkStrings = CensorJunkStrings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement