Re: Censor

From Crys, 3 Weeks ago, written in Lua, viewed 18 times. This paste is a reply to Re: Censor from Crys - view diff
URL https://paste.godclan.hu/view/edc38a5e Embed
Download Paste or View Raw
  1. do
  2.   local words = {
  3.     "fasz",
  4.     "faSzszopó",
  5.     "kurva",
  6.     "geci",
  7.     "anyád",
  8.     "köcsög"
  9.   }
  10.  
  11.   local stringImpl = utf8 or string
  12.   local REPLACEMENT_CHAR = "*"
  13.  
  14.   function censor(input)
  15.     if type(input) ~= "string" then
  16.       return input
  17.     end
  18.    
  19.     local output = input
  20.     local haystack = stringImpl.lower(input)
  21.    
  22.     for _, word in ipairs(words) do
  23.       word = stringImpl.lower(word)
  24.      
  25.       local replacement = string.rep(REPLACEMENT_CHAR, stringImpl.len(word))
  26.       local start = 1
  27.          
  28.       while true do
  29.         local i, j = stringImpl.find(haystack, word, start, true)
  30.  
  31.         if i == nil then
  32.           break
  33.         end
  34.        
  35.         start = j + 1
  36.  
  37.         output = stringImpl.sub(output, 1, i - 1) .. replacement .. stringImpl.sub(output, start)
  38.       end
  39.     end
  40.  
  41.     return output
  42.   end
  43. end
  44.  
  45. addEventHandler("onClientChatMessage", root, function (text, r, g, b)
  46.   local censored = censor(text)
  47.  
  48.   if censored ~= text then
  49.     cancelEvent()
  50.     outputChatBox(text, root, r, g, b, true)
  51.   end
  52. end)

Reply to "Re: Censor"

Here you can reply to the paste above