Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STRING_H
- #define STRING_H
- #include <iostream>
- #include <cstring>
- #include <fstream>
- class WordIterator;
- class String
- {
- private:
- int len;
- public:
- char* str;
- String();
- String(const char* s);
- String(const String& other);
- ~String()
- {
- delete[] str;
- str = nullptr;
- }
- String& operator=(const String& other);
- friend String operator+(const String& s1, const String& s2)
- {
- String result;
- result.len = s1.len + s2.len;
- result.str = new char[result.len + 1];
- strcpy(result.str, s1.str);
- strcat(result.str, s2.str);
- return result;
- }
- friend String operator+(const String& s1, char c)
- {
- String result;
- result.len = s1.len + 1;
- result.str = new char[result.len + 1];
- strcpy(result.str, s1.str);
- result.str[result.length() - 1] = c;
- return result;
- }
- friend bool operator==(const String& s1, const String& s2)
- {
- return strcmp(s1.str, s2.str) == 0;
- }
- friend bool operator!=(const String& s1, const String& s2)
- {
- return !(s1 == s2);
- }
- char& operator[](int index) const
- {
- return str[index];
- }
- int length() const
- {
- return len;
- }
- int indexOfChar(char c) const
- {
- for (int i = 0; i < len; i++)
- {
- if (c == str[i])
- return i;
- }
- return -1;
- }
- bool hasChar(char c) const
- {
- char* f = str;
- while(*f != '\0')
- {
- if(*f == c)
- {
- return true;
- }
- f++;
- }
- return false;
- }
- char* skip_spaces(char* s, const String& separators)
- {
- while(separators.hasChar(*s))
- {
- s++;
- }
- return s;
- }
- char* skip_until_spaces(char* s, const String& separators)
- {
- while(*s && !separators.hasChar(*s))
- {
- s++;
- }
- return s;
- }
- String substring(int start, int end) const
- {
- int len = end - start;
- char* buffer = new char(len + 1);
- for (int i = start; i < end; i++)
- {
- buffer[i - start] = str[i];
- }
- buffer[len] = '\0';
- return String(buffer);
- }
- int index(const String& subst, int start = 0)
- {
- if (start + 1 >= len) return -1;
- for (int i = start; i < len; i++)
- {
- if (str[i] == subst[0])
- {
- bool isSub = true;
- for (int j = 0; j < subst.length(); j++)
- {
- if (subst[j] == str[i + j])
- {
- continue;
- }
- else
- {
- isSub = false;
- break;
- }
- }
- if (isSub)
- {
- return i;
- }
- }
- }
- return -1;
- }
- /*
- friend std::istream& operator>>(std::istream& in, String& s)
- {
- char* buffer = new char;
- in >> buffer;
- s = String(buffer);
- delete buffer;
- return in;
- }*/
- friend std::ostream& operator<<(std::ostream& out, const String& s)
- {
- if (s.str == nullptr)
- {
- return out;
- }
- out << s.str;
- return out;
- }
- /*
- friend std::istream& operator>>(std::istream& in, String& s)
- {
- int i = 0;
- char* buffer = new char;
- while(1)
- {
- char c = in.get();
- if (c == '\n' || c == EOF) break;
- if (i >= strlen(buffer))
- {
- }
- s.str[i] = c; // very bad!e
- i++;
- }
- //s.str[i] = 0;
- return in;
- }*/
- friend std::istream& operator>>(std::istream& in, String& s)
- {
- int capacity = 2;
- int length = 0;
- char * buffer = (char*)malloc(capacity * sizeof(char));
- if (buffer == NULL)
- {
- printf("error with memory\n");
- exit(1);
- }
- int c;
- while((c = in.get()) != '\n' && c != EOF && !in.eof())
- {
- if (length == capacity - 1)
- {
- capacity *= 2;
- char * temp = (char*) malloc(capacity * sizeof(char));
- if (temp == NULL)
- {
- free(buffer);
- printf("error with memory\n");
- exit(1);
- }
- strncpy(temp, buffer, length);
- free(buffer);
- buffer = temp;
- }
- buffer[length] = c;
- length++;
- }
- buffer[length] = 0;
- s = String(buffer);
- free(buffer);
- return in;
- }
- bool operator<(const String& other) const
- {
- return strcmp(str, other.str) < 0;
- }
- WordIterator split(const String& separators);
- };
- struct ListItem
- {
- String str;
- ListItem* next = 0;
- };
- class WordIterator
- {
- public:
- ListItem* words;
- ListItem* start;
- int count = 0;
- ListItem* current;
- int* refCount = 0;
- WordIterator()
- {
- words = new ListItem();
- start = words;
- current = start;
- refCount = new int();
- (*refCount)++;
- }
- void checkReferences();
- void copyReferences(const WordIterator& other);
- WordIterator(WordIterator& other)
- {
- copyReferences(other);
- }
- void add(char* word)
- {
- words->str = word;
- words->next = new ListItem();
- words = words->next;
- count++;
- }
- int getCount() const
- {
- return count;
- }
- const String& getCurrent()
- {
- return current->str;
- }
- void setToBegin()
- {
- current = start;
- }
- bool moveNext()
- {
- current = current->next;
- if (current->str == "")
- return false;
- else
- return true;
- }
- void printList() const
- {
- int i = 0;
- ListItem* cur = start;
- while(true)
- {
- if (i == count)
- break;
- std::cout << "[" << i << "] " << cur->str << std::endl;
- cur = cur->next;
- i++;
- }
- }
- ~WordIterator()
- {
- checkReferences();
- }
- WordIterator& operator=(const WordIterator& other);
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement