Skip to content

Instantly share code, notes, and snippets.

@jeffmccune
Created December 7, 2011 04:51
Show Gist options
  • Save jeffmccune/1441513 to your computer and use it in GitHub Desktop.
Save jeffmccune/1441513 to your computer and use it in GitHub Desktop.
Urge to kill rising...
class String
ZAML_ESCAPES = %w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \x08 \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f }
def escaped_for_zaml
gsub( /\x5C/u, "\\\\\\" ). # Demi-kludge for Maglev/rubinius; the regexp should be /\\/ but parsetree chokes on that.
gsub( /"/u, "\\\"" ).
gsub( /([\x00-\x1F])/u ) { |x| ZAML_ESCAPES[ x.unpack("C")[0] ] }.
gsub( /([\x80-\xFF])/n ) { |x| "\\x#{x.unpack("C")[0].to_s(16)}" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment