From: Christian P. <cp...@us...> - 2004-12-27 07:00:40
|
Update of /cvsroot/pclasses/pclasses2/src/Unicode In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9925/src/Unicode Modified Files: String.cpp Log Message: Added String::utf8(), added String::ConstIterator, added std::string support. Index: String.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Unicode/String.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- String.cpp 23 Dec 2004 05:51:19 -0000 1.2 +++ String.cpp 27 Dec 2004 07:00:31 -0000 1.3 @@ -24,6 +24,7 @@ #include "pclasses/Algorithm.h" #include <cstring> +#include <sstream> namespace P { @@ -59,6 +60,11 @@ { } +String::String(const std::string& str) +{ + *this = fromLatin1(str.c_str()); +} + String::~String() throw() { } @@ -98,10 +104,19 @@ newLength = length; } -// else -// { -// size_t len1; -// } + else + { + size_t len1 = holeOffset - length; + size_t off2 = holeOffset; + size_t len2 = length - off2; + + newStr = new char[sizeof(Char) * sz]; + copy_construct((Char*)newStr, data(), len1); + construct((Char*)newStr + holeOffset, holeLen); + copy_construct((Char*)newStr + off2, data(), len2); + + newLength = length + holeLen; + } newData->str = (Char*)newStr; newData->size = sz; @@ -115,6 +130,19 @@ return _length; } +std::string String::utf8() const +{ + std::ostringstream os; + ConstIterator i = begin(); + while(i != end()) + { + os << (*i).latin1(); + ++i; + } + + return os.str(); +} + String String::part(size_t offset, size_t length) const throw(OutOfMemory, OutOfBounds) { @@ -134,24 +162,11 @@ return part(_length - length, length); } -void String::makeUnique() const throw(OutOfMemory) +String String::deepCopy() const { - if(_data.useCount() > 1) - { - // allocate new data structure - SharedPtr<Data> newData(new Data); - - // allocate and copy-construct string data - char* newStr = new char[sizeof(Char) * (_length + 8)]; - copy_construct((Char*)newStr, data(), _length); - - // set the copied string data - newData->str = (Char*)newStr; - newData->size = _length + 8; - _data = newData; - _offset = 0; - _length = _length; - } + String str(_length + 8); + copy_construct(str.data(), data(), _length); + return str; } const Char& String::at(size_t pos) const throw(OutOfBounds) @@ -176,7 +191,8 @@ >= _length) throw OutOfBounds("Index is out of bounds", P_SOURCEINFO); - makeUnique(); + if(_data.useCount() > 1) + *this = deepCopy(); // construct Char's that were not yet constructed, but allocated /*if(pos >= _length) @@ -214,6 +230,12 @@ return *this; } +String& String::operator=(const std::string& str) +{ + *this = fromLatin1(str.c_str()); + return *this; +} + bool String::operator==(const String& str) const throw() { if(&str == this || (str.data() == data() && str._length == _length)) @@ -316,22 +338,38 @@ String::Iterator String::begin() { - makeUnique(); + if(_data.useCount() > 1) + *this = deepCopy(); + return Iterator(data()); } String::Iterator String::end() { - makeUnique(); + if(_data.useCount() > 1) + *this = deepCopy(); + return Iterator(data() + _length); } +String::ConstIterator String::begin() const +{ + return ConstIterator(data()); +} + +String::ConstIterator String::end() const +{ + return ConstIterator(data() + _length); +} + void String::insert(const Iterator& pos, const Char& ch) { -/* if(_data.useCount() > 1 || _length + 1 > _data->size) - resize(_data->size + 8);*/ + if(_data.useCount() > 1 || _length + 1 > _data->size) + resize(size() + 1, &(*pos) - data(), 1); + else + { /* @todo */ } - + *pos = ch; } void String::insert(const Iterator& pos, const String& str) @@ -440,6 +478,68 @@ return (_current != iter._current); } + +String::ConstIterator::ConstIterator(const String::ConstIterator& iter) +: _current(iter._current) +{ +} + +String::ConstIterator::ConstIterator(const Char* current) +: _current(current) +{ +} + +String::ConstIterator::~ConstIterator() +{ +} + +const Char& String::ConstIterator::operator*() const +{ + return *_current; +} + +String::ConstIterator& String::ConstIterator::operator++() +{ + ++_current; + return *this; +} + +String::ConstIterator String::ConstIterator::operator++(int) +{ + ConstIterator tmp = *this; + ++_current; + return tmp; +} + +String::ConstIterator& String::ConstIterator::operator--() +{ + --_current; + return *this; +} + +String::ConstIterator String::ConstIterator::operator--(int) +{ + ConstIterator tmp = *this; + --_current; + return tmp; +} + +String::ConstIterator& String::ConstIterator::operator=(const String::ConstIterator& iter) +{ + _current = iter._current; + return *this; +} + +bool String::ConstIterator::operator==(const String::ConstIterator& iter) const +{ + return (_current == iter._current); +} + +bool String::ConstIterator::operator!=(const String::ConstIterator& iter) const +{ + return (_current != iter._current); +} + } // !namespace Unicode } // !namespace P |