Str.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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() { memset(reinterpret_cast<void *>(this),0,sizeof(Str)); }
  33. explicit ZT_ALWAYS_INLINE Str(const char *s) { *this = s; }
  34. ZT_ALWAYS_INLINE Str &operator=(const char *s)
  35. {
  36. if (s) {
  37. unsigned int l = 0;
  38. while (l < C) {
  39. char c = s[l];
  40. if (!c) break;
  41. _s[l++] = c;
  42. }
  43. _s[l] = 0;
  44. _l = (uint16_t)l;
  45. } else {
  46. _l = 0;
  47. _s[0] = 0;
  48. }
  49. }
  50. ZT_ALWAYS_INLINE char operator[](const unsigned int i) const
  51. {
  52. if (i >= (unsigned int)_l)
  53. return 0;
  54. return _s[i];
  55. }
  56. ZT_ALWAYS_INLINE void clear() { _l = 0; _s[0] = 0; }
  57. ZT_ALWAYS_INLINE const char *c_str() const { return _s; }
  58. ZT_ALWAYS_INLINE unsigned int length() const { return (unsigned int)_l; }
  59. ZT_ALWAYS_INLINE bool empty() const { return (_l == 0); }
  60. ZT_ALWAYS_INLINE iterator begin() { return (iterator)_s; }
  61. ZT_ALWAYS_INLINE iterator end() { return (iterator)(_s + (unsigned long)_l); }
  62. ZT_ALWAYS_INLINE const_iterator begin() const { return (const_iterator)_s; }
  63. ZT_ALWAYS_INLINE const_iterator end() const { return (const_iterator)(_s + (unsigned long)_l); }
  64. ZT_ALWAYS_INLINE Str &operator<<(const char *s)
  65. {
  66. if (s) {
  67. unsigned int l = _l;
  68. while (l < C) {
  69. char c = s[l];
  70. if (!c) break;
  71. _s[l++] = c;
  72. }
  73. _s[l] = 0;
  74. _l = (uint16_t)l;
  75. }
  76. }
  77. ZT_ALWAYS_INLINE Str &operator<<(const Str &s) { return ((*this) << s._s); }
  78. ZT_ALWAYS_INLINE Str &operator<<(const char c)
  79. {
  80. if (_l < C) {
  81. _s[_l++] = c;
  82. _s[_l] = 0;
  83. }
  84. return *this;
  85. }
  86. ZT_ALWAYS_INLINE Str &operator<<(const Address &a)
  87. {
  88. char tmp[32];
  89. return ((*this) << a.toString(tmp));
  90. }
  91. ZT_ALWAYS_INLINE Str &operator<<(const InetAddress &a)
  92. {
  93. char tmp[128];
  94. return ((*this) << a.toString(tmp));
  95. }
  96. ZT_ALWAYS_INLINE Str &operator<<(const MAC &a)
  97. {
  98. char tmp[64];
  99. return ((*this) << a.toString(tmp));
  100. }
  101. ZT_ALWAYS_INLINE operator bool() const { return (_l != 0); }
  102. ZT_ALWAYS_INLINE bool operator==(const Str &s) const { return ((_l == s._l)&&(memcmp(_s,s._s,_l) == 0)); }
  103. ZT_ALWAYS_INLINE bool operator!=(const Str &s) const { return ((_l != s._l)||(memcmp(_s,s._s,_l) != 0)); }
  104. ZT_ALWAYS_INLINE bool operator<(const Str &s) const { return ( (_l < s._l) ? true : ((_l == s._l) ? (memcmp(_s,s._s,_l) < 0) : false) ); }
  105. ZT_ALWAYS_INLINE bool operator>(const Str &s) const { return (s < *this); }
  106. ZT_ALWAYS_INLINE bool operator<=(const Str &s) const { return !(s < *this); }
  107. ZT_ALWAYS_INLINE bool operator>=(const Str &s) const { return !(*this < s); }
  108. ZT_ALWAYS_INLINE bool operator==(const char *s) const { return (strcmp(_s,s) == 0); }
  109. ZT_ALWAYS_INLINE bool operator!=(const char *s) const { return (strcmp(_s,s) != 0); }
  110. ZT_ALWAYS_INLINE bool operator<(const char *s) const { return (strcmp(_s,s) < 0); }
  111. ZT_ALWAYS_INLINE bool operator>(const char *s) const { return (strcmp(_s,s) > 0); }
  112. ZT_ALWAYS_INLINE bool operator<=(const char *s) const { return (strcmp(_s,s) <= 0); }
  113. ZT_ALWAYS_INLINE bool operator>=(const char *s) const { return (strcmp(_s,s) >= 0); }
  114. ZT_ALWAYS_INLINE unsigned long hashCode() const { return Utils::hashString(_s,_l); }
  115. private:
  116. uint16_t _l;
  117. char _s[C+1];
  118. };
  119. } // namespace ZeroTier
  120. #endif