Str.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_STR_HPP
  27. #define ZT_STR_HPP
  28. #include "Constants.hpp"
  29. #include "Utils.hpp"
  30. #include "Address.hpp"
  31. #include "MAC.hpp"
  32. #include "InetAddress.hpp"
  33. #include <string>
  34. #define ZT_STR_CAPACITY 254
  35. namespace ZeroTier {
  36. /**
  37. * A short non-allocating replacement for std::string
  38. */
  39. class Str
  40. {
  41. public:
  42. typedef char * iterator;
  43. typedef const char * const_iterator;
  44. inline Str() { _l = 0; _s[0] = 0; }
  45. inline Str(const Str &s)
  46. {
  47. _l = s._l;
  48. memcpy(_s,s._s,_l+1);
  49. }
  50. inline Str(const char *s)
  51. {
  52. _l = 0;
  53. _s[0] = 0;
  54. (*this) << s;
  55. }
  56. inline Str(const std::string &s)
  57. {
  58. *this = s;
  59. }
  60. inline Str &operator=(const Str &s)
  61. {
  62. _l = s._l;
  63. memcpy(_s,s._s,_l+1);
  64. return *this;
  65. }
  66. inline Str &operator=(const char *s)
  67. {
  68. _l = 0;
  69. _s[0] = 0;
  70. return ((*this) << s);
  71. }
  72. inline Str &operator=(const std::string &s)
  73. {
  74. if (s.length() > ZT_STR_CAPACITY) {
  75. _l = 0;
  76. _s[0] = 0;
  77. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  78. } else {
  79. _l = (uint8_t)s.length();
  80. memcpy(_s,s.data(),s.length());
  81. _s[s.length()] = 0;
  82. }
  83. return *this;
  84. }
  85. inline char operator[](const unsigned int i) const
  86. {
  87. if (unlikely(i >= (unsigned int)_l))
  88. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  89. return _s[i];
  90. }
  91. inline void clear() { _l = 0; _s[0] = 0; }
  92. inline const char *c_str() const { return _s; }
  93. inline unsigned int length() const { return (unsigned int)_l; }
  94. inline bool empty() const { return (_l == 0); }
  95. inline iterator begin() { return (iterator)_s; }
  96. inline iterator end() { return (iterator)(_s + (unsigned long)_l); }
  97. inline const_iterator begin() const { return (const_iterator)_s; }
  98. inline const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
  99. inline Str &operator<<(const char *s)
  100. {
  101. if (likely(s != (const char *)0)) {
  102. unsigned long l = _l;
  103. while (*s) {
  104. if (unlikely(l >= ZT_STR_CAPACITY)) {
  105. _s[ZT_STR_CAPACITY] = 0;
  106. _l = ZT_STR_CAPACITY;
  107. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  108. }
  109. _s[l++] = *s;
  110. ++s;
  111. }
  112. _s[l] = 0;
  113. _l = (uint8_t)l;
  114. }
  115. return *this;
  116. }
  117. inline Str &operator<<(const Str &s) { return ((*this) << s._s); }
  118. inline Str &operator<<(const char c)
  119. {
  120. if (unlikely(_l >= ZT_STR_CAPACITY)) {
  121. _s[ZT_STR_CAPACITY] = 0;
  122. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  123. }
  124. _s[(unsigned long)(_l++)] = c;
  125. _s[(unsigned long)_l] = 0;
  126. return *this;
  127. }
  128. inline Str &operator<<(const unsigned long n)
  129. {
  130. char tmp[32];
  131. Utils::decimal(n,tmp);
  132. return ((*this) << tmp);
  133. }
  134. inline Str &operator<<(const unsigned int n)
  135. {
  136. char tmp[32];
  137. Utils::decimal((unsigned long)n,tmp);
  138. return ((*this) << tmp);
  139. }
  140. inline Str &operator<<(const Address &a)
  141. {
  142. char tmp[32];
  143. return ((*this) << a.toString(tmp));
  144. }
  145. inline Str &operator<<(const InetAddress &a)
  146. {
  147. char tmp[128];
  148. return ((*this) << a.toString(tmp));
  149. }
  150. inline Str &operator<<(const MAC &a)
  151. {
  152. char tmp[64];
  153. return ((*this) << a.toString(tmp));
  154. }
  155. inline operator bool() const { return (_l != 0); }
  156. inline bool operator==(const Str &s) const { return ((_l == s._l)&&(strcmp(_s,s._s) == 0)); }
  157. inline bool operator!=(const Str &s) const { return ((_l != s._l)||(strcmp(_s,s._s) != 0)); }
  158. inline bool operator<(const Str &s) const { return ((_l < s._l)&&(strcmp(_s,s._s) < 0)); }
  159. inline bool operator>(const Str &s) const { return ((_l > s._l)&&(strcmp(_s,s._s) > 0)); }
  160. inline bool operator<=(const Str &s) const { return ((_l <= s._l)&&(strcmp(_s,s._s) <= 0)); }
  161. inline bool operator>=(const Str &s) const { return ((_l >= s._l)&&(strcmp(_s,s._s) >= 0)); }
  162. inline bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  163. inline bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  164. inline bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
  165. inline bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
  166. inline bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
  167. inline bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
  168. private:
  169. uint8_t _l;
  170. char _s[ZT_STR_CAPACITY+1];
  171. };
  172. } // namespace ZeroTier
  173. #endif