Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. namespace nall {
  2.  
  3. string::string() : _data(nullptr), _capacity(SSO - 1), _size(0) {
  4. }
  5.  
  6. auto string::pointer() -> char* {
  7. if(_capacity < SSO) return _text;
  8. if(*_refs > 1) _copy();
  9. return _data;
  10. }
  11.  
  12. auto string::data() const -> const char* {
  13. if(_capacity < SSO) return _text;
  14. return _data;
  15. }
  16.  
  17. auto string::reset() -> type& {
  18. if(_capacity >= SSO && !--*_refs) memory::free(_data);
  19. _data = nullptr;
  20. _capacity = SSO - 1;
  21. _size = 0;
  22. return *this;
  23. }
  24.  
  25. auto string::reserve(unsigned capacity) -> type& {
  26. if(capacity <= _capacity) return *this;
  27. capacity = bit::round(capacity + 1) - 1;
  28. if(_capacity < SSO) {
  29. _capacity = capacity;
  30. _allocate();
  31. } else if(*_refs > 1) {
  32. _capacity = capacity;
  33. _copy();
  34. } else {
  35. _capacity = capacity;
  36. _resize();
  37. }
  38. return *this;
  39. }
  40.  
  41. auto string::resize(unsigned size) -> type& {
  42. reserve(size);
  43. pointer()[_size = size] = 0;
  44. return *this;
  45. }
  46.  
  47. auto string::operator=(const string& source) -> type& {
  48. if(&source == this) return *this;
  49. reset();
  50. if(source._capacity >= SSO) {
  51. _data = source._data;
  52. _refs = source._refs;
  53. _capacity = source._capacity;
  54. _size = source._size;
  55. ++*_refs;
  56. } else {
  57. memory::copy(_text, source._text, SSO);
  58. _capacity = source._capacity;
  59. _size = source._size;
  60. }
  61. return *this;
  62. }
  63.  
  64. auto string::operator=(string&& source) -> type& {
  65. if(&source == this) return *this;
  66. reset();
  67. memory::copy(this, &source, sizeof(string));
  68. source._data = nullptr;
  69. source._capacity = SSO - 1;
  70. source._size = 0;
  71. return *this;
  72. }
  73.  
  74. //SSO -> COW
  75. auto string::_allocate() -> void {
  76. char _temp[SSO];
  77. memory::copy(_temp, _text, SSO);
  78. _data = (char*)memory::allocate(_capacity + 1 + sizeof(unsigned));
  79. memory::copy(_data, _temp, SSO);
  80. _refs = (unsigned*)(_data + _capacity + 1); //always aligned by 32 via reserve()
  81. *_refs = 1;
  82. }
  83.  
  84. //COW -> Unique
  85. auto string::_copy() -> void {
  86. auto _temp = (char*)memory::allocate(_capacity + 1 + sizeof(unsigned));
  87. memory::copy(_temp, _data, _size = min(_capacity, _size));
  88. _temp[_size] = 0;
  89. --*_refs;
  90. _data = _temp;
  91. _refs = (unsigned*)(_data + _capacity + 1);
  92. *_refs = 1;
  93. }
  94.  
  95. //COW -> Resize
  96. auto string::_resize() -> void {
  97. _data = (char*)memory::resize(_data, _capacity + 1 + sizeof(unsigned));
  98. _refs = (unsigned*)(_data + _capacity + 1);
  99. *_refs = 1;
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement