Skip to content

Instantly share code, notes, and snippets.

@ConorOBrien-Foxx
Created March 5, 2018 23:21
Show Gist options
  • Save ConorOBrien-Foxx/8d49ed0c01f95fb9a7b27187920eb97e to your computer and use it in GitHub Desktop.
Save ConorOBrien-Foxx/8d49ed0c01f95fb9a7b27187920eb97e to your computer and use it in GitHub Desktop.
TIO encoding
require 'zlib'
require 'base64'
FIELD_SEPARATOR = "\xff"
def deflate(str)
Zlib::Deflate.deflate(str,9)[2..-5]
end
def finalize(state_string)
compressed = deflate(state_string)
encoded = Base64.encode64 compressed
encoded.tr("+", "@").gsub(/=+/, "")
end
def tio_encode(program)
state_string = "attache#{FIELD_SEPARATOR * 2}#{program}#{FIELD_SEPARATOR * 2}"
url = finalize state_string
"tio.run/###{url}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment