Advertisement
Solingen

string.cpp

Dec 21st, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include "string.h"
  4.  
  5. class String;
  6.  
  7. String::String()
  8. {
  9.     len = 0;
  10.     str = new char[1];
  11.     str[0] = '\0';
  12. }
  13. String::String(const char* s)
  14. {
  15.     len = strlen(s);
  16.     str = new char[len + 1];
  17.     strcpy(str, s);
  18. }
  19. String::String(const String& other)
  20. {
  21.     len = other.len;
  22.     str = new char[len + 1];
  23.     strcpy(str, other.str);
  24. }
  25.  
  26. String& String::operator=(const String& other)
  27. {
  28.     if (this == &other)
  29.         return *this;
  30.  
  31.     delete[] str;
  32.     len = other.len;
  33.     str = new char[len + 1];
  34.     strcpy(str, other.str);
  35.     return *this;
  36. }
  37.  
  38. WordIterator String::split(const String& separators)
  39. {
  40.     WordIterator result;
  41.     char* p = this->str;
  42.     while(p)
  43.     {
  44.         p = skip_spaces(p, separators);
  45.         if (!*p)
  46.             break;
  47.        
  48.         char* word = p;
  49.         p = skip_until_spaces(p, separators);
  50.         int  len = p-word;
  51.         char* wordcc = new char[len+1];
  52.         strncpy(wordcc, word, len);
  53.         wordcc[len] = 0;
  54.         result.add(wordcc);
  55.         delete[] wordcc;
  56.     }
  57.     return result;
  58. }
  59. void WordIterator::checkReferences()
  60. {
  61.     (*refCount)--;
  62.     if ((*refCount) == 0)
  63.     {
  64.         while (start)
  65.         {
  66.             ListItem* tmp = start->next;
  67.             delete start;
  68.             start = tmp;
  69.         }
  70.         delete refCount;
  71.     }
  72. }
  73. void WordIterator::copyReferences(const WordIterator& other)
  74. {
  75.     this->words = other.words;
  76.     this->start = other.start;
  77.     this->count = other.count;
  78.     this->current = other.current;
  79.     this->refCount = other.refCount;
  80.     (*refCount)++;
  81. }
  82.  
  83. WordIterator& WordIterator::operator=(const WordIterator& other)
  84. {
  85.     checkReferences();  
  86.     copyReferences(other);
  87.     return *this;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement