Last active
August 25, 2016 20:03
-
-
Save hackervera/6494ca18457854f115d9563cd0863483 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "stringio" | |
substr1 = encode_str [255, 0, 0, 1, "h"] | |
substr2 = encode_str [0, 13, 24, 4, "ello"] | |
str = StringIO.new(substr1 + substr2) | |
read_directive(str) | |
def encode_str(str_tuple) | |
[128396, str_tuple].flatten.pack("UC4a*") | |
end | |
def read_directive(str) | |
return if str.eof? | |
val = str.read(4).unpack("U") | |
match = [128396] | |
case val | |
when match | |
puts "This is a string colouring" | |
puts "Red value: #{str.read(1).unpack("C")}" | |
puts "Green value: #{str.read(1).unpack("C")}" | |
puts "Blue value: #{str.read(1).unpack("C")}" | |
length = str.read(1).unpack("C").first | |
puts "Length of string #{length}" | |
puts "String is: #{str.read(length)}" | |
read_directive(str) | |
else | |
"fail: #{val} #{match}" | |
end | |
end |
This file contains hidden or 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
2.1.2 :408 > substr1 = encode_str [255, 0, 0, 1, "h"] | |
=> "\xF0\x9F\x96\x8C\xFF\x00\x00\x01h" | |
2.1.2 :409 > substr2 = encode_str [0, 13, 24, 4, "ello"] | |
=> "\xF0\x9F\x96\x8C\x00\r\x18\x04ello" | |
2.1.2 :410 > str = StringIO.new(substr1 + substr2) | |
=> #<StringIO:0x007fe2b11bd960> | |
2.1.2 :411 > read_directive(str) | |
This is a string colouring | |
Red value: [255] | |
Green value: [0] | |
Blue value: [0] | |
Length of string 1 | |
String is: h | |
This is a string colouring | |
Red value: [0] | |
Green value: [13] | |
Blue value: [24] | |
Length of string 4 | |
String is: ello | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment