Str.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  98. _s[l] = 0;
  99. _l = (uint8_t)l;
  100. }
  101. return *this;
  102. }
  103. ZT_ALWAYS_INLINE Str &operator<<(const Str &s) { return ((*this) << s._s); }
  104. ZT_ALWAYS_INLINE Str &operator<<(const char c)
  105. {
  106. if (unlikely(_l >= ZT_STR_CAPACITY)) {
  107. _s[ZT_STR_CAPACITY] = 0;
  108. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  109. }
  110. _s[(unsigned long)(_l++)] = c;
  111. _s[(unsigned long)_l] = 0;
  112. return *this;
  113. }
  114. ZT_ALWAYS_INLINE Str &operator<<(const unsigned long n)
  115. {
  116. char tmp[32];
  117. Utils::decimal(n,tmp);
  118. return ((*this) << tmp);
  119. }
  120. ZT_ALWAYS_INLINE Str &operator<<(const unsigned int n)
  121. {
  122. char tmp[32];
  123. Utils::decimal((unsigned long)n,tmp);
  124. return ((*this) << tmp);
  125. }
  126. ZT_ALWAYS_INLINE Str &operator<<(const Address &a)
  127. {
  128. char tmp[32];
  129. return ((*this) << a.toString(tmp));
  130. }
  131. ZT_ALWAYS_INLINE Str &operator<<(const InetAddress &a)
  132. {
  133. char tmp[128];
  134. return ((*this) << a.toString(tmp));
  135. }
  136. ZT_ALWAYS_INLINE Str &operator<<(const MAC &a)
  137. {
  138. char tmp[64];
  139. return ((*this) << a.toString(tmp));
  140. }
  141. ZT_ALWAYS_INLINE Str &append(const char *s,const unsigned int max)
  142. {
  143. if (likely(s != (const char *)0)) {
  144. unsigned long l = _l;
  145. unsigned int c = 0;
  146. while (*s) {
  147. if (c++ >= max) break;
  148. if (unlikely(l >= ZT_STR_CAPACITY)) {
  149. _s[ZT_STR_CAPACITY] = 0;
  150. _l = ZT_STR_CAPACITY;
  151. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  152. }
  153. _s[l++] = *s;
  154. ++s;
  155. }
  156. _s[l] = 0;
  157. _l = (uint8_t)l;
  158. }
  159. return *this;
  160. }
  161. ZT_ALWAYS_INLINE operator bool() const { return (_l != 0); }
  162. ZT_ALWAYS_INLINE bool operator==(const Str &s) const { return ((_l == s._l)&&(memcmp(_s,s._s,_l) == 0)); }
  163. ZT_ALWAYS_INLINE bool operator!=(const Str &s) const { return ((_l != s._l)||(memcmp(_s,s._s,_l) != 0)); }
  164. ZT_ALWAYS_INLINE bool operator<(const Str &s) const { return ( (_l < s._l) ? true : ((_l == s._l) ? (memcmp(_s,s._s,_l) < 0) : false) ); }
  165. ZT_ALWAYS_INLINE bool operator>(const Str &s) const { return (s < *this); }
  166. ZT_ALWAYS_INLINE bool operator<=(const Str &s) const { return !(s < *this); }
  167. ZT_ALWAYS_INLINE bool operator>=(const Str &s) const { return !(*this < s); }
  168. ZT_ALWAYS_INLINE bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  169. ZT_ALWAYS_INLINE bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  170. ZT_ALWAYS_INLINE bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
  171. ZT_ALWAYS_INLINE bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
  172. ZT_ALWAYS_INLINE bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
  173. ZT_ALWAYS_INLINE bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
  174. ZT_ALWAYS_INLINE unsigned long hashCode() const
  175. {
  176. const char *p = _s;
  177. unsigned long h = 0;
  178. char c;
  179. while ((c = *(p++)))
  180. h = (31 * h) + (unsigned long)c;
  181. return h;
  182. }
  183. private:
  184. uint8_t _l;
  185. char _s[ZT_STR_CAPACITY+1];
  186. };
  187. } // namespace ZeroTier
  188. #endif