Created
August 16, 2015 17:39
-
-
Save badcc/f6f3ad0682dbfb786df6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Message = "This is a very powerful function and can be used in multiple ways. Used simply it can replace all instances of the pattern provided with the replacement. A pair of values is returned, the modified string and the number of substitutions made. The optional fourth argument n can be used to limit the number of substitutions made:" | |
local Methods = { | |
{ "%s+", " " }, | |
{ "are", "r" }, | |
{ "be", "b" }, | |
{ "you", "u" }, | |
{ "i%sam", "i'm" }, | |
{ "first", "1st" }, | |
{ "second", "2nd" }, | |
{ "third", "3rd" }, | |
{ "fourth", "4th" }, | |
{ "fifth", "5th" }, | |
{ "sixth", "6th" }, | |
{ "seventh", "7th" }, | |
{ "eigth", "8th" }, | |
{ "ninth", "9th" }, | |
{ "tenth", "10th" }, | |
{ "one", "1" }, | |
{ "two", "2" }, | |
{ "three", "3" }, | |
{ "four", "4" }, | |
{ "five", "5" }, | |
{ "six", "6" }, | |
{ "seven", "7" }, | |
{ "eight", "8" }, | |
{ "nine", "9" }, | |
{ "ten", "10" }, | |
{ "next", "nxt" }, | |
{ "i'm", "im" }, | |
{ ", but", ";" }, | |
{ ", and", ";" }, | |
{ ", or", ";" }, | |
{ "(%p)%s*", "%1" }, | |
{ "%sa%s", " "}, | |
{ "%san%s", " "}, | |
{ "%sthe%s", " "} | |
} | |
local function Compress(Message) | |
Message = Message:lower() | |
local OriginalLength = #Message | |
local MaxAttempt = #Methods | |
local Attempt = 0 | |
while (#Message > 140 and Attempt < MaxAttempt) do | |
Attempt = Attempt + 1 | |
Message = Message:gsub(Methods[Attempt][1], Methods[Attempt][2]) | |
wait() | |
end | |
local NewLength = #Message | |
print('Shortened by', OriginalLength - NewLength, 'characters.') | |
print('Now', NewLength) | |
print(Message) | |
end | |
Compress(Message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment