From 52ccba7737ead79471e790c126455abc4d310f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 6 Sep 2016 19:13:27 -0700 Subject: [PATCH] Cleanup. --- include/bx/string.h | 294 ++++++++++++++++++++++---------------------- 1 file changed, 147 insertions(+), 147 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 35e9117..df837a3 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -23,153 +23,6 @@ namespace bx { - /// Non-zero-terminated string view. - class StringView - { - public: - StringView() - { - clear(); - } - - StringView(const StringView& _rhs) - { - set(_rhs.m_ptr, _rhs.m_len); - } - - StringView& operator=(const StringView& _rhs) - { - set(_rhs.m_ptr, _rhs.m_len); - return *this; - } - - StringView(const char* _ptr, uint32_t _len = UINT32_MAX) - { - set(_ptr, _len); - } - - void set(const char* _ptr, uint32_t _len = UINT32_MAX) - { - clear(); - - if (NULL != _ptr) - { - uint32_t len = UINT32_MAX == _len ? strlen(_ptr) : _len; - if (0 != len) - { - m_len = len; - m_ptr = _ptr; - } - } - } - - void clear() - { - m_ptr = ""; - m_len = 0; - } - - const char* getPtr() const - { - return m_ptr; - } - - const char* getTerm() const - { - return m_ptr + m_len; - } - - bool isEmpty() const - { - return 0 == m_len; - } - - uint32_t getLength() const - { - return m_len; - } - - protected: - friend uint32_t hashMurmur2A(const StringView& _data); - - const char* m_ptr; - uint32_t m_len; - }; - - inline uint32_t hashMurmur2A(const StringView& _data) - { - return hashMurmur2A(_data.m_ptr, _data.m_len); - } - - inline uint32_t hashMurmur2A(const char* _data) - { - return hashMurmur2A(StringView(_data) ); - } - - /// ASCII string - template - class StringT : public StringView - { - public: - StringT() - : StringView("", 0) - { - } - - StringT(const StringT& _rhs) - { - set(_rhs.m_ptr, _rhs.m_len); - } - - StringT& operator=(const StringT& _rhs) - { - set(_rhs.m_ptr, _rhs.m_len); - return *this; - } - - StringT(const char* _ptr, uint32_t _len = UINT32_MAX) - { - set(_ptr, _len); - } - - StringT(const StringView& _rhs) - { - set(_rhs.getPtr(), _rhs.getLength() ); - } - - ~StringT() - { - clear(); - } - - void set(const char* _ptr, uint32_t _len = UINT32_MAX) - { - clear(); - - if (0 != _len) - { - uint32_t len = UINT32_MAX == _len ? strlen(_ptr) : _len; - m_len = len; - char* ptr = (char*)BX_ALLOC(*AllocatorT, len+1); - - memcpy(ptr, _ptr, len); - ptr[len] = '\0'; - - *const_cast(&m_ptr) = ptr; - } - } - - void clear() - { - if (0 != m_len) - { - BX_FREE(*AllocatorT, const_cast(m_ptr) ); - - StringView::clear(); - } - } - }; - /// inline bool toBool(const char* _str) { @@ -678,6 +531,153 @@ namespace bx return(dlen + (s - _src)); /* count does not include NUL */ } + /// Non-zero-terminated string view. + class StringView + { + public: + StringView() + { + clear(); + } + + StringView(const StringView& _rhs) + { + set(_rhs.m_ptr, _rhs.m_len); + } + + StringView& operator=(const StringView& _rhs) + { + set(_rhs.m_ptr, _rhs.m_len); + return *this; + } + + StringView(const char* _ptr, uint32_t _len = UINT32_MAX) + { + set(_ptr, _len); + } + + void set(const char* _ptr, uint32_t _len = UINT32_MAX) + { + clear(); + + if (NULL != _ptr) + { + uint32_t len = uint32_t(strnlen(_ptr, _len) ); + if (0 != len) + { + m_len = len; + m_ptr = _ptr; + } + } + } + + void clear() + { + m_ptr = ""; + m_len = 0; + } + + const char* getPtr() const + { + return m_ptr; + } + + const char* getTerm() const + { + return m_ptr + m_len; + } + + bool isEmpty() const + { + return 0 == m_len; + } + + uint32_t getLength() const + { + return m_len; + } + + protected: + friend uint32_t hashMurmur2A(const StringView& _data); + + const char* m_ptr; + uint32_t m_len; + }; + + inline uint32_t hashMurmur2A(const StringView& _data) + { + return hashMurmur2A(_data.m_ptr, _data.m_len); + } + + inline uint32_t hashMurmur2A(const char* _data) + { + return hashMurmur2A(StringView(_data) ); + } + + /// ASCII string + template + class StringT : public StringView + { + public: + StringT() + : StringView("", 0) + { + } + + StringT(const StringT& _rhs) + { + set(_rhs.m_ptr, _rhs.m_len); + } + + StringT& operator=(const StringT& _rhs) + { + set(_rhs.m_ptr, _rhs.m_len); + return *this; + } + + StringT(const char* _ptr, uint32_t _len = UINT32_MAX) + { + set(_ptr, _len); + } + + StringT(const StringView& _rhs) + { + set(_rhs.getPtr(), _rhs.getLength() ); + } + + ~StringT() + { + clear(); + } + + void set(const char* _ptr, uint32_t _len = UINT32_MAX) + { + clear(); + + if (0 != _len) + { + uint32_t len = uint32_t(strnlen(_ptr, _len) ); + m_len = len; + char* ptr = (char*)BX_ALLOC(*AllocatorT, len+1); + + memcpy(ptr, _ptr, len); + ptr[len] = '\0'; + + *const_cast(&m_ptr) = ptr; + } + } + + void clear() + { + if (0 != m_len) + { + BX_FREE(*AllocatorT, const_cast(m_ptr) ); + + StringView::clear(); + } + } + }; + } // namespace bx #endif // BX_STRING_H_HEADER_GUARD