diff options
Diffstat (limited to 'src/include/rc_string.hpp')
-rw-r--r-- | src/include/rc_string.hpp | 48 |
1 files changed, 13 insertions, 35 deletions
diff --git a/src/include/rc_string.hpp b/src/include/rc_string.hpp index 6091d748..4649b36e 100644 --- a/src/include/rc_string.hpp +++ b/src/include/rc_string.hpp @@ -14,16 +14,7 @@ public: m_ptr(nullptr), m_len(0) {} - RcString(const char* s, unsigned int len): - m_ptr( new unsigned int[1 + (len+1 + sizeof(unsigned int)-1) / sizeof(unsigned int)] ), - m_len(len) - { - *m_ptr = 1; - char* data_mut = reinterpret_cast<char*>(m_ptr + 1); - for(unsigned int j = 0; j < len; j ++ ) - data_mut[j] = s[j]; - data_mut[len] = '\0'; - } + RcString(const char* s, unsigned int len); RcString(const char* s): RcString(s, ::std::strlen(s)) { @@ -37,7 +28,7 @@ public: m_ptr(x.m_ptr), m_len(x.m_len) { - *m_ptr += 1; + if( m_ptr ) *m_ptr += 1; } RcString(RcString&& x): m_ptr(x.m_ptr), @@ -47,18 +38,7 @@ public: x.m_len = 0; } - ~RcString() - { - if(m_ptr) - { - *m_ptr -= 1; - if( *m_ptr == 0 ) - { - delete[] m_ptr; - m_ptr = nullptr; - } - } - } + ~RcString(); RcString& operator=(const RcString& x) { @@ -67,7 +47,7 @@ public: this->~RcString(); m_ptr = x.m_ptr; m_len = x.m_len; - *m_ptr += 1; + if( m_ptr ) *m_ptr += 1; } return *this; } @@ -86,18 +66,16 @@ public: const char* c_str() const { - return reinterpret_cast<const char*>(m_ptr + 1); - } - bool operator==(const char* s) const { - if( m_len == 0 ) - return *s == '\0'; - auto m = this->c_str(); - do { - if( *m != *s ) - return false; - } while( *m++ != '\0' && *s++ != '\0' ); - return true; + if( m_len > 0 ) + { + return reinterpret_cast<const char*>(m_ptr + 1); + } + else + { + return ""; + } } + bool operator==(const char* s) const; friend ::std::ostream& operator<<(::std::ostream& os, const RcString& x) { return os << x.c_str(); } |