Minor cleanup.

This commit is contained in:
Adam Ierymenko 2017-07-17 14:21:09 -07:00
parent ab0806a036
commit b9e1d53d7a
33 changed files with 154 additions and 193 deletions

View file

@ -95,7 +95,7 @@ public:
Buffer(unsigned int l)
{
if (l > C)
throw std::out_of_range("Buffer: construct with size larger than capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
_l = l;
}
@ -119,7 +119,7 @@ public:
inline Buffer &operator=(const Buffer<C2> &b)
{
if (unlikely(b._l > C))
throw std::out_of_range("Buffer: assignment from buffer larger than capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
if (C2 == C) {
memcpy(this,&b,sizeof(Buffer<C>));
} else {
@ -131,7 +131,7 @@ public:
inline void copyFrom(const void *b,unsigned int l)
{
if (unlikely(l > C))
throw std::out_of_range("Buffer: set from C array larger than capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
memcpy(_b,b,l);
_l = l;
}
@ -139,14 +139,14 @@ public:
unsigned char operator[](const unsigned int i) const
{
if (unlikely(i >= _l))
throw std::out_of_range("Buffer: [] beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
return (unsigned char)_b[i];
}
unsigned char &operator[](const unsigned int i)
{
if (unlikely(i >= _l))
throw std::out_of_range("Buffer: [] beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
return ((unsigned char *)_b)[i];
}
@ -166,13 +166,13 @@ public:
unsigned char *field(unsigned int i,unsigned int l)
{
if (unlikely((i + l) > _l))
throw std::out_of_range("Buffer: field() beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
return (unsigned char *)(_b + i);
}
const unsigned char *field(unsigned int i,unsigned int l) const
{
if (unlikely((i + l) > _l))
throw std::out_of_range("Buffer: field() beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
return (const unsigned char *)(_b + i);
}
@ -187,7 +187,7 @@ public:
inline void setAt(unsigned int i,const T v)
{
if (unlikely((i + sizeof(T)) > _l))
throw std::out_of_range("Buffer: setAt() beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
#ifdef ZT_NO_TYPE_PUNNING
uint8_t *p = reinterpret_cast<uint8_t *>(_b + i);
for(unsigned int x=1;x<=sizeof(T);++x)
@ -209,7 +209,7 @@ public:
inline T at(unsigned int i) const
{
if (unlikely((i + sizeof(T)) > _l))
throw std::out_of_range("Buffer: at() beyond end of data");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
#ifdef ZT_NO_TYPE_PUNNING
T v = 0;
const uint8_t *p = reinterpret_cast<const uint8_t *>(_b + i);
@ -235,7 +235,7 @@ public:
inline void append(const T v)
{
if (unlikely((_l + sizeof(T)) > C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
#ifdef ZT_NO_TYPE_PUNNING
uint8_t *p = reinterpret_cast<uint8_t *>(_b + _l);
for(unsigned int x=1;x<=sizeof(T);++x)
@ -257,7 +257,7 @@ public:
inline void append(unsigned char c,unsigned int n)
{
if (unlikely((_l + n) > C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
for(unsigned int i=0;i<n;++i)
_b[_l++] = (char)c;
}
@ -270,7 +270,7 @@ public:
inline void appendRandom(unsigned int n)
{
if (unlikely((_l + n) > C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
Utils::getSecureRandom(_b + _l,n);
_l += n;
}
@ -285,7 +285,7 @@ public:
inline void append(const void *b,unsigned int l)
{
if (unlikely((_l + l) > C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
memcpy(_b + _l,b,l);
_l += l;
}
@ -311,7 +311,7 @@ public:
{
for(;;) {
if (unlikely(_l >= C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
if (!(_b[_l++] = *(s++)))
break;
}
@ -343,7 +343,7 @@ public:
inline char *appendField(unsigned int l)
{
if (unlikely((_l + l) > C))
throw std::out_of_range("Buffer: append beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
char *r = _b + _l;
_l += l;
return r;
@ -360,7 +360,7 @@ public:
inline void addSize(unsigned int i)
{
if (unlikely((i + _l) > C))
throw std::out_of_range("Buffer: setSize to larger than capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
_l += i;
}
@ -375,7 +375,7 @@ public:
inline void setSize(const unsigned int i)
{
if (unlikely(i > C))
throw std::out_of_range("Buffer: setSize to larger than capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
_l = i;
}
@ -383,14 +383,14 @@ public:
* Move everything after 'at' to the buffer's front and truncate
*
* @param at Truncate before this position
* @throw std::out_of_range Position is beyond size of buffer
* @throws std::out_of_range Position is beyond size of buffer
*/
inline void behead(const unsigned int at)
{
if (!at)
return;
if (unlikely(at > _l))
throw std::out_of_range("Buffer: behead() beyond capacity");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
::memmove(_b,_b + at,_l -= at);
}
@ -399,13 +399,13 @@ public:
*
* @param start Starting position
* @param length Length of block to erase
* @throw std::out_of_range Position plus length is beyond size of buffer
* @throws std::out_of_range Position plus length is beyond size of buffer
*/
inline void erase(const unsigned int at,const unsigned int length)
{
const unsigned int endr = at + length;
if (unlikely(endr > _l))
throw std::out_of_range("Buffer: erase() range beyond end of buffer");
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
::memmove(_b + at,_b + endr,_l - endr);
_l -= length;
}