Str.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #define ZT_STR_CAPACITY 254
  34. namespace ZeroTier {
  35. /**
  36. * A short non-allocating replacement for std::string
  37. */
  38. class Str
  39. {
  40. public:
  41. typedef char * iterator;
  42. typedef const char * const_iterator;
  43. inline Str() { _l = 0; _s[0] = 0; }
  44. inline Str(const Str &s)
  45. {
  46. _l = s._l;
  47. memcpy(_s,s._s,_l+1);
  48. }
  49. inline Str(const char *s)
  50. {
  51. _l = 0;
  52. _s[0] = 0;
  53. (*this) << s;
  54. }
  55. inline Str &operator=(const Str &s)
  56. {
  57. _l = s._l;
  58. memcpy(_s,s._s,_l+1);
  59. return *this;
  60. }
  61. inline Str &operator=(const char *s)
  62. {
  63. _l = 0;
  64. _s[0] = 0;
  65. return ((*this) << s);
  66. }
  67. inline char operator[](const unsigned int i) const
  68. {
  69. if (unlikely(i >= (unsigned int)_l))
  70. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  71. return _s[i];
  72. }
  73. inline void clear() { _l = 0; _s[0] = 0; }
  74. inline const char *c_str() const { return _s; }
  75. inline unsigned int length() const { return (unsigned int)_l; }
  76. inline iterator begin() { return (iterator)_s; }
  77. inline iterator end() { return (iterator)(_s + (unsigned long)_l); }
  78. inline const_iterator begin() const { return (const_iterator)_s; }
  79. inline const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
  80. inline Str &operator<<(const char *s)
  81. {
  82. if (likely(s != (const char *)0)) {
  83. unsigned long l = _l;
  84. while (*s) {
  85. if (unlikely(l >= ZT_STR_CAPACITY)) {
  86. _s[ZT_STR_CAPACITY] = 0;
  87. _l = ZT_STR_CAPACITY;
  88. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  89. }
  90. _s[l++] = *s;
  91. }
  92. _s[l] = 0;
  93. _l = (uint8_t)l;
  94. }
  95. return *this;
  96. }
  97. inline Str &operator<<(const Str &s) { return ((*this) << s._s); }
  98. inline Str &operator<<(const char c)
  99. {
  100. if (unlikely(_l >= ZT_STR_CAPACITY)) {
  101. _s[ZT_STR_CAPACITY] = 0;
  102. throw ZT_EXCEPTION_OUT_OF_BOUNDS;
  103. }
  104. _s[(unsigned long)(_l++)] = c;
  105. _s[(unsigned long)_l] = 0;
  106. }
  107. inline Str &operator<<(const unsigned long n)
  108. {
  109. char tmp[32];
  110. Utils::decimal(n,tmp);
  111. return ((*this) << tmp);
  112. }
  113. inline Str &operator<<(const unsigned int n)
  114. {
  115. char tmp[32];
  116. Utils::decimal((unsigned long)n,tmp);
  117. return ((*this) << tmp);
  118. }
  119. inline Str &operator<<(const Address &a)
  120. {
  121. char tmp[32];
  122. return ((*this) << a.toString(tmp));
  123. }
  124. inline Str &operator<<(const InetAddress &a)
  125. {
  126. char tmp[128];
  127. return ((*this) << a.toString(tmp));
  128. }
  129. inline Str &operator<<(const MAC &a)
  130. {
  131. char tmp[64];
  132. return ((*this) << a.toString(tmp));
  133. }
  134. inline bool operator==(const Str &s) const { return ((_l == s._l)&&(strcmp(_s,s._s) == 0)); }
  135. inline bool operator!=(const Str &s) const { return ((_l != s._l)||(strcmp(_s,s._s) != 0)); }
  136. inline bool operator<(const Str &s) const { return ((_l < s._l)&&(strcmp(_s,s._s) < 0)); }
  137. inline bool operator>(const Str &s) const { return ((_l > s._l)&&(strcmp(_s,s._s) > 0)); }
  138. inline bool operator<=(const Str &s) const { return ((_l <= s._l)&&(strcmp(_s,s._s) <= 0)); }
  139. inline bool operator>=(const Str &s) const { return ((_l >= s._l)&&(strcmp(_s,s._s) >= 0)); }
  140. inline bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  141. inline bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  142. inline bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
  143. inline bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
  144. inline bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
  145. inline bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
  146. private:
  147. uint8_t _l;
  148. char _s[ZT_STR_CAPACITY+1];
  149. };
  150. } // namespace ZeroTier
  151. #endif