Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 2012, University of Freiburg,
- // Chair of Algorithms and Data Structures.
- // Author: Hannah Bast <bast>, Anton Stepan <stepana>
- #include <stdio.h>
- #include <stdlib.h>
- #include "./String.h"
- // Max length of a line.
- const int MAX_LINE_LENGTH = 1000;
- // _____________________________________________________________________________
- template<class T>
- String<T>::String()
- {
- _size = 0;
- _contents = NULL;
- }
- // _____________________________________________________________________________
- String<bool>::String()
- {
- _size = 0;
- _contents = NULL;
- }
- // _____________________________________________________________________________
- template<class T>
- String<T>::~String()
- {
- delete[] _contents;
- }
- // _____________________________________________________________________________
- String<bool>::~String()
- {
- delete[] _contents;
- }
- // _____________________________________________________________________________
- template<class T>
- String<T>::String(const String<T>& s)
- {
- _size = s._size;
- _contents = new char[_size];
- for (int i = 0; i < s._size; i++) _contents[i] = s._contents[i];
- }
- // _____________________________________________________________________________
- template<class T>
- String<T>& String<T>::operator=(const String<T>& x)
- {
- if (_contents != NULL)
- {
- delete[] _contents;
- }
- _contents = new char[_size];
- for (int i = 0; i < _size; i++) _contents[i] = x._contents[i];
- return *this;
- }
- // _____________________________________________________________________________
- String<bool>& String<bool>::operator=(const char* x)
- {
- // if left String already exists, delete.
- if (_contents != NULL)
- {
- delete[] _contents;
- }
- _size = 0;
- _contents = NULL;
- // if const char is "".
- if (x[0] == 0)
- {
- _contents = NULL;
- return *this;
- }
- // new string is created by appending the const char*.
- int i = 0;
- while (x[i] != 0)
- {
- if (x[i] == '1')
- {
- append(1);
- }
- else if (x[i] == '0')
- {
- append(0);
- }
- else
- {
- fprintf(stderr, "Wrong user input. %c\n", x[i]);
- }
- i++;
- }
- return *this;
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::set(const char* s)
- {
- _size = 0;
- if (_contents != NULL) delete[] _contents;
- while (s[_size] != 0) _size++;
- _contents = new char[_size];
- for (int i = 0; i < _size; i++) _contents[i] = s[i];
- }
- // _____________________________________________________________________________
- template<class T>
- int String<T>::size() const
- {
- _numCallsToSizeMethod++;
- return _size;
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::append(String<T> s)
- {
- char* _newContents = new char[_size + s._size];
- for (int i = 0; i < _size; i++) _newContents[i] = _contents[i];
- for (int i = 0; i < s._size; i++) _newContents[_size + i] = s._contents[i];
- delete[] _contents;
- _contents = _newContents;
- _size += s._size;
- }
- // _____________________________________________________________________________
- void String<bool>::append(bool x)
- {
- /*if (_size == 0)
- {
- char* _tmpContents = new char;
- _tmpContents[0] = 0;
- _contents = _tmpContents;
- }
- else if (_size / 8 > 1 && _size % 8 == 0)
- {
- char* _newContents = new char[(_size / 8) + 1];
- for (int i = 0; i < _size; i++) _contents[i] |= _newContents[i];
- _contents = _newContents;
- delete[] _newContents;
- }*/
- // if memory is not enough, allocate 1 more char.
- if (_size % 8 == 0)
- {
- // allocate a temporary Array with one more char as before.
- char* _newContents = new char[_size/8 + 1];
- // initiate every bit with 0.
- for (int i = 0; i < _size/8+1; i++) _newContents[i] = 0;
- // copy old content into new content.
- for (int i = 0; i < _size/8; i++)
- {
- _newContents[i] = _contents[i];
- }
- // delete old content and redirect the pointer to the temp.array
- delete[] _contents;
- _contents = _newContents;
- }
- // If the bool which will be appended is true, add 1, else it's
- // already true.
- if (x == true)
- {
- int byteIndex = _size / 8;
- int bitIndex = _size % 8;
- char bitMask = 1 << bitIndex;
- _contents[byteIndex] |= bitMask;
- }
- // else, just increase size.
- _size++;
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::insert(int pos, const String<T> &x)
- {
- // create a temporary character array with old size plus size of the string
- // which has to be inserted.
- if (pos < 0 || pos > _size) return;
- char* _tmpContents = new char[_size + x.size()];
- for (int i = 0; i < pos; i++) _tmpContents[i] = _contents[i];
- for (int i = 0; i < x._size; i++) _tmpContents[pos + i] = x._contents[i];
- for (int i = pos; i < _size; i++)
- {
- _tmpContents[x._size + i] = _contents[i];
- }
- delete[] _contents;
- _size += x.size();
- _contents = _tmpContents;
- }
- // _____________________________________________________________________________
- void String<bool>::insert(int pos, const String<bool>& b)
- {
- if (pos < 0 || pos > _size) return;
- String<bool> tmp;
- // creating temporary String and appending old and new string to it.
- for (int i = 0; i < pos; i++)
- {
- tmp.append((*this)[i]);
- /*
- if ((*this)[i])
- {
- tmp.append(true);
- }
- else
- {
- tmp.append(false);
- }*/
- }
- for (int i = 0; i < b.size(); i++)
- {
- tmp.append(b[i]);
- /*
- if (b[i] == true)
- {
- tmp.append(true);
- }
- else
- {
- tmp.append(false);
- }*/
- }
- for (int i = pos; i < _size; i++)
- {
- tmp.append((*this)[i]);
- /*
- if ((*this)[i])
- {
- tmp.append(true);
- }
- else
- {
- tmp.append(false);
- }*/
- }
- _size += b.size();
- assert(_contents != NULL);
- delete[] _contents;
- _contents = new char[_size/8 + (_size%8 > 0)];
- int lengthOfNewArray = (_size/8 + (_size != 0));
- for (int i = 0; i < lengthOfNewArray; i++) _contents[i] = tmp._contents[i];
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::erase(int pos, int length)
- {
- // creates a temporary character array with
- // size minus 'erase-length'.
- // catch wrong input.
- if (pos < 0 || pos > _size) return;
- if ((pos + length) > _size) length = _size - pos;
- if (length == _size)
- {
- delete[] _contents;
- _contents = NULL;
- _size = 0;
- return;
- }
- char* _tmpContents = new char[_size - length];
- // copy all characters until position pos.
- for (int i = 0; i < pos; i++) _tmpContents[i] = _contents[i];
- // copy all characters after pos + length.
- for (int i = pos+length; i < _size; i++)
- {
- _tmpContents[i - length] = _contents[i];
- }
- // give the old memory free.
- delete[] _contents;
- // point to the new stuff.
- _contents = _tmpContents;
- _size -= length;
- }
- // _____________________________________________________________________________
- int String<bool>::size() const
- {
- return _size;
- }
- // _____________________________________________________________________________
- void String<bool>::erase(int pos, int length)
- {
- if (pos < 0 || pos > _size) return;
- if ((pos + length) > _size) length = _size - pos;
- if (length == _size)
- {
- delete[] _contents;
- _contents = NULL;
- _size = 0;
- return;
- }
- String<bool> tmp;
- for (int i= 0; i < pos; i++)
- {
- if ((*this)[i])
- {
- tmp.append(true);
- }
- else
- {
- tmp.append(false);
- }
- }
- for (int i = pos+length; i < _size; i++)
- {
- if ((*this)[i])
- {
- tmp.append(true);
- }
- else
- {
- tmp.append(false);
- }
- }
- _size -= length;
- assert(_contents != NULL);
- delete[] _contents;
- _contents = new char[_size/8 + (_size%8 != 0)];
- int lengthOfNewArray = _size/8 + (_size%8 != 0);
- for (int i = 0; i < lengthOfNewArray; i++) _contents[i] = tmp._contents[i];
- }
- // _____________________________________________________________________________
- template<class T>
- char String<T>::operator[](int n) const
- {
- return _contents[n];
- }
- // _____________________________________________________________________________
- bool String<bool>::operator[](int i) const
- {
- assert(i >= 0 && i < _size);
- int byteIndex = i / 8;
- int bitIndex = i % 8;
- char bitMask = 1 << bitIndex;
- return (_contents[byteIndex] & bitMask) == 0 ? false : true;
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::print()
- {
- for (int i = 0; i < _size; i++) printf("%c", _contents[i]);
- printf("\n");
- }
- // _____________________________________________________________________________
- void String<bool>::print() const
- {
- for (int i = 0; i < _size; i++) printf("%s ", (*this)[i] ? "1" : "0");
- printf("\n");
- }
- // _____________________________________________________________________________
- template<class T>
- bool String<T>::operator==(const String &s)
- {
- for (int i = 0; i < _size; i++)
- {
- // check if all characters are the same.
- if (_contents[i] != s._contents[i]) return false;
- }
- return true;
- }
- // _____________________________________________________________________________
- template<class T>
- int String<T>::find(int start, const String<T> &s)
- {
- // Catch wrong input.
- if (start < 0 || start >= _size) return -1;
- if (s._size > _size) return -1;
- if (s._size == 0) return -1;
- int i = start;
- int length = s.size();
- // starts at the given position.
- while (i < _size)
- {
- // if first letter of the input is found, check if its the word or just
- // another String with the same starting character.
- if (_contents[i] == s[0])
- {
- bool check = true;
- for (int j = 0; j < length; j++)
- {
- if (_contents[i + j] != s[j]) check = false;
- }
- if (check) return i;
- }
- i++;
- }
- // return -1 if found nothing.
- return -1;
- }
- /*
- // _____________________________________________________________________________
- void String::replace(const String& str, const String& other, String* resultStr)
- {
- resultStr = this;
- while (resultStr->find(0, str) != -1)
- {
- int start = resultStr->find(0, str);
- resultStr->erase(start, str.size());
- resultStr->insert(start, other);
- }
- }
- */
- // _____________________________________________________________________________
- template<class T>
- void String<T>::replace(const String<T>& str, const String<T>& other)
- {
- // Catch wrong input.
- if (_size == 0 || str._size == 0) return;
- // While str is still in the String.
- while (find(0, str) != -1)
- {
- int start = find(0, str);
- // delete str in the String.
- erase(start, str.size());
- // insert other at the deleted position.
- insert(start, other);
- }
- }
- // _____________________________________________________________________________
- template<class T>
- bool String<T>::getLine(FILE* file)
- {
- if (file == NULL) return false;
- char line[MAX_LINE_LENGTH + 1];
- fgets(line, MAX_LINE_LENGTH, file);
- if (feof(file)) return false;
- set(line);
- return true;
- }
- // _____________________________________________________________________________
- template<class T>
- void String<T>::printToFile(FILE* file)
- {
- if (file == NULL)
- {
- return;
- }
- else
- {
- fprintf(file, "%s", _contents);
- }
- }
- // Explicit instantiation
- template class String<char>;
- // template class String<bool>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement