Pioneer
ByteRange.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _BYTERANGE_H
5 #define _BYTERANGE_H
6 
7 #include <algorithm>
8 #include <cassert>
9 #include <cctype>
10 #include <cstring>
11 
12 struct ByteRange {
14  begin(0),
15  end(0) {}
16  ByteRange(const char *begin_, const char *end_) :
17  begin(begin_),
18  end(end_)
19  {
20  assert(begin_ && end_);
21  assert((end_ - begin_) >= 0);
22  }
23  ByteRange(const char *begin_, size_t size) :
24  begin(begin_),
25  end(begin_ + size)
26  {
27  assert(begin_);
28  }
29 
30  const char *begin;
31  const char *end;
32 
33  bool Empty() const { return (begin == end); }
34  size_t Size() const { return (end - begin); }
35 
36  const char &operator[](size_t idx) const { return begin[idx]; }
37  const char &operator*() const { return *begin; }
38 
39  size_t read(char *to, size_t sz, size_t count);
40 };
41 
42 inline size_t ByteRange::read(char *to, size_t sz, size_t count)
43 {
44  size_t avail = Size() / sz;
45  count = std::min(count, avail);
46  size_t fullsize = sz * count;
47  assert(fullsize <= Size());
48  memcpy(to, begin, fullsize);
49  begin += fullsize;
50  return count;
51 }
52 
53 #endif
Definition: ByteRange.h:12
ByteRange(const char *begin_, size_t size)
Definition: ByteRange.h:23
const char & operator*() const
Definition: ByteRange.h:37
size_t Size() const
Definition: ByteRange.h:34
size_t read(char *to, size_t sz, size_t count)
Definition: ByteRange.h:42
ByteRange(const char *begin_, const char *end_)
Definition: ByteRange.h:16
bool Empty() const
Definition: ByteRange.h:33
const char * end
Definition: ByteRange.h:31
ByteRange()
Definition: ByteRange.h:13
const char * begin
Definition: ByteRange.h:30
const char & operator[](size_t idx) const
Definition: ByteRange.h:36