Created
January 4, 2019 10:53
-
-
Save gfldex/a88776b16929d8817dba41cc646762b8 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
use v6.d; | |
use MONKEY-TYPING; | |
class HTML is Str { | |
our sub escape(Str() \verbatim) { | |
verbatim.subst('&', '&', :g).subst('<', '<', :g).subst('>', '>', :g).subst('"', '"', :g).subst("'", ''', :g) | |
#verbatim.split('<', :v).hyper(:degree(16)).map({ .subst('&', '&', :g).subst('<', '<', :g).subst('>', '>', :g).subst('"', '"', :g).subst("'", ''', :g)}).join | |
} | |
our sub unescape(Str() \escaped --> Str) { | |
escaped.subst(''', "'", :g).subst('"', '"', :g).subst('>', '>', :g).subst('<', '<', :g).subst('&', '&', :g); | |
} | |
} | |
augment class Cool { | |
method HTML { HTML::escape(self) } | |
} | |
Int.HOW.compose(Int); | |
Num.HOW.compose(Num); | |
Str.HOW.compose(Str); | |
Complex.HOW.compose(Complex); | |
my $v = '<a>&42</a>' x 100000; | |
say $v.chars; | |
put HTML::unescape($v.HTML).chars; | |
say now - BEGIN now; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment