RiSE4fun samples for BexList of built-in samples for the Bex in RiSE4funen-USrise4fun &copy; 2017 Microsoft Corporationhttp://rise4fun.com//Images/Rise.gifRiSE4fun samples for Bexhttp://rise4fun.com/Bex/htmldecodesimplehtmldecodesimple//helpers function D1(c) = ite(('0' <= c) && (c <= '9'), c -'0', 10 + ite(('A' <= c) && (c <= 'F'), c -'A', c - 'a')); function D2(b,x0,x1) = (b*D1(x0) + D1(x1)); program HtmlDecode(_){ replace { //"&amp;" ==> "&"; //"&lt;" ==> "<"; //"&gt;" ==> ">"; //"&quot;" ==> "\""; "&#[0-9];" ==> [D1(#2)]; "&#[0-9][0-9];" ==> [D2(10,#2,#3)]; else ==> [#0]; } } == js(HtmlDecode); display(minimize(dom(HtmlDecode)),100); //Type &#88; or &#99; to see the corresponding encoded resultshttp://rise4fun.com/Bex/htmlencodestricthtmlencodestrictfunction H(x) = ite((0 <= x) && (x <= 9), x + 48, x + 55); program HtmlEncode(_){ replace { "<" ==> "&lt;"; ">" ==> "&gt;"; "\x22"==> "&quot;"; "&" ==> "&amp;"; "\'" ==> "&apos;"; //encode all control and non ascii chars @"[\x01-\x0F]" ==> ['&','#','X', H(#0), ';']; //do not encode \0 @"[\x10-\x1F\x7F-\xFF]" ==> ['&','#','X', H(#0 >> 4), H(#0 & 0xF), ';']; @"[\u0100-\u0FFF]" ==> ['&','#','X', H(#0 >> 8), H((#0 >> 4) & 0xF), H(#0 & 0xF), ';']; @"[\u1000-\uFFFF]" ==> ['&','#','X', H(#0 >> 12), H((#0 >> 8) & 0xF), H((#0 >> 4) & 0xF), H(#0 & 0xF), ';']; else ==> [#0]; }} == js(HtmlEncode);http://rise4fun.com/Bex/base64base64/* function E and D maps x to the following base64-digit and viceversa 0..25 ==> 'A'..'Z' 26..51 ==> 'a'..'z' 52..61 ==> '0'..'9' 62 ==> '+' 63 ==> '/' */ function E(x)=(ite(x<=25,x+65,ite(x<=51,x+71,ite(x<=61,x-4,ite(x==62,'+','/'))))); function D(x)=(ite(x=='/',63,ite(x=='+',62,ite(x<='9',x+4,ite(x<='Z',x-65,x-71))))); // base64 encoder of an input of bytes, any value in the sequence > 0xFF raises an exception // for example base64encode("Man") = "TWFu" program base64encode(_){ replace { @"[\0-\xFF]{3}" ==> [E(#0>>2), E((Bits(1,0,#0)<<4)|Bits(7,4,#1)), E((Bits(3,0,#1)<<2)|Bits(7,6,#2)), E(Bits(5,0,#2))]; @"[\0-\xFF]{2}$" ==> [E(Bits(7,2,#0)), E((Bits(1,0,#0)<<4)|Bits(7,4,#1)), E(Bits(3,0,#1)<<2), '=']; @"[\0-\xFF]$" ==> [E(Bits(7,2,#0)), E(Bits(1,0,#0)<<4), '=', '=']; } } //base64 decoder program base64decode(_){ replace { "[a-zA-Z0-9+/]{4}" ==> [(D(#0)<<2)|bits(5,4,D(#1)), (bits(3,0,D(#1))<<4)|bits(5,2,D(#2)), (bits(1,0,D(#2))<<6)|D(#3)]; "[a-zA-Z0-9+/]{3}=$" ==> [(D(#0)<<2)|bits(5,4,D(#1)), (bits(3,0,D(#1))<<4)|bits(5,2,D(#2))]; "[a-zA-Z0-9+/]{2}==$"==> [(D(#0)<<2)|bits(5,4,D(#1))]; } } // Identity Encoder program ID(_){replace { "." ==> [#0];}} == BYTES = regex(@"^[\0-\xFF]*$"); //domain of sequences of bytes ID_BYTES = restrict(ID,BYTES); //identity over sequences of bytes ed = join(base64encode, base64decode); //composition, first encode then decode eq(ID_BYTES, ed); //check functional correctness