Advertisement
Guest User

Untitled

a guest
Sep 18th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.68 KB | None | 0 0
  1. import std.traits;
  2. import std.stdio;
  3.  
  4. static
  5. ubyte[] packVarint(T)( T value )
  6. if( isIntegral!T && isUnsigned!T )
  7. out( arr )
  8. {
  9.     writeln( "Inside of out contract: value=", value, " type=", typeid( typeof(value) ), " result=", arr );
  10. }
  11. body
  12. {
  13.     writeln( "Inside of body: value=", value, " type=", typeid( typeof(value) ) );
  14.    
  15.     ubyte[] res;
  16.    
  17.     immutable ubyte maximal = 0b_1000_0000;
  18.    
  19.     while( value >= maximal )
  20.     {
  21.         res ~= cast( ubyte )( value | maximal );
  22.         value >>= 7;
  23.     }
  24.    
  25.     import std.conv: to;
  26.     res ~= to!( ubyte )( value );
  27.    
  28.     return res;
  29. }
  30.  
  31. void main()
  32. {
  33.     ulong v = 300;
  34.     packVarint( v );
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement