Message-ID: <394A8433.FDE909DD@programmingparadise.com> Date: Sat, 17 Jun 2000 01:17:01 +0530 From: Vikas Yadav Organization: programmingParadise.com X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en MIME-Version: 1.0 To: DJGPP Subject: class String has problem Content-Type: multipart/mixed; boundary="------------F07E21CD207FAF2F71D2F43E" Reply-To: djgpp AT delorie DOT com This is a multi-part message in MIME format. --------------F07E21CD207FAF2F71D2F43E Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit #define MAX_STRING 0x4000 #ifndef min #define min(x,y) ( x > y ? y : x ) #endif class String { /////////////////////////////////// //--------------------------------- // Hello evrybody. // I had written this class about 6 months before; // Everything is running perfect, but this does not look // to be a healthy code; // // What is a *better alternative* that this big stack; // // 16K char ptr1[MAX_STRING]; // This ptr1 is used in function like right()/substring() etc. // The caller need not supply the extra parameter like // ....,char * dest, int dest_size){ // Free from these *extra parameters* make the coding really very easy // at least for me. :-) // But this seems to be the price I have to pay. // I have also defined a heap buffer below // *which actually holds the string*. This heapbuffer gets resized // depending on the new string it gets. etc.. etc.. // Due to this HUGE stack "ptr1" I SHOULD not do this: // String s ="hello world\n"; //insted // String *s = new String("hellow world\n"); // as this is far more safer. // --Sometimes this huge stack DID actually crash a program; // --This stack also PUTS A LIMIT TO MAXIMUM POSSIBLE string size; // --A complete waste of memory // When you use TC/BCC this will not even compile. // DJGPP or MSVC do not mind that BIG thing at all. // My coding style is not at at all good but // *it works perfect*. // // Any suggestions to improve this class and to remove this // BOMB from here? // // Thank you very much! // Vikas Yadav //--------------------------------- /////////////////////////////////// public: char *str1; ////////////////////////////////// String() { str1 = NULL; } ////////////////////////////////// String(char*x1) { str1 = NULL; if(!x1) return; str1=new char[strlen(x1)+1]; strcpy(str1,x1); } ////////////////////////////////// ~String() { if(str1 && *str1) { delete str1; str1=NULL; } } ////////////////////////////////// char* operator=(void*x1) { if(str1) { delete str1; str1=NULL; } if(!x1) return (char*)x1; //alloc=strlen((const char*)x1)+2; //DPrint("=: Allocated %lu for \"%s\"(%lu)",alloc,x1,strlen((const char*)x1)); str1=strdup((const char*)x1); return (char*)x1; } ////////////////////////////////// char* operator+=(void* x1) { int l1; if(!x1) return (char*)str1; unsigned long alloc = l1 = strlen(str1) + strlen((char*)x1) + 2; char *str2 = new char[alloc]; ::sprintf(str2,"%s%s",str1,x1); if(str1) { delete str1; str1=NULL; } str1=str2; return str1; } ////////////////////////////////// char* operator+=(String &x1) { int l1; unsigned long alloc= l1 = strlen(str1) + strlen(x1.str1) + 1 ; char *str2 = new char[alloc]; ::sprintf(str2,"%s%s",str1,x1.str1); if(str1) { delete str1; str1=NULL; } str1=str2; return str1; } ////////////////////////////////// char* right(int r1) { if(!str1) return NULL; strcpy(ptr1,str1); return (ptr1 + ( strlen(ptr1) - r1)); } ////////////////////////////////// char* left(int r1) { if(!str1) return NULL; strcpy(ptr1,str1); ptr1[r1]=0; return ptr1; } ////////////////////////////////// short operator==(const char*x1) { if(!str1 && !strlen(x1)) return 1; return strcmp(str1,x1)==0; } ////////////////////////////////// short operator!=(const char*x1) { if(!str1 && !strlen(x1)) return 0; return strcmp(str1,x1)!=0; } ////////////////////////////////// int length() { if(!str1) return 0; return strlen(str1); } ////////////////////////////////// void toUpperCase() { if(!str1) return; unsigned long len = strlen( str1 ); for( char *p = str1; p < (str1 + len); p++ ) { if( islower( *p ) ) *p = (char)toupper( *p ); } } ////////////////////////////////// void toLowerCase() { if(!str1) return; unsigned long len = strlen( str1 ); for( char *p = str1; p < (str1 + len); p++ ) { if( isupper( *p ) ) *p =(char)tolower( *p ); } } ////////////////////////////////// char* operator+(char*x1) { if(!x1) return (char*)str1; ::sprintf(ptr1,"%s%s",str1,x1); return ptr1; } ////////////////////////////////// // This operator is also a BUG. // I want to use the String class something like this // String s; // fscanf(infile,1,255,s); // This is not possible at current state. // "char*" operator should be clever enough. // /* operator char*() * { * strcpy(ptr1,str1); * delete str1; * str1=new char[MAX_STRING]; * strcpy(str1,ptr1); * return str1; * } */ ////////////////////////////////// long lastIndexOf(char x1) { if(!str1) return -1; long c1=-1; for(c1=length(); c1>0; c1--) if( str1[c1] == x1 ) return c1; return c1; } ////////////////////////////////// long indexOf(char x1) { if(!str1) return -1; long c1=-1,c2=length(); for(c1=0; c1