Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include "string.h"
- class String;
- String::String()
- {
- len = 0;
- str = new char[1];
- str[0] = '\0';
- }
- String::String(const char* s)
- {
- len = strlen(s);
- str = new char[len + 1];
- strcpy(str, s);
- }
- String::String(const String& other)
- {
- len = other.len;
- str = new char[len + 1];
- strcpy(str, other.str);
- }
- String& String::operator=(const String& other)
- {
- if (this == &other)
- return *this;
- delete[] str;
- len = other.len;
- str = new char[len + 1];
- strcpy(str, other.str);
- return *this;
- }
- WordIterator String::split(const String& separators)
- {
- WordIterator result;
- char* p = this->str;
- while(p)
- {
- p = skip_spaces(p, separators);
- if (!*p)
- break;
- char* word = p;
- p = skip_until_spaces(p, separators);
- int len = p-word;
- char* wordcc = new char[len+1];
- strncpy(wordcc, word, len);
- wordcc[len] = 0;
- result.add(wordcc);
- delete[] wordcc;
- }
- return result;
- }
- void WordIterator::checkReferences()
- {
- (*refCount)--;
- if ((*refCount) == 0)
- {
- while (start)
- {
- ListItem* tmp = start->next;
- delete start;
- start = tmp;
- }
- delete refCount;
- }
- }
- void WordIterator::copyReferences(const WordIterator& other)
- {
- this->words = other.words;
- this->start = other.start;
- this->count = other.count;
- this->current = other.current;
- this->refCount = other.refCount;
- (*refCount)++;
- }
- WordIterator& WordIterator::operator=(const WordIterator& other)
- {
- checkReferences();
- copyReferences(other);
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement