If you rewrite [uri_encode] so that it doesn't use the accumulator [acc] anymore, but rather does the natural concatenations with recursive calls, then you should see very efficient imperative code come out of the Ur/Web compiler, when the result of [uri_encode] is dropped directly into page output.

On 04/14/2016 07:35 AM, Artyom Shalkhakov wrote:
The code is as follows:
fun
uri_encode (s:string):string = let
   fun
   tohexstr i = let
     val low = i % 16
     val high = i / 16
     fun hexdigit i =
         case i of
           0 => "0" | 1 => "1" | 2 => "2" | 3 => "3" | 4 => "4" | 5 => "5" | 6 => "6" | 7 => "7" | 8 
=> "8" | 9 => "9"
         | 10 => "A" | 11 => "B" | 12 => "C" | 13 => "D" | 14 => "E" | 15 => "F"
         | _ => error <xml>tohexstr: invalid digit {[i]}</xml>
   in
     hexdigit high ^ hexdigit low
   end

   fun
   aux i n s acc =
   if i < n then let
       val c = strsub s i
     in
       (* NOTE: strcat seems to be QUITE inefficient here *)
       if isalnum c || Option.isSome (String.index ";,/?:@&=+$#" c) then
         aux (i+1) n s (strcat acc (str1 c))
       else
         aux (i+1) n s (strcat acc (strcat "%" (tohexstr (ord c))))
     end
   else acc
   val res = aux 0 (strlen s) s ""
in
   res
end

_______________________________________________
Ur mailing list
[email protected]
http://www.impredicative.com/cgi-bin/mailman/listinfo/ur

Reply via email to