ZeroTierOne/node/Str.hpp

178 lines
4.7 KiB
C++
Raw Normal View History

2019-08-08 20:03:52 +00:00
/*
* Copyright (c)2019 ZeroTier, Inc.
2019-08-08 20:03:52 +00:00
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
2019-08-08 20:03:52 +00:00
*
* Change Date: 2023-01-01
2019-08-08 20:03:52 +00:00
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
2019-08-08 20:03:52 +00:00
*/
/****/
2019-08-08 20:03:52 +00:00
#ifndef ZT_STR_HPP
#define ZT_STR_HPP
#include "Constants.hpp"
#include "Utils.hpp"
#include "Address.hpp"
#include "MAC.hpp"
#include "InetAddress.hpp"
#include <string>
2019-08-08 20:03:52 +00:00
#define ZT_STR_CAPACITY 254
namespace ZeroTier {
2019-08-14 19:48:45 +00:00
/**
* A short non-allocating replacement for std::string
*/
2019-08-08 20:03:52 +00:00
class Str
{
public:
2019-08-14 19:48:45 +00:00
typedef char * iterator;
typedef const char * const_iterator;
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str() { _l = 0; _s[0] = 0; }
ZT_ALWAYS_INLINE Str(const Str &s)
2019-08-08 20:03:52 +00:00
{
_l = s._l;
memcpy(_s,s._s,_l+1);
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str(const char *s)
2019-08-08 20:03:52 +00:00
{
_l = 0;
_s[0] = 0;
(*this) << s;
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str(const std::string &s)
{
*this = s;
}
2019-08-08 20:03:52 +00:00
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator=(const Str &s)
2019-08-08 20:03:52 +00:00
{
_l = s._l;
memcpy(_s,s._s,_l+1);
return *this;
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator=(const char *s)
2019-08-08 20:03:52 +00:00
{
_l = 0;
_s[0] = 0;
return ((*this) << s);
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator=(const std::string &s)
{
if (s.length() > ZT_STR_CAPACITY) {
_l = 0;
_s[0] = 0;
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
} else {
_l = (uint8_t)s.length();
memcpy(_s,s.data(),s.length());
_s[s.length()] = 0;
}
return *this;
}
2019-08-08 20:03:52 +00:00
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE char operator[](const unsigned int i) const
2019-08-08 20:03:52 +00:00
{
if (unlikely(i >= (unsigned int)_l))
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
return _s[i];
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE void clear() { _l = 0; _s[0] = 0; }
ZT_ALWAYS_INLINE const char *c_str() const { return _s; }
ZT_ALWAYS_INLINE unsigned int length() const { return (unsigned int)_l; }
ZT_ALWAYS_INLINE bool empty() const { return (_l == 0); }
ZT_ALWAYS_INLINE iterator begin() { return (iterator)_s; }
ZT_ALWAYS_INLINE iterator end() { return (iterator)(_s + (unsigned long)_l); }
ZT_ALWAYS_INLINE const_iterator begin() const { return (const_iterator)_s; }
ZT_ALWAYS_INLINE const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
2019-08-08 20:03:52 +00:00
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const char *s)
2019-08-08 20:03:52 +00:00
{
if (likely(s != (const char *)0)) {
unsigned long l = _l;
while (*s) {
if (unlikely(l >= ZT_STR_CAPACITY)) {
2019-08-14 19:48:45 +00:00
_s[ZT_STR_CAPACITY] = 0;
_l = ZT_STR_CAPACITY;
2019-08-08 20:03:52 +00:00
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
}
_s[l++] = *s;
2019-08-19 22:43:15 +00:00
++s;
2019-08-08 20:03:52 +00:00
}
_s[l] = 0;
_l = (uint8_t)l;
}
return *this;
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const Str &s) { return ((*this) << s._s); }
ZT_ALWAYS_INLINE Str &operator<<(const char c)
2019-08-08 20:03:52 +00:00
{
2019-08-14 19:48:45 +00:00
if (unlikely(_l >= ZT_STR_CAPACITY)) {
_s[ZT_STR_CAPACITY] = 0;
throw ZT_EXCEPTION_OUT_OF_BOUNDS;
2019-08-08 20:03:52 +00:00
}
2019-08-14 19:48:45 +00:00
_s[(unsigned long)(_l++)] = c;
_s[(unsigned long)_l] = 0;
return *this;
2019-08-08 20:03:52 +00:00
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const unsigned long n)
2019-08-08 20:03:52 +00:00
{
char tmp[32];
Utils::decimal(n,tmp);
return ((*this) << tmp);
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const unsigned int n)
2019-08-08 20:03:52 +00:00
{
char tmp[32];
Utils::decimal((unsigned long)n,tmp);
return ((*this) << tmp);
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const Address &a)
2019-08-08 20:03:52 +00:00
{
char tmp[32];
return ((*this) << a.toString(tmp));
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const InetAddress &a)
2019-08-08 20:03:52 +00:00
{
char tmp[128];
return ((*this) << a.toString(tmp));
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE Str &operator<<(const MAC &a)
2019-08-08 20:03:52 +00:00
{
char tmp[64];
return ((*this) << a.toString(tmp));
}
2019-08-27 03:18:28 +00:00
ZT_ALWAYS_INLINE operator bool() const { return (_l != 0); }
ZT_ALWAYS_INLINE bool operator==(const Str &s) const { return ((_l == s._l)&&(strcmp(_s,s._s) == 0)); }
ZT_ALWAYS_INLINE bool operator!=(const Str &s) const { return ((_l != s._l)||(strcmp(_s,s._s) != 0)); }
ZT_ALWAYS_INLINE bool operator<(const Str &s) const { return ((_l < s._l)&&(strcmp(_s,s._s) < 0)); }
ZT_ALWAYS_INLINE bool operator>(const Str &s) const { return ((_l > s._l)&&(strcmp(_s,s._s) > 0)); }
ZT_ALWAYS_INLINE bool operator<=(const Str &s) const { return ((_l <= s._l)&&(strcmp(_s,s._s) <= 0)); }
ZT_ALWAYS_INLINE bool operator>=(const Str &s) const { return ((_l >= s._l)&&(strcmp(_s,s._s) >= 0)); }
ZT_ALWAYS_INLINE bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
ZT_ALWAYS_INLINE bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
ZT_ALWAYS_INLINE bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
ZT_ALWAYS_INLINE bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
ZT_ALWAYS_INLINE bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
ZT_ALWAYS_INLINE bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
2019-08-08 20:03:52 +00:00
private:
uint8_t _l;
char _s[ZT_STR_CAPACITY+1];
};
} // namespace ZeroTier
#endif