Str.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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. namespace ZeroTier {
  21. /**
  22. * A short non-allocating replacement for std::string
  23. *
  24. * @tparam C Maximum capacity (default: 1021 to make total size 1024)
  25. */
  26. template<unsigned long C = 1021>
  27. class Str
  28. {
  29. public:
  30. typedef char * iterator;
  31. typedef const char * const_iterator;
  32. ZT_ALWAYS_INLINE Str() { _l = 0; _s[0] = 0; }
  33. ZT_ALWAYS_INLINE Str(const Str &s)
  34. {
  35. _l = s._l;
  36. memcpy(_s,s._s,_l+1);
  37. }
  38. ZT_ALWAYS_INLINE Str(const char *s)
  39. {
  40. _l = 0;
  41. _s[0] = 0;
  42. (*this) << s;
  43. }
  44. ZT_ALWAYS_INLINE Str(const std::string &s) { *this = s; }
  45. ZT_ALWAYS_INLINE Str &operator=(const Str &s)
  46. {
  47. _l = s._l;
  48. memcpy(_s,s._s,_l+1);
  49. return *this;
  50. }
  51. ZT_ALWAYS_INLINE Str &operator=(const char *s)
  52. {
  53. _l = 0;
  54. _s[0] = 0;
  55. return ((*this) << s);
  56. }
  57. ZT_ALWAYS_INLINE Str &operator=(const std::string &s)
  58. {
  59. if (s.length() > C) {
  60. _l = 0;
  61. _s[0] = 0;
  62. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  63. } else {
  64. _l = (uint8_t)s.length();
  65. memcpy(_s,s.data(),s.length());
  66. _s[s.length()] = 0;
  67. }
  68. return *this;
  69. }
  70. ZT_ALWAYS_INLINE char operator[](const unsigned int i) const
  71. {
  72. if (unlikely(i >= (unsigned int)_l))
  73. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  74. return _s[i];
  75. }
  76. ZT_ALWAYS_INLINE void clear() { _l = 0; _s[0] = 0; }
  77. ZT_ALWAYS_INLINE const char *c_str() const { return _s; }
  78. ZT_ALWAYS_INLINE unsigned int length() const { return (unsigned int)_l; }
  79. ZT_ALWAYS_INLINE bool empty() const { return (_l == 0); }
  80. ZT_ALWAYS_INLINE iterator begin() { return (iterator)_s; }
  81. ZT_ALWAYS_INLINE iterator end() { return (iterator)(_s + (unsigned long)_l); }
  82. ZT_ALWAYS_INLINE const_iterator begin() const { return (const_iterator)_s; }
  83. ZT_ALWAYS_INLINE const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
  84. ZT_ALWAYS_INLINE Str &operator<<(const char *s)
  85. {
  86. if (likely(s != (const char *)0)) {
  87. unsigned long l = _l;
  88. while (*s) {
  89. if (unlikely(l >= C)) {
  90. _s[C] = 0;
  91. _l = C;
  92. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  93. }
  94. _s[l++] = *(s++);
  95. }
  96. _s[l] = 0;
  97. _l = (uint8_t)l;
  98. }
  99. return *this;
  100. }
  101. ZT_ALWAYS_INLINE Str &operator<<(const Str &s) { return ((*this) << s._s); }
  102. ZT_ALWAYS_INLINE Str &operator<<(const char c)
  103. {
  104. if (unlikely(_l >= C)) {
  105. _s[C] = 0;
  106. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  107. }
  108. _s[(unsigned long)(_l++)] = c;
  109. _s[(unsigned long)_l] = 0;
  110. return *this;
  111. }
  112. ZT_ALWAYS_INLINE Str &operator<<(const unsigned long n)
  113. {
  114. char tmp[32];
  115. Utils::decimal(n,tmp);
  116. return ((*this) << tmp);
  117. }
  118. ZT_ALWAYS_INLINE Str &operator<<(const unsigned int n)
  119. {
  120. char tmp[32];
  121. Utils::decimal((unsigned long)n,tmp);
  122. return ((*this) << tmp);
  123. }
  124. ZT_ALWAYS_INLINE Str &operator<<(const Address &a)
  125. {
  126. char tmp[32];
  127. return ((*this) << a.toString(tmp));
  128. }
  129. ZT_ALWAYS_INLINE Str &operator<<(const InetAddress &a)
  130. {
  131. char tmp[128];
  132. return ((*this) << a.toString(tmp));
  133. }
  134. ZT_ALWAYS_INLINE Str &operator<<(const MAC &a)
  135. {
  136. char tmp[64];
  137. return ((*this) << a.toString(tmp));
  138. }
  139. inline Str &append(const char *s,const unsigned int max)
  140. {
  141. if (likely(s != (const char *)0)) {
  142. unsigned long l = _l;
  143. unsigned int c = 0;
  144. while (*s) {
  145. if (c++ >= max) break;
  146. if (unlikely(l >= C)) {
  147. _s[C] = 0;
  148. _l = C;
  149. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  150. }
  151. _s[l++] = *s;
  152. ++s;
  153. }
  154. _s[l] = 0;
  155. _l = (uint8_t)l;
  156. }
  157. return *this;
  158. }
  159. ZT_ALWAYS_INLINE operator bool() const { return (_l != 0); }
  160. ZT_ALWAYS_INLINE bool operator==(const Str &s) const { return ((_l == s._l)&&(memcmp(_s,s._s,_l) == 0)); }
  161. ZT_ALWAYS_INLINE bool operator!=(const Str &s) const { return ((_l != s._l)||(memcmp(_s,s._s,_l) != 0)); }
  162. ZT_ALWAYS_INLINE bool operator<(const Str &s) const { return ( (_l < s._l) ? true : ((_l == s._l) ? (memcmp(_s,s._s,_l) < 0) : false) ); }
  163. ZT_ALWAYS_INLINE bool operator>(const Str &s) const { return (s < *this); }
  164. ZT_ALWAYS_INLINE bool operator<=(const Str &s) const { return !(s < *this); }
  165. ZT_ALWAYS_INLINE bool operator>=(const Str &s) const { return !(*this < s); }
  166. ZT_ALWAYS_INLINE bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  167. ZT_ALWAYS_INLINE bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  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 unsigned long hashCode() const { return Utils::hashString(_s,_l); }
  173. private:
  174. uint16_t _l;
  175. char _s[C+1];
  176. };
  177. } // namespace ZeroTier
  178. #endif