Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.89 KB | None | 0 0
  1. private @safe struct BytesReader(A = const ubyte[])
  2. {
  3.     A arr;
  4.     size_t currIdx;
  5.  
  6.     this(A a)
  7.     {
  8.         arr = a;
  9.     }
  10.  
  11.     T* read(T)() @trusted
  12.     {
  13.         const incremented = currIdx + T.sizeof;
  14.  
  15.         // Malformed buffer?
  16.         if(incremented > arr.length)
  17.             throw new AnswerException(ExceptionType.FATAL_ERROR, null);
  18.  
  19.         auto ret = cast(T*) &arr[currIdx];
  20.  
  21.         currIdx = incremented;
  22.  
  23.         return ret;
  24.     }
  25.  
  26.     A readBuff(size_t len)
  27.     in(len >= 0) // <-- here -------------------------------------------------------------------
  28.     {
  29.         const incremented = currIdx + len;
  30.  
  31.         // Malformed buffer?
  32.         if(incremented > arr.length)
  33.             throw new AnswerException(ExceptionType.FATAL_ERROR, null);
  34.  
  35.         auto ret = arr[currIdx .. incremented];
  36.  
  37.         currIdx = incremented;
  38.  
  39.         return ret;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement