Advertisement
Guest User

String Class Implementation

a guest
May 4th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.55 KB | None | 0 0
  1. #include "lib_string.h"
  2.  
  3. abj::String::String(){
  4.   this->curr_size=0;
  5.   this->storage=NULL;
  6. }
  7.  
  8. abj::String::~String(){
  9.   if(this->storage!=NULL && this->curr_size!=0){
  10.     FILE* fptr = fopen(this->logFile, "a");
  11.     fprintf(fptr,"String destructor for \"%s\"\n",this->storage);
  12.     free(this->storage);
  13.     this->storage=NULL;
  14.     this->curr_size=0;
  15.     fclose(fptr);
  16.   }
  17. }
  18.  
  19. abj::String::String(std::string str){
  20.   int size = str.size();
  21.  
  22.   this->storage = (char*)calloc(size+1,sizeof(char));
  23.  
  24.   for(int i=0; i<size; i++) this->storage[i]=str[i];
  25.   this->storage[size]='\0';
  26.  
  27.   this->curr_size=size;
  28. }
  29.  
  30.  
  31. abj::String::String(const abj::String& data){
  32.   this->storage = (char*)calloc(data.size()+1,sizeof(char));
  33.  
  34.   for(int i=0; i<data.size(); i++) this->storage[i]=data.get(i);
  35.   this->storage[data.size()]='\0';
  36.  
  37.   this->curr_size = data.size();
  38. }
  39.  
  40. abj::String::String(const char* data){
  41.   int size = 0;
  42.   while(data[size]!='\0') size++;
  43.   //size--; // because it incremented extra-one-time at the end.
  44.  
  45.   this->storage = (char*)calloc(size+1,sizeof(char));
  46.   for(int i=0; i<size; i++) this->storage[i]=data[i];
  47.   this->storage[size]='\0';
  48.  
  49.   this->curr_size=size;
  50. }
  51.  
  52. abj::String::String(char* data, int FLAG){
  53.   int size=0;
  54.   while(data[size]!='\0') size++;
  55.   //size--;
  56.  
  57.   if(FLAG==USE_OLD_MEMORY){
  58.     this->curr_size = size;
  59.     this->storage = data;
  60.   }else if(FLAG==ALLOCATE_NEW_MEMORY){
  61.     this->storage = (char*)calloc(size+1, sizeof(char));
  62.     for(int i=0; i<size; i++) this->storage[i]=data[i];
  63.     this->storage[size]='\0';
  64.    
  65.     this->curr_size=size;
  66.    
  67.   }
  68.  
  69.   else{
  70.     printf("Unrecognized command! Initializing with NULL.\n");
  71.     this->storage=NULL;
  72.     this->curr_size=0;
  73.   }
  74. }
  75.  
  76.  
  77. int abj::String::size() const{
  78.   return this->curr_size;
  79. }
  80.  
  81. void abj::String::print() const{
  82.   printf("%s\n",this->storage);
  83. }
  84.  
  85. char abj::String::capitalize(char c) const{
  86.     int offset = 'a'-'A';
  87.     if(c>='a' && c<='z') c=c-offset;
  88.     return c;
  89. }
  90.  
  91. void abj::String::capitalize(){
  92.     for(int i=0; i<this->curr_size; i++) this->storage[i]=capitalize(this->storage[i]);
  93. }
  94.  
  95. char* abj::String::get_raw_data() const{
  96.   char* temp = (char*)calloc(this->curr_size+1, sizeof(char));
  97.  
  98.   for(int i=0; i<this->curr_size; i++) temp[i] = this->storage[i];
  99.   temp[this->curr_size]='\0';
  100.  
  101.   return temp;
  102. }
  103.  
  104. abj::String abj::String::copy(){
  105.   String t(*this);
  106.   return t;
  107. }
  108.  
  109. void abj::String::resize(int size){
  110.   size = std::max(size,this->curr_size+1);
  111.   char* new_memory = (char*)calloc(size,sizeof(char));
  112.   for(int i=0; i<this->curr_size; i++) new_memory[i]=this->storage[i];
  113.   new_memory[this->curr_size]='\0';
  114.   free(this->storage);
  115.   this->storage=NULL;
  116.  
  117.   this->storage = new_memory;
  118.   // this->curr_size = this->curr_size;
  119. }
  120.  
  121. void abj::String::concatenate_at_end(abj::String& data, char separator){
  122.   // Getting String Size
  123.   int old_size = this->curr_size;
  124.   this->resize(this->curr_size+data.size());
  125.  
  126.   this->storage[old_size]=separator;
  127.  
  128.   int j=0;
  129.   for(int i=old_size+1; j<data.size(); i++, j++)this->storage[i]=data[j];
  130.  
  131.   this->curr_size = old_size+data.size()+1; //+1 for separator
  132.   this->storage[old_size+data.size()+1]='\0';
  133. }
  134.  
  135. void abj::String::concatenate_at_end(abj::String& data){
  136.   // Getting String Size
  137.   int old_size = this->curr_size;
  138.   this->resize(this->curr_size+data.size());
  139.  
  140.   int j=0;
  141.   for(int i=old_size; j<data.size(); i++, j++)this->storage[i]=data[j];
  142.  
  143.   this->curr_size = old_size+data.size();
  144.   this->storage[old_size+data.size()+1]='\0';
  145. }
  146.  
  147.  
  148. bool abj::String::equals(abj::String& data) const{
  149.   if(data.size()!=this->curr_size) return false;
  150.  
  151.   for(int i=0; i<data.size(); i++){
  152.     if(data.get(i)!=this->storage[i]) return false;
  153.   }
  154.   return true;
  155. }
  156.  
  157. bool abj::String::equals(const char* data) const{
  158.   int data_size=0;
  159.   while(data[data_size]!='\0') data_size++;
  160.   // data_size--;
  161.  
  162.   if(data_size!=this->curr_size) return false;
  163.  
  164.   for(int i=0; i<data_size; i++){
  165.     if(data[i]!=this->storage[i]) return false;
  166.   }
  167.   return true;
  168. }
  169.  
  170.  
  171. // How it works
  172. // f("abhi paul", "jit', " ") = abhi  jit paul
  173. // f("abhi paul", "jit', "") = abhi jit paul
  174. // f("abhi paul", "jit', "-") = abhi -jit paul
  175. void abj::String::concatenate_at_point(abj::String& data, int index, char separator){
  176.   this->insert_char_at_point(index,separator);
  177.   for(int i=1; i<=data.size(); i++) this->insert_char_at_point(index+i, data[i-1]);
  178. }
  179. void abj::String::concatenate_at_point(abj::String& data, int index){
  180.   for(int i=0; i<data.size(); i++) this->insert_char_at_point(index+i, data[i]);
  181. }
  182.  
  183.  
  184. char abj::String::get(int index) const{
  185.   if(this->curr_size==0){
  186.     printf("Trying to access NULL string!\n");
  187.     return (char)NULL;
  188.   }
  189.   if(index<0 || index>=this->curr_size){
  190.     printf("String Index out of bound! (%s,%d)\n",this->storage, index);
  191.     return (char)NULL;
  192.     //exit(1);
  193.   }
  194.   return this->storage[index];
  195. }
  196.  
  197.  
  198. void abj::String::initialize(const abj::String& data){
  199.   this->storage = (char*)calloc(data.size()+1,sizeof(char));
  200.  
  201.   for(int i=0; i<data.size(); i++) this->storage[i]=data.get(i);
  202.   this->storage[data.size()]='\0';
  203.  
  204.   this->curr_size = data.size();
  205. }
  206.  
  207. void abj::String::initialize(char* data){
  208.   int size = 0;
  209.   while(data[size]!='\0') size++;
  210.   //size--;
  211.  
  212.   this->storage = (char*)calloc(size+1,sizeof(char));
  213.  
  214.   for(int i=0; i<size; i++) this->storage[i]=data[i];
  215.   this->storage[size]='\0';
  216.  
  217.   this->curr_size=size;
  218. }
  219.  
  220. void abj::String::initialize(const char* data){
  221.   int size = 0;
  222.   while(data[size]!='\0') size++;
  223.   //size--;
  224.  
  225.   this->storage = (char*)calloc(size+1,sizeof(char));
  226.  
  227.   for(int i=0; i<size; i++) this->storage[i]=data[i];
  228.   this->storage[size]='\0';
  229.  
  230.   this->curr_size=size;
  231. }
  232.  
  233.  
  234. void abj::String::removeData(){
  235.   this->curr_size=0;
  236.   free(this->storage);
  237.   this->storage=NULL;
  238. }
  239.  
  240. //0123
  241. //abhjit,3 -> abhijit
  242. void abj::String::insert_char_at_point(int index, char data){
  243.   resize(this->curr_size+2); //+1 for null, +! for new character
  244.   this->curr_size++;
  245.  
  246.   char oldCharacter = this->storage[index];
  247.   this->storage[index] = data;
  248.  
  249.   for(int i=index+1; i<this->curr_size; i++){
  250.     //swap
  251.     char temp = oldCharacter;
  252.     oldCharacter = this->storage[i];
  253.     this->storage[i] = temp;
  254.   }
  255. }
  256.  
  257.  
  258. char abj::String::lastChar() const{
  259.   return this->storage[this->curr_size-1];
  260. }
  261.  
  262.  
  263. bool abj::String::set(int index, char data){
  264.   if(index>=this->curr_size){ printf("Can't set(%d,%c). String index out of bound.\n",index,data); return false; };
  265.   if(data=='\0') this->curr_size=index;
  266.   this->storage[index]=data;
  267.   return true;
  268. }
  269.  
  270. char abj::String::operator[](int index) const{
  271.   if(index<0 || index>=this->curr_size){
  272.     printf("String index out of bound!\n");
  273.     return (char)NULL;
  274.   }
  275.   return this->storage[index];
  276. }
  277.  
  278. abj::String abj::String::operator+(const abj::String& data){
  279.   char* new_data = (char*)calloc(this->curr_size+data.size()+1, sizeof(char)); //+1 for NULL
  280.   for(int i=0; i<this->curr_size; i++) new_data[i]=this->storage[i];
  281.  
  282.   int j=0;
  283.   for(int i=this->curr_size; j<data.size(); i++, j++) new_data[i]=data[j];
  284.   new_data[this->curr_size+data.size()]='\0'; //5,4 -> 0-4, 5,6,7,8
  285.  
  286.   abj::String s(new_data);
  287.   return s;
  288. }
  289.  
  290. bool abj::String::operator==(const abj::String& data) const{
  291.   if(data.size()!=this->curr_size) return false;
  292.  
  293.   for(int i=0; i<data.size(); i++){
  294.     if(capitalize(data[i])!=capitalize(this->storage[i])) return false;
  295.   }
  296.   return true;
  297. }
  298.  
  299. abj::String& abj::String::operator=(const abj::String& data){
  300.   if(this!=&data){
  301.     abj::String temp(data);
  302.     this->swap(temp);
  303.   }
  304.   return *this;
  305. }
  306.  
  307. int abj::String::compare_string(char *str1, char *str2) const{
  308.   while( ( *str1 != '\0' && *str2 != '\0' ) && capitalize(*str1) == capitalize(*str2) ){
  309.         str1++;
  310.         str2++;
  311.     }
  312.  
  313.     if(*str1 == *str2) return 0; // strings are identical or found null for both
  314.     else return capitalize(*str1) - capitalize(*str2);
  315. }
  316. bool abj::String::operator>(const abj::String& data) const{
  317.   if(this->compare_string(this->storage, data.get_raw_data()) > 0) return true;
  318.   return false;
  319. }
  320. bool abj::String::operator<(const abj::String& data) const{
  321.   if(this->compare_string(this->storage, data.get_raw_data()) < 0) return true;
  322.   return false;
  323. }
  324.  
  325. void abj::String::setStorage(char* storage){
  326.   this->storage = storage;
  327. }
  328. char* abj::String::getStorage() const{
  329.   return this->storage;
  330. }
  331. void abj::String::setSize(int newSize){
  332.   this->curr_size = newSize;
  333. }
  334.  
  335.  
  336. void abj::String::swap(abj::String& data){
  337.   char* temp_stroage = data.getStorage();
  338.   data.setStorage(this->storage);
  339.   this->storage = temp_stroage;
  340.  
  341.   int temp_size = data.size();
  342.   data.setSize(this->size());
  343.   this->curr_size = temp_size;
  344. }
  345.  
  346. void abj::String::test_function(){
  347. printf("Testing String----------------\n");
  348.     abj::String x("I am Abhijit Paul.");
  349.     x.capitalize();
  350.     x.print();
  351.  
  352.     abj::String y("I am only human~");
  353.         y.capitalize();
  354.     printf("x==y? %d\n",y.equals(x));
  355.  
  356.     char str[] = "New Word.";
  357.     abj::String z(str);
  358.     z.print();
  359.  
  360.     x.concatenate_at_end(z, ':');
  361.     x.print();
  362.  
  363.     y.concatenate_at_point(z, y.size(), '_');
  364.     y.print();
  365.  
  366.     abj::String t;
  367.     t.concatenate_at_end(x, '_');
  368.     t.print();
  369.  
  370.     t.insert_char_at_point(t.size()-2, '!');
  371.     t.insert_char_at_point(t.size()-3, '~');
  372.     t.print();
  373.  
  374.     printf("New test cases.\n");
  375.  
  376.     abj::Vector<abj::String>v;
  377.     for(int i=0; i<10000; i++){
  378.       abj::String* newString = new abj::String("Random data is being loaded!");
  379.       v.push(*newString);
  380.     }
  381.  
  382.     for(int i=0; i<v.size(); i++) v[i].~String();
  383.  
  384.     abj::String* data1 = new abj::String();
  385.     data1->initialize("Data one.");
  386.     data1->print();
  387.     abj::String* data2 = new abj::String(*data1);
  388.     data2->concatenate_at_point(*new abj::String("Stuff1"),4,'_');
  389.     data2->print();
  390.     data2->concatenate_at_end(*new abj::String("I am only human~"), '.');
  391.     data2->print();
  392.     abj::String* data3 = new abj::String("Eat");
  393.     data3->print();
  394.     data3->concatenate_at_end(*new abj::String("ing."));
  395.     data3->print();
  396.     abj::String name("AbhiPaul");
  397.     name.concatenate_at_point(*new abj::String("jit "),4);
  398.     name.print();
  399.  
  400.     printf("Raw data=%sEND\n",name.get_raw_data());
  401.     abj::String name2("Abhijit Paul");
  402.     printf("Equal?%d\n",name.equals(name2));
  403.     printf("Equal?%d\n",name==name2);
  404.  
  405.     abj::String firstPart("Rubik's");
  406.     abj::String lastPart("Cube is fun!");
  407.     abj::String answer;
  408.     answer = firstPart + *new abj::String(" ") + lastPart;
  409.     answer.capitalize();
  410.     answer.print();
  411. }
  412.  
  413. // int main(){
  414. //   abj::String::test_function();
  415. //   return 0;
  416. // }
  417.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement