/* * Acess2 C++ Library * - By John Hodge (thePowersGang) * * string (header) * - C++'s String type */ #ifndef _LIBCXX_STRING_ #define _LIBCXX_STRING_ #include "_libcxx_helpers.h" #include namespace std { template struct char_traits { }; template <> struct char_traits { typedef char char_type; typedef int int_type; //typedef streamoff off_type; //typedef streampos pos_type; //typedef mbstate_t state_type; static bool eq(const char_type& c, const char_type& d) { return c == d; } static bool lt(const char_type& c, const char_type& d) { return c < d; } static size_t length(const char_type* s) { size_t ret = 0; while(*s++) ret ++; return ret; } }; template <> struct char_traits { typedef wchar_t char_type; typedef int int_type; //typedef streamoff off_type; //typedef streampos pos_type; //typedef mbstate_t state_type; static size_t length(const char_type* s) { size_t ret = 0; while(*s++) ret ++; return ret; } static bool eq(const char_type& c, const char_type& d) { return c == d; } static bool lt(const char_type& c, const char_type& d) { return c < d; } }; extern void _throw_out_of_range(const char *message); template < class charT, class traits=char_traits, class Alloc=allocator > class basic_string { public: typedef traits traits_type; typedef Alloc allocator_type; typedef charT value_type; typedef typename allocator_type::reference reference; typedef typename allocator_type::const_reference const_reference; typedef size_t size_type; private: struct dynamic_info { allocator_type m_allocator; int m_ref_count = 1; size_type m_capacity = 0; size_type m_size = 0; typename allocator_type::pointer m_data = 0; dynamic_info(const allocator_type& alloc): m_allocator(alloc) { } dynamic_info(const dynamic_info& other): m_allocator(other.m_allocator), m_ref_count(1), m_capacity(other.m_capacity), m_size(other.m_size) { m_data = m_allocator.allocate(m_capacity); for( size_type i = 0; i < m_size; i ++ ) m_data[i] = other.m_data[i]; } }; allocator_type m_allocator; dynamic_info *m_content; public: basic_string(const allocator_type& alloc = allocator_type()): m_allocator(alloc), m_content(0) { } basic_string(const basic_string& str) throw(): basic_string(allocator_type()) { *this = str; } #if _CXX11_AVAIL basic_string(basic_string&& str): m_allocator(str.m_allocator), m_content(str.m_content) { str.m_content = 0; ::_sys::debug("basic_string(move) %p %s", m_content, c_str()); } #endif basic_string(const basic_string& str, const allocator_type& alloc): basic_string(str, 0, str.length(), alloc) { } basic_string(const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()): basic_string(alloc) { if( pos < str.length() ) { if( len > str.length() - pos ) len = str.length() - pos; reserve(len); for( size_type i = 0; i < len; i ++ ) m_content->m_data[i] = str.m_content->m_data[pos+i]; m_content->m_size = len; } } basic_string(const charT *s, const allocator_type& alloc = allocator_type()): basic_string(s, traits::length(s), alloc) { } basic_string(const charT *s, size_type n, const allocator_type& alloc = allocator_type()): basic_string(alloc) { if( n > 0 ) { reserve(n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[i] = s[i]; m_content->m_data[n] = 0; m_content->m_size = n; } } basic_string(size_type n, charT c, const allocator_type& alloc = allocator_type()): basic_string(alloc) { if( n > 0 ) { reserve(n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[i] = c; m_content->m_data[n] = 0; m_content->m_size = n; } } #if __cplusplus < 199711L basic_string(basic_string&& str) noexcept: basic_string(allocator_type()) { } basic_string(basic_string&& str, const allocator_type& alloc) noexcept: basic_string(alloc) { m_content = str.m_content; str.m_content = 0; } #endif ~basic_string() { release_content(); } basic_string& operator=(const basic_string& str) throw() { return assign(str); } basic_string& operator=(const charT* s) { return assign(s); } basic_string& operator=(charT c) { return assign(c); } // iterators // capacity size_type size() const { return m_content ? m_content->m_size : 0; } size_type length() const { return size(); } size_type max_size() const { return -1; } void resize(size_type size, charT c = 0) { reserve(size); if( m_content->m_size < size ) { for( size_type ofs = m_content->m_size; ofs < size; ofs ++ ) m_content->m_data[ofs] = c; m_content->m_data[size] = 0; } m_content->m_size = size; m_content->m_data[size] = 0; } size_type capacity() const { return m_content ? m_content->m_capacity : 0; } void reserve(size_type size) { own_content(); size = (size+1 + 31) & ~31; if( size > m_content->m_capacity ) { auto new_area = m_allocator.allocate(size); for( size_type i = 0; i < m_content->m_size; i ++ ) new_area[i] = m_content->m_data[i]; m_allocator.deallocate(m_content->m_data, m_content->m_capacity); m_content->m_data = new_area; m_content->m_capacity = size; } } void clear() { own_content(); m_content->m_size = 0; } bool empty() const { return length() == 0; } // Access reference operator[] (size_type pos) { own_content(); return m_content->m_data[pos]; } const_reference operator[] (size_type pos) const { return (m_content ? m_content->m_data[pos] : *(const charT*)0); } reference at(size_type pos) { own_content(); if(pos >= m_content->m_size) _throw_out_of_range("basic_string - at"); return m_content->m_data[pos]; } const_reference at(size_type pos) const { if(!m_content || pos >= m_content.m_size) _throw_out_of_range("basic_string - at"); return m_content->m_data[pos]; } // Modifiers basic_string& operator +=(const basic_string& str) { return append(str); } basic_string& operator +=(const charT* s) { return append(s); } basic_string& operator +=(charT c) { push_back(c); return *this; } basic_string& append(const basic_string& str) { return append(str, 0, npos); } basic_string& append(const basic_string& str, size_type subpos, size_type sublen) { if(subpos >= str.size()) _throw_out_of_range("basic_string - assign source"); if( sublen > str.size() - subpos ) sublen = str.size() - subpos; append( str.data() + subpos, sublen ); return *this; } basic_string& append(const charT* s) { return append(s, traits::length(s)); } basic_string& append(const charT* s, size_type n) { reserve(size() + n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[size() + i] = s[i]; m_content->m_data[size()+n] = '\0'; m_content->m_size += n; return *this; } basic_string& append(size_type n, charT c) { reserve(size() + n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[size() + i] = c; m_content->m_data[size()+n] = '\0'; m_content->m_size += n; return *this; } void push_back(charT c) { append(1, c); } basic_string& assign(const basic_string& str) throw() { // Special case, triggers copy-on-write. release_content(); m_content = str.m_content; m_content->m_ref_count ++; return *this; } basic_string& assign(const basic_string& str, size_type subpos, size_type sublen) { if(subpos >= str.size()) _throw_out_of_range("basic_string - assign source"); if( sublen > str.size() - subpos ) sublen = str.size() - subpos; return assign(str.data() + subpos, sublen); } basic_string& assign(const charT* s) { return assign(s, traits::length(s)); } basic_string& assign(const charT* s, size_type n) { release_content(); reserve(n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[i] = s[i]; m_content->m_data[n] = '\0'; m_content->m_size = n; return *this; } basic_string& assign(size_type n, charT c) { release_content(); reserve(n); for( size_type i = 0; i < n; i ++ ) m_content->m_data[i] = c; m_content->m_data[n] = '\0'; m_content->m_size = n; return *this; } // String operations const char *c_str() const { // TODO: this is const, but also might need to do processing if(m_content) { _libcxx_assert(m_content->m_data[m_content->m_size] == '\0'); } return (m_content ? m_content->m_data : ""); } const char *data() const { return (m_content ? m_content->m_data : NULL); } static const size_type npos = -1; private: void own_content() { if(!m_content) { m_content = new dynamic_info(m_allocator); } else if( m_content->m_ref_count > 1 ) { dynamic_info *new_cont = new dynamic_info(*m_content); m_content->m_ref_count --; m_content = new_cont; } else { // already owned } } void release_content() { if( m_content ) { m_content->m_ref_count --; if( m_content->m_ref_count == 0 ) { m_allocator.deallocate(m_content->m_data, m_content->m_capacity); delete m_content; } m_content = NULL; } } }; typedef basic_string string; template basic_string operator+(const basic_string& lhs, const basic_string& rhs) { basic_string ret; ret.reserve(lhs.size() + rhs.size()); ret += lhs; ret += rhs; return ret; } template basic_string operator+(const basic_string& lhs, const charT* rhs) { basic_string ret; ret.reserve(lhs.size() + traits::length(rhs)); ret += lhs; ret += rhs; return ret; } template basic_string operator+(const charT* lhs, const basic_string& rhs) { basic_string ret; ret.reserve(traits::length(lhs) + rhs.size()); ret += lhs; ret += rhs; return ret; } template basic_string operator+(const basic_string& lhs, const charT rhs) { basic_string ret; ret.reserve(lhs.size() + 1); ret += lhs; ret += rhs; return ret; } template basic_string operator+(const charT lhs, const basic_string& rhs) { basic_string ret; ret.reserve(1 + rhs.size()); ret += lhs; ret += rhs; return ret; } }; #endif // vim: ft=cpp