Str.hpp 4.2 KB

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