Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tim Noye
- #include "stdafx.h"
- #include "myString.h"
- namespace notstd
- {
- myString::myString()
- : dataPtr(NULL), arrSize(0), currLength(0), minResize(5)
- {
- }
- myString::myString(const myString& msIn)
- : currLength(msIn.length()), minResize(5)
- {
- if (currLength == 0)
- {
- dataPtr = NULL;
- arrSize = 0;
- }
- else
- {
- arrSize = currLength + minResize - currLength % minResize;
- dataPtr = new char[arrSize]();
- char *tempPtr = msIn.data();
- std::copy(tempPtr, tempPtr + currLength, dataPtr);
- delete[] tempPtr;
- }
- }
- //pre: charPtrIn must be a NULL terminated array
- myString::myString(char* charPtrIn)
- : currLength(0), minResize(5)
- {
- char *cPtr = charPtrIn;
- while (*(cPtr + currLength) != NULL)
- ++currLength;
- if (currLength == 0)
- {
- arrSize = 0;
- dataPtr = NULL;
- }
- else
- {
- arrSize = currLength + minResize - currLength % minResize;
- dataPtr = new char[arrSize]();
- std::copy(charPtrIn, charPtrIn + currLength, dataPtr);
- }
- }
- myString::~myString()
- {
- delete[] dataPtr;
- }
- myString& myString::operator=(const myString& msIn)
- {
- if (&msIn != this)
- {
- if (msIn.length() == 0)
- {
- delete[] dataPtr;
- dataPtr = NULL;
- arrSize = 0;
- currLength = 0;
- }
- else
- {
- currLength = msIn.length();
- char *tempPtr = msIn.data();
- if (msIn.length() > arrSize)
- {
- arrSize = currLength + minResize - currLength % minResize;
- delete[] dataPtr;
- dataPtr = new char[arrSize]();
- }
- std::copy(tempPtr, tempPtr + currLength, dataPtr);
- delete[] tempPtr;
- }
- }
- return *this;
- }
- myString& myString::operator+=(const myString& msIn)
- {
- if (!msIn.isEmpty())
- {
- arrSize = msIn.length() + currLength;
- arrSize += minResize - (arrSize % minResize);
- char* tPtr = new char[arrSize]();
- std::copy(dataPtr, dataPtr + currLength, tPtr);
- char* msInPtr = msIn.data();
- std::copy(msInPtr, msInPtr + msIn.length(), tPtr + currLength);
- delete[] dataPtr;
- delete[] msInPtr;
- dataPtr = tPtr;
- currLength = msIn.length() + currLength;
- }
- return *this;
- }
- myString& myString::operator+=(const char charIn)
- {
- if (currLength + 1 > arrSize)
- {
- arrSize += minResize;
- char* tPtr = new char[arrSize]();
- std::copy(dataPtr, dataPtr + currLength, tPtr);
- delete[] dataPtr;
- dataPtr = tPtr;
- }
- dataPtr[currLength] = charIn;
- ++currLength;
- return *this;
- }
- char& myString::operator[](const size_t indexIn)
- {
- return dataPtr[indexIn];
- }
- const char& myString::operator[](const size_t indexIn)const
- {
- return dataPtr[indexIn];
- }
- char& myString::at(const size_t indexIn)
- {
- return dataPtr[indexIn];
- }
- char& myString::front()
- {
- return dataPtr[0];
- }
- const char& myString::front()const
- {
- return dataPtr[0];
- }
- char& myString::back()
- {
- return dataPtr[currLength - 1];
- }
- const char& myString::back()const
- {
- return dataPtr[currLength - 1];
- }
- void myString::insert(const size_t posIn, const myString& msIn)
- {
- if (!msIn.isEmpty() && posIn < currLength)
- {
- char* cPtr = NULL;
- cPtr = msIn.data();
- if (currLength + msIn.length() > arrSize)
- {
- arrSize = msIn.length() + currLength;
- arrSize += minResize - (arrSize % minResize);
- }
- char* tPtr = new char[arrSize]();
- std::copy(dataPtr, dataPtr + posIn, tPtr);
- std::copy(cPtr, cPtr + msIn.length(), tPtr + posIn);
- std::copy(dataPtr + posIn, dataPtr + currLength, tPtr + msIn.length() + posIn);
- delete[] dataPtr;
- delete[] cPtr;
- dataPtr = tPtr;
- currLength += msIn.length();
- }
- }
- void myString::replace(const size_t fromIn, size_t lenIn, const myString& msIn)
- {
- if (fromIn <= currLength || (msIn.isEmpty() && lenIn == 0))
- {
- if (fromIn + lenIn > currLength)
- {
- lenIn = currLength - fromIn;
- }
- erase(fromIn, lenIn);
- insert(fromIn, msIn);
- }
- }
- void myString::erase(const size_t posIn, const size_t lenIn)
- {
- if (currLength != 0 && lenIn > 0 && posIn < currLength)
- {
- if (posIn + lenIn >= currLength)
- {
- currLength = posIn;
- }
- else
- {
- char* tPtr = new char[arrSize]();
- if (posIn == 0)
- {
- std::copy(dataPtr + lenIn, dataPtr + currLength, tPtr);
- }
- else
- {
- std::copy(dataPtr, dataPtr + posIn, tPtr);
- std::copy(dataPtr + posIn + lenIn, dataPtr + currLength, tPtr + posIn);
- }
- delete[] dataPtr;
- dataPtr = tPtr;
- currLength -= lenIn;
- }
- }
- }
- void myString::clear()
- {
- delete[]dataPtr;
- dataPtr = NULL;
- currLength = 0;
- arrSize = 0;
- }
- char* myString::data()const
- {
- char *tempPtr = new char[currLength + 1]();
- std::copy(dataPtr, dataPtr + currLength, tempPtr);
- tempPtr[currLength] = NULL;
- return tempPtr;
- }
- size_t myString::find(const myString& msIn, const size_t posIn)const
- {
- bool mismatch;
- if (currLength >= posIn + msIn.length())
- {
- for (size_t datai = posIn; datai <= currLength - msIn.length(); ++datai)
- {
- mismatch = false;
- for (size_t msIni = 0; msIni < msIn.length() && !mismatch; ++msIni)
- if (msIn[msIni] != dataPtr[datai + msIni])
- mismatch = true;
- if (mismatch == false)
- return datai;
- }
- }
- return -1;
- }
- myString myString::substr(const size_t posIn, size_t lenIn)const
- {
- if (lenIn == 0 || posIn > currLength)
- return myString();
- if (currLength < posIn + lenIn)
- lenIn = currLength - posIn;
- char* tempData;
- tempData = new char[lenIn + 1]();
- std::copy(dataPtr + posIn, dataPtr + posIn + lenIn, tempData);
- myString tempMS(tempData);
- delete[] tempData;
- return tempMS;
- }
- size_t myString::length()const
- {
- return currLength;
- }
- bool myString::isEmpty()const
- {
- return currLength == 0;
- }
- myString myString::operator+(const myString& msIn)const
- {
- myString tempMS(*this);
- tempMS += msIn;
- return tempMS;
- }
- std::istream& operator >>(std::istream& in, myString& msIn)
- {
- msIn.clear();
- char cIn;
- while (in.get(cIn) && cIn != '\n')
- {
- if (msIn.currLength == msIn.arrSize)
- msIn.resize(msIn.arrSize + msIn.minResize);
- msIn.dataPtr[msIn.currLength] = cIn;
- ++msIn.currLength;
- }
- return in;
- }
- std::ostream& operator <<(std::ostream& out, myString& msIn)
- {
- char* tempPtr = msIn.data();
- out << tempPtr;
- delete[] tempPtr;
- return out;
- }
- void myString::resize(const size_t newSize)
- {
- if (newSize > arrSize)
- {
- char* tempPtr = new char[newSize]();
- std::copy(dataPtr, dataPtr + currLength, tempPtr);
- delete[] dataPtr;
- dataPtr = tempPtr;
- arrSize = newSize;
- }
- }
- char* myString::begin()
- {
- return dataPtr;
- }
- char* myString::end()
- {
- return dataPtr + currLength;
- }
- //Doesn't check for a string that is empty
- char& myString::rsearch(char& charPtrIn, notstd::myString msIn)
- {
- for (size_t i = currLength; i >= 0; i--) {
- if (msIn[i] == charPtrIn)
- {
- return charPtrIn;
- }
- }
- }
- myString& myString::rsearchstring(myString& stringPtrIn, notstd::myString msIn)
- {
- for (size_t i = currLength; i >= 0; i--) {
- if(stringPtrIn == msIn)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement