Created
March 5, 2018 23:21
-
-
Save ConorOBrien-Foxx/8d49ed0c01f95fb9a7b27187920eb97e to your computer and use it in GitHub Desktop.
TIO encoding
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
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