Advertisement
Solingen

string.h

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