Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created January 4, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfldex/a88776b16929d8817dba41cc646762b8 to your computer and use it in GitHub Desktop.
Save gfldex/a88776b16929d8817dba41cc646762b8 to your computer and use it in GitHub Desktop.
use v6.d;
use MONKEY-TYPING;
class HTML is Str {
our sub escape(Str() \verbatim) {
verbatim.subst('&', '&amp;', :g).subst('<', '&lt;', :g).subst('>', '&gt;', :g).subst('"', '&quot;', :g).subst("'", '&#39;', :g)
#verbatim.split('<', :v).hyper(:degree(16)).map({ .subst('&', '&amp;', :g).subst('<', '&lt;', :g).subst('>', '&gt;', :g).subst('"', '&quot;', :g).subst("'", '&#39;', :g)}).join
}
our sub unescape(Str() \escaped --> Str) {
escaped.subst('&#39;', "'", :g).subst('&quot;', '"', :g).subst('&gt;', '>', :g).subst('&lt;', '<', :g).subst('&amp;', '&', :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