Advertisement
Solingen

string.h

Nov 1st, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4.  
  5. class WordIterator;
  6.  
  7. class String
  8. {
  9. private:
  10.     int len;
  11.  
  12. public:
  13.     char* str;
  14.     String();
  15.  
  16.     String(const char* s);
  17.    
  18.     String(const String& other);
  19.  
  20.     ~String()
  21.     {
  22.         delete[] str;
  23.         str = nullptr;
  24.     }
  25.  
  26.     String& operator=(const String& other);
  27.  
  28.     friend String operator+(const String& s1, const String& s2)
  29.     {
  30.         String result;
  31.         result.len = s1.len + s2.len;
  32.         result.str = new char[result.len + 1];
  33.         strcpy(result.str, s1.str);
  34.         strcat(result.str, s2.str);
  35.         return result;
  36.     }
  37.  
  38.     friend String operator+(const String& s1, char c)
  39.     {
  40.         String result;
  41.         result.len = s1.len + 1;
  42.         result.str = new char[result.len + 1];
  43.         strcpy(result.str, s1.str);
  44.         result.str[result.length() - 1] = c;
  45.         return result;
  46.     }
  47.  
  48.     friend bool operator==(const String& s1, const String& s2)
  49.     {
  50.         return strcmp(s1.str, s2.str) == 0;
  51.     }
  52.  
  53.     friend bool operator!=(const String& s1, const String& s2)
  54.     {
  55.         return !(s1 == s2);
  56.     }
  57.  
  58.     char& operator[](int index)
  59.     {
  60.         return str[index];
  61.     }
  62.  
  63.     int length() const
  64.     {
  65.         return len;
  66.     }
  67.  
  68.     int indexOfChar(char c) const
  69.     {
  70.         for (int i = 0; i < len; i++)
  71.         {
  72.             if (c == str[i])
  73.                 return i;
  74.         }
  75.         return -1;
  76.     }
  77.     bool hasChar(char c) const
  78.     {
  79.         char* f = str;
  80.         while(*f != '\0')
  81.         {
  82.             if(*f == c)
  83.             {
  84.                 return true;
  85.             }
  86.            
  87.             f++;
  88.         }
  89.        
  90.         return false;
  91. }
  92.  
  93.     char* skip_spaces(char* s, const String& separators)
  94.     {
  95.         while(separators.hasChar(*s))
  96.         {
  97.             s++;
  98.         }
  99.        
  100.         return s;
  101.     }
  102.  
  103.     char* skip_until_spaces(char* s, const String& separators)
  104.     {
  105.         while(*s && !separators.hasChar(*s))
  106.         {
  107.             s++;
  108.         }
  109.        
  110.         return s;
  111.     }
  112.  
  113.     String substring(int start, int end) const
  114.     {
  115.         int len = end - start;
  116.         char* buffer = new char(len + 1);
  117.         for (int i = start; i < end; i++)
  118.         {
  119.             buffer[i - start] = str[i];
  120.         }
  121.         buffer[len] = '\0';
  122.         return String(buffer);
  123.     }
  124.  
  125.     /*
  126.     friend std::istream& operator>>(std::istream& in, String& s)
  127.     {
  128.         char* buffer = new char;
  129.         in >> buffer;
  130.         s = String(buffer);
  131.         delete buffer;
  132.         return in;
  133.     }*/
  134.  
  135.     friend std::ostream& operator<<(std::ostream& out, const String& s)
  136.     {
  137.         if (s.str == nullptr)
  138.         {
  139.             return out;
  140.         }
  141.         out << s.str;
  142.         return out;
  143.     }
  144.     /*
  145.     friend std::istream& operator>>(std::istream& in, String& s)
  146.     {
  147.         int i = 0;
  148.         char* buffer = new char;
  149.         while(1)
  150.         {
  151.             char c = in.get();
  152.             if (c == '\n' || c == EOF) break;
  153.             if (i >= strlen(buffer))
  154.             {
  155.  
  156.             }
  157.             s.str[i] = c; // very bad!e
  158.             i++;        
  159.         }
  160.         //s.str[i] = 0;
  161.        
  162.         return in;
  163.     }*/
  164.     friend std::istream& operator>>(std::istream& in, String& s)
  165.     {
  166.         int capacity = 2;
  167.         int length = 0;
  168.         char * buffer = (char*)malloc(capacity * sizeof(char));
  169.         if (buffer == NULL)
  170.         {
  171.             printf("error with memory\n");
  172.             exit(1);
  173.         }
  174.         int c;
  175.         while((c = in.get()) != '\n' && c != EOF && !in.eof())
  176.         {
  177.             if (length == capacity - 1)
  178.             {
  179.                 capacity *= 2;
  180.                 char * temp = (char*) malloc(capacity * sizeof(char));
  181.                 if (temp == NULL)
  182.                 {
  183.                     free(buffer);
  184.                     printf("error with memory\n");
  185.                     exit(1);
  186.                 }
  187.  
  188.                 strncpy(temp, buffer, length);
  189.                 free(buffer);
  190.                 buffer = temp;
  191.             }
  192.                         buffer[length] = c;
  193.             length++;
  194.         }
  195.         buffer[length] = 0;
  196.         s = String(buffer);
  197.         free(buffer);
  198.         return in;
  199.     }
  200.     bool operator<(const String& other) const
  201.     {
  202.         return strcmp(str, other.str) < 0;
  203.     }
  204.  
  205.     WordIterator split(const String& separators);
  206. };
  207.  
  208. struct ListItem
  209. {
  210.     String str;
  211.     ListItem* next = 0;
  212. };
  213.  
  214. class WordIterator
  215. {
  216.     ListItem* words;
  217.     ListItem* start;
  218.     int count = 0;
  219.     ListItem* current;
  220.     public:
  221.     int* refCount = 0;
  222.     WordIterator()
  223.     {
  224.         words = new ListItem();
  225.         start = words;
  226.         current = start;
  227.         refCount = new int();
  228.         (*refCount)++;
  229.     }
  230.     WordIterator(const WordIterator& other)
  231.     {
  232.         this->words = other.words;
  233.         this->start = other.start;
  234.         this->count = other.count;
  235.         this->current = other.current;
  236.         this->refCount = other.refCount;
  237.         (*refCount)++;
  238.     }
  239.     void add(char* word)
  240.     {
  241.         words->str = word;
  242.         words->next = new ListItem();
  243.         words = words->next;
  244.         count++;
  245.     }
  246.     int getCount() const
  247.     {
  248.         return count;
  249.     }
  250.     const String& getCurrent()
  251.     {
  252.         return current->str;
  253.     }
  254.     void setToBegin()
  255.     {
  256.         current = start;
  257.     }
  258.     bool moveNext()
  259.     {
  260.         current = current->next;
  261.         if (current->str == "")
  262.             return false;
  263.         else
  264.             return true;
  265.     }
  266.     void printList() const
  267.     {
  268.         int i = 0;
  269.         ListItem* cur = start;
  270.         while(true)
  271.         {
  272.             if (i == count)
  273.                 break;
  274.             std::cout << "[" << i << "] " << cur->str << std::endl;
  275.             cur = cur->next;
  276.             i++;
  277.         }
  278.     }
  279.     ~WordIterator()
  280.     {
  281.         (*refCount)--;
  282.         if ((*refCount) == 0)
  283.         {
  284.             while (start)
  285.             {
  286.                 ListItem* tmp = start->next;
  287.                 delete start;
  288.                 start = tmp;
  289.             }
  290.             delete refCount;
  291.         }
  292.     }
  293. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement