Str.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_STR_HPP
  14. #define ZT_STR_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "Address.hpp"
  18. #include "MAC.hpp"
  19. #include "InetAddress.hpp"
  20. #include <string>
  21. #define ZT_STR_CAPACITY 254
  22. namespace ZeroTier {
  23. /**
  24. * A short non-allocating replacement for std::string
  25. */
  26. class Str
  27. {
  28. public:
  29. typedef char * iterator;
  30. typedef const char * const_iterator;
  31. ZT_ALWAYS_INLINE Str() { _l = 0; _s[0] = 0; }
  32. ZT_ALWAYS_INLINE Str(const Str &s)
  33. {
  34. _l = s._l;
  35. memcpy(_s,s._s,_l+1);
  36. }
  37. ZT_ALWAYS_INLINE Str(const char *s)
  38. {
  39. _l = 0;
  40. _s[0] = 0;
  41. (*this) << s;
  42. }
  43. ZT_ALWAYS_INLINE Str(const std::string &s)
  44. {
  45. *this = s;
  46. }
  47. ZT_ALWAYS_INLINE Str &operator=(const Str &s)
  48. {
  49. _l = s._l;
  50. memcpy(_s,s._s,_l+1);
  51. return *this;
  52. }
  53. ZT_ALWAYS_INLINE Str &operator=(const char *s)
  54. {
  55. _l = 0;
  56. _s[0] = 0;
  57. return ((*this) << s);
  58. }
  59. ZT_ALWAYS_INLINE Str &operator=(const std::string &s)
  60. {
  61. if (s.length() > ZT_STR_CAPACITY) {
  62. _l = 0;
  63. _s[0] = 0;
  64. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  65. } else {
  66. _l = (uint8_t)s.length();
  67. memcpy(_s,s.data(),s.length());
  68. _s[s.length()] = 0;
  69. }
  70. return *this;
  71. }
  72. ZT_ALWAYS_INLINE char operator[](const unsigned int i) const
  73. {
  74. if (unlikely(i >= (unsigned int)_l))
  75. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  76. return _s[i];
  77. }
  78. ZT_ALWAYS_INLINE void clear() { _l = 0; _s[0] = 0; }
  79. ZT_ALWAYS_INLINE const char *c_str() const { return _s; }
  80. ZT_ALWAYS_INLINE unsigned int length() const { return (unsigned int)_l; }
  81. ZT_ALWAYS_INLINE bool empty() const { return (_l == 0); }
  82. ZT_ALWAYS_INLINE iterator begin() { return (iterator)_s; }
  83. ZT_ALWAYS_INLINE iterator end() { return (iterator)(_s + (unsigned long)_l); }
  84. ZT_ALWAYS_INLINE const_iterator begin() const { return (const_iterator)_s; }
  85. ZT_ALWAYS_INLINE const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
  86. ZT_ALWAYS_INLINE Str &operator<<(const char *s)
  87. {
  88. if (likely(s != (const char *)0)) {
  89. unsigned long l = _l;
  90. while (*s) {
  91. if (unlikely(l >= ZT_STR_CAPACITY)) {
  92. _s[ZT_STR_CAPACITY] = 0;
  93. _l = ZT_STR_CAPACITY;
  94. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  95. }
  96. _s[l++] = *s;
  97. ++s;
  98. }
  99. _s[l] = 0;
  100. _l = (uint8_t)l;
  101. }
  102. return *this;
  103. }
  104. ZT_ALWAYS_INLINE Str &operator<<(const Str &s) { return ((*this) << s._s); }
  105. ZT_ALWAYS_INLINE Str &operator<<(const char c)
  106. {
  107. if (unlikely(_l >= ZT_STR_CAPACITY)) {
  108. _s[ZT_STR_CAPACITY] = 0;
  109. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  110. }
  111. _s[(unsigned long)(_l++)] = c;
  112. _s[(unsigned long)_l] = 0;
  113. return *this;
  114. }
  115. ZT_ALWAYS_INLINE Str &operator<<(const unsigned long n)
  116. {
  117. char tmp[32];
  118. Utils::decimal(n,tmp);
  119. return ((*this) << tmp);
  120. }
  121. ZT_ALWAYS_INLINE Str &operator<<(const unsigned int n)
  122. {
  123. char tmp[32];
  124. Utils::decimal((unsigned long)n,tmp);
  125. return ((*this) << tmp);
  126. }
  127. ZT_ALWAYS_INLINE Str &operator<<(const Address &a)
  128. {
  129. char tmp[32];
  130. return ((*this) << a.toString(tmp));
  131. }
  132. ZT_ALWAYS_INLINE Str &operator<<(const InetAddress &a)
  133. {
  134. char tmp[128];
  135. return ((*this) << a.toString(tmp));
  136. }
  137. ZT_ALWAYS_INLINE Str &operator<<(const MAC &a)
  138. {
  139. char tmp[64];
  140. return ((*this) << a.toString(tmp));
  141. }
  142. ZT_ALWAYS_INLINE operator bool() const { return (_l != 0); }
  143. ZT_ALWAYS_INLINE bool operator==(const Str &s) const { return ((_l == s._l)&&(strcmp(_s,s._s) == 0)); }
  144. ZT_ALWAYS_INLINE bool operator!=(const Str &s) const { return ((_l != s._l)||(strcmp(_s,s._s) != 0)); }
  145. ZT_ALWAYS_INLINE bool operator<(const Str &s) const { return ((_l < s._l)&&(strcmp(_s,s._s) < 0)); }
  146. ZT_ALWAYS_INLINE bool operator>(const Str &s) const { return ((_l > s._l)&&(strcmp(_s,s._s) > 0)); }
  147. ZT_ALWAYS_INLINE bool operator<=(const Str &s) const { return ((_l <= s._l)&&(strcmp(_s,s._s) <= 0)); }
  148. ZT_ALWAYS_INLINE bool operator>=(const Str &s) const { return ((_l >= s._l)&&(strcmp(_s,s._s) >= 0)); }
  149. ZT_ALWAYS_INLINE bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  150. ZT_ALWAYS_INLINE bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  151. ZT_ALWAYS_INLINE bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
  152. ZT_ALWAYS_INLINE bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
  153. ZT_ALWAYS_INLINE bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
  154. ZT_ALWAYS_INLINE bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
  155. private:
  156. uint8_t _l;
  157. char _s[ZT_STR_CAPACITY+1];
  158. };
  159. } // namespace ZeroTier
  160. #endif