Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

String.cpp

Go to the documentation of this file.
00001 
00007 #include<stdlib.h>
00008 #include<string.h>
00009 #include<stdio.h>
00010 #include<ctype.h>
00011 #include<String.h>
00012 namespace clawsoft{
00013 
00014 int __str_shrink(char **buf){
00015         char *tmp;
00016         int n, i;
00017         if(*buf == 0)
00018                 return 0;
00019         n = strlen(*buf);
00020         tmp = new char[n + 1];
00021         for(i = 0; i < n; i++)
00022                 tmp[i] = *buf[i];
00023         tmp[n] = 0;
00024         claw_delete_array(buf);
00025         *buf = tmp;
00026         return n;
00027 }
00028 
00029 void int2char(int num, char buf[]){
00030         sprintf(buf, "%d", num);
00031         return;
00032         float tmp;
00033         int dec;
00034         int idx = 0;
00035         unsigned int flat;
00036         if(num < 0){
00037                 buf[idx++] = '-';
00038                 num = num * -1;
00039         }
00040         while(num != 0){
00041                 tmp = num / 10.0;
00042                 flat = num / 10;
00043                 dec = (int)((tmp - (float)flat) * 10);
00044                 buf[idx++] = dec + '0';
00045                 num = num / 10;
00046         }
00047         buf[idx] = 0;
00048 }
00049 
00050 const char *String::toCharPtr(){
00051         return buffer;
00052 }
00053 
00054 String::~String(){
00055         destroy();
00056 }
00057 
00058 String::String():Object("String"){
00059         buffer = 0;
00060         len = 0;
00061 }
00067 String::String(String &s):Object("String"){
00068         buffer = 0;
00069         len = 0;
00070         assign(s.buffer);
00071 }
00072 
00073 String::String(const char *s):Object("String"){
00074         buffer = 0;
00075         len = 0;
00076         assign(s);
00077 }
00078 
00079 String::String(const int n):Object("String"){
00080         buffer = 0;
00081         len = 0;
00082         assign(n);
00083 }
00084 
00085 String::String(const unsigned int n):Object("String"){
00086         buffer = 0;
00087         len = 0;
00088         assign(n);
00089 }
00090 
00091 String::String(const float n):Object("String"){
00092         buffer = 0;
00093         len = 0;
00094         assign(n);
00095 }
00096 
00097 String::String(const double n):Object("String"){
00098         buffer = 0;
00099         len = 0;
00100         assign(n);
00101 }
00102 
00103 String::String(const long double n):Object("String"){
00104         buffer = 0;
00105         len = 0;
00106         assign(n);
00107 }
00108 
00109 String::String(const long long n):Object("String"){
00110         buffer = 0;
00111         len = 0;
00112         assign(n);
00113 }
00114 
00115 
00116 void String::destroy(){
00117         if(buffer == 0)
00118                 return;
00119         claw_delete_array(buffer);
00120         len = 0;
00121 }
00122 
00123 void String::assign(const char *s){
00124         int n;
00125         if(s == 0)
00126                 return;
00127         if(s[0] == 0)
00128                 return;
00129         n = strlen(s);
00130         destroy();
00131         len = n;
00132         buffer = new char[n + 1];
00133         for(n = 0; n < len; n++){
00134                 buffer[n] = s[n];
00135         }
00136         buffer[n] = 0;
00137 }
00138 
00139 void String::assign(const int n){
00140         char *num = 0;
00141         num = new char[14];
00142         int2char(n, num);
00143         assign(num);
00144         claw_delete_array(num);
00145 }
00146 
00147 void String::assign(const unsigned int n){
00148         char *num = 0;
00149         num = new char[13];
00150         sprintf(num, "%u", n);
00151         assign(num);
00152         claw_delete_array(num);
00153 }
00154 
00155 void String::assign(const float n){
00156         char *num = 0;
00157         num = new char[80];
00158         sprintf(num, "%f", n);
00159         assign(num);
00160         claw_delete_array(num);
00161 }
00162 
00163 void String::assign(const double n){
00164         char *num = 0;
00165         num = new char[80];
00166         sprintf(num, "%f", n);
00167         assign(num);
00168         claw_delete_array(num);
00169 }
00170 
00171 void String::assign(const long double n){
00172         char *num = 0;
00173         num = new char[256];
00174         sprintf(num, "%Lf", n);
00175         assign(num);
00176         claw_delete_array(num);
00177 }
00178 
00179 void String::assign(const long long n){
00180         char *num = 0;
00181         num = new char[32];
00182         sprintf(num, "%lld", n);
00183         assign(num);
00184         claw_delete_array(num);
00185 }
00186 
00187 void String::concat(const char *s){
00188         int len_s, i, j;
00189         char *tmp;
00190         if(s == 0)
00191                 return;
00192         else
00193                 if(s[0] == 0)
00194                         return;
00195         if(buffer == 0){
00196                 assign(s);
00197                 return;
00198         }
00199         len_s = strlen(s);
00200         tmp = new char[len + len_s + 1];
00201         for(i = 0; i < len; i++){
00202                 tmp[i] = buffer[i];
00203         }
00204         for(j = 0; j < len_s; j++, i++){
00205                 tmp[i] = s[j];
00206         }
00207         tmp[i] = 0;
00208         claw_delete_array(buffer);
00209         assign(tmp);
00210         claw_delete_array(tmp);
00211 }
00212 
00213 void String::concat(String &s){
00214         concat(s.buffer);
00215 }
00216 
00217 void String::concat(const int n){
00218         char *buf;
00219         buf = new char[14];
00220         int2char(n, buf);
00221         concat(buf);
00222         claw_delete_array(buf);
00223 }
00224 
00225 void String::concat(const unsigned int n){
00226         char *buf;
00227         buf = new char[14];
00228         sprintf(buf, "%u", n);
00229         concat(buf);
00230         claw_delete_array(buf);
00231 }
00232 
00233 void String::concat(const float n){
00234         char *buf;
00235         buf = new char[256];
00236         sprintf(buf, "%f", n);
00237         concat(buf);
00238         claw_delete_array(buf);
00239 }
00240 
00241 void String::concat(const double n){
00242         char *buf;
00243         buf = new char[256];
00244         sprintf(buf, "%f", n);
00245         concat(buf);
00246         claw_delete_array(buf);
00247 }
00248 
00249 void String::concat(const long double n){
00250         char *buf;
00251         buf = new char[256];
00252         sprintf(buf, "%Lf", n);
00253         concat(buf);
00254         claw_delete_array(buf);
00255 }
00256 
00257 void String::concat(const long long n){
00258         char *buf;
00259         buf = new char[256];
00260         sprintf(buf, "%lld", n);
00261         concat(buf);
00262         claw_delete_array(buf);
00263 }
00264 
00265 String& String::operator=(const char *s){
00266         assign(s);
00267         return *this;
00268 }
00269 
00270 String& String::operator=(const int n){
00271         assign(n);
00272         return *this;
00273 }
00274 
00275 String& String::operator=(const unsigned int n){
00276         assign(n);
00277         return *this;
00278 }
00279 
00280 String& String::operator=(const float n){
00281                 assign(n);
00282         return *this;
00283 }
00284 
00285 String& String::operator=(const double n){
00286         assign(n);
00287         return *this;
00288 }
00289 
00290 String& String::operator=(const long double n){
00291         assign(n);
00292         return *this;
00293 }
00294 
00295 String& String::operator=(const long long n){
00296         assign(n);
00297         return *this;
00298 }
00299 
00300 String& String::operator+(String &s){
00301         concat(s);
00302         return *this;
00303 }
00304 
00305 String& String::operator+(const char *s){
00306         concat(s);
00307         return *this;
00308 }
00309 
00310 String& String::operator+(const int n){
00311         concat(n);
00312         return *this;
00313 }
00314 
00315 String& String::operator+(const unsigned int n){
00316         concat(n);
00317         return *this;
00318 }
00319 
00320 String& String::operator+(const float n){
00321         concat(n);
00322         return *this;
00323 }
00324 
00325 String& String::operator+(const double n){
00326         concat(n);
00327         return *this;
00328 }
00329 
00330 String& String::operator+(const long double n){
00331         concat(n);
00332         return *this;
00333 }
00334 
00335 String& String::operator+(const long long n){
00336         concat(n);
00337         return *this;
00338 }
00339 
00340 String& String::operator+=(String &s){
00341         concat(s);
00342         return *this;
00343 }
00344 
00345 String& String::operator+=(const char *s){
00346         concat(s);
00347         return *this;
00348 }
00349 
00350 String& String::operator+=(const int n){
00351         concat(n);
00352         return *this;
00353 }
00354 
00355 String& String::operator+=(const unsigned int n){
00356         concat(n);
00357         return *this;
00358 }
00359 
00360 String& String::operator+=(const float n){
00361         concat(n);
00362         return *this;
00363 }
00364 
00365 String& String::operator+=(const double n){
00366         concat(n);
00367         return *this;
00368 }
00369 
00370 String& String::operator+=(const long double n){
00371         concat(n);
00372         return *this;
00373 }
00374 
00375 String& String::operator+=(const long long n){
00376         concat(n);
00377         return *this;
00378 }
00379 
00380 std::ostream& operator<<(std::ostream& stream, String s){
00381         stream << s.buffer;
00382         return stream;
00383 }
00384 
00385 const char String::charAt(int i){
00386         if(i < 0 || i >= len)
00387                 throw ArrayOutOfBoundsException();
00388         return buffer[i];
00389 }
00390 
00391 const char String::operator[](int i){
00392         return charAt(i);
00393 }
00394 
00395 void String::deleteString(){
00396           destroy();
00397 }
00398 
00399 String &String::trimLeft(const char c){
00400         int i = 0;
00401         int j = 0;
00402         char *buf;
00403         if(buffer == 0){
00404                 throw NullPointerException();
00405                 return *this;
00406         }
00407         buf = new char[len + 1];
00408         while(buffer[i] == c) 
00409                 i++;
00410         for(j = 0; buffer[i] != 0; j++, i++)
00411                 buf[j] = buffer[i];
00412         buf[j] = 0;
00413         destroy();
00414         assign(buf);
00415         claw_delete_array(buf);
00416         return *this;
00417 }
00418 
00419 
00420 String &String::trimRight(const char c){
00421         int i;
00422         
00423         if(buffer == 0){
00424                 throw NullPointerException();  //hacer
00425                 return *this;
00426         }
00427 
00428         for(i = len - 1; buffer[i] == c; i--)
00429                 buffer[i] = 0;
00430         return *this;
00431 }
00432 
00433 String& String::trim(const char c){
00434         trimLeft(c);
00435         trimRight(c);
00436         return *this;
00437 }
00438 
00439 String& String::toUpperCase(){
00440         register int i;
00441         for(i = 0; i < len; i++){
00442                 buffer[i] = toupper(buffer[i]);
00443         }
00444         return *this;
00445 }
00446 
00447 String& String::toLowerCase(){
00448         register int i;
00449         for(i = 0; i < len; i++){
00450                 buffer[i] = tolower(buffer[i]);
00451         }
00452         return *this;
00453 }
00454 
00455 const unsigned int String::lenght(){
00456         return len;
00457 }
00458 
00459 const unsigned int String::size(){
00460         return len;
00461 }
00462 
00463 int __power(int n){
00464         int i, num = 1;
00465         for(i = 1; i <= n; i++){
00466                 num = num * 10;
00467         }
00468         return num;
00469 }
00470 
00471 const int String::toInt(){
00472         int num = 0, low = 0;
00473         int i, j, signo = 1;
00474         if(buffer == 0)
00475                 return 0;
00476         if(buffer[0] == 0)
00477                 return 0;
00478         if(buffer[0] == '+')
00479                 low = 1;
00480         if(buffer[0] == '-'){
00481                 low = 1;
00482                 signo = -1;
00483         }
00484         if(isdigit(buffer[0]) || buffer[0] == '+' || buffer[0] == '-'){
00485                 for(i = (len - 1), j = 0; i >= low; i--, j++){
00486                         if(!isdigit(buffer[i]))
00487                                 throw NumberException();
00488                         num = num + (buffer[i] - '0')* __power(j);
00489                 }
00490         }
00491         else
00492                 if(buffer[0] != '-' && buffer[0] != '+')
00493                         throw NumberException();
00494         return signo * num;
00495 }
00496 
00497 const unsigned int String::toUInt(){
00498         unsigned int num = 0, low = 0;
00499         unsigned int i, j;
00500         if(buffer == 0)
00501                 return 0;
00502         if(buffer[0] == 0)
00503                 return 0;
00504         if(buffer[0] == '+')
00505                 low = 1;
00506         if(isdigit(buffer[0]) || buffer[0] == '+'){
00507                 for(i = (len - 1), j = 0; i >= low; i--, j++){
00508                         if(!isdigit(buffer[i])){
00509                                 throw NumberException();
00510                         }
00511                         num = num + (buffer[i] - '0')* __power(j);
00512                 }
00513         }
00514         else
00515                 if(buffer[0] != '+'){
00516                         throw NumberException();
00517                 }
00518         return num;
00519 }
00520 
00521 const float String::toFloat(){
00522         return (float)toDouble();
00523 }
00524 
00525 const double String::toDouble(){
00526         int low = 0;
00527         int i, j;
00528         double num, numN, buf, sign = 1;
00529         char *integer;
00530         char *fractional;
00531         bool hasfrac = false;
00532         String Int;
00533         String Frac;
00534         if(buffer == 0)
00535                 return 0;
00536         if(buffer[0] == 0)
00537                 return 0;
00538         if(buffer[0] == '+')
00539                 low = 1;
00540         if(buffer[0] == '-'){
00541                 low = 1;
00542                 sign = -1;
00543         }
00544         integer = new char[len];
00545         fractional = new char[len];
00546         fractional[0] = 0;
00547         for(i = low, j = 0; buffer[i] != '\0'; i++){
00548                 if(buffer[i] == '.'){
00549                         i++;
00550                         hasfrac = true;
00551                         break;
00552                 }
00553                 else{
00554                         integer[j++] = buffer[i];
00555                 }
00556         }
00557         integer[j] = 0;
00558         if(hasfrac){
00559                 for(j = 0; buffer[i] != '\0'; j++){
00560                         fractional[j] = buffer[i++];
00561                 }
00562                 fractional[j] = 0;
00563         }
00564         Int = integer;
00565         numN = Int.toInt();
00566         if(hasfrac){
00567                 Frac = fractional;
00568                 num = Frac.toInt();
00569                 if(num != 0)
00570                         num = num / (float)__power(Frac.size());
00571         }
00572         else
00573                 num = 0;
00574         delete integer;
00575         delete fractional;
00576         buf = num + numN;
00577         return buf * sign;
00578 }
00579 
00580 const long double String::toLongDouble(){
00581         long long low = 0;
00582         long long i, j;
00583         long double num, numN, buf, sign = 1;
00584         char *integer;
00585         char *fractional;
00586         bool hasfrac = false;
00587         String Int;
00588         String Frac;
00589         if(buffer == 0)
00590                 return 0;
00591         if(buffer[0] == 0)
00592                 return 0;
00593         if(buffer[0] == '+')
00594                 low = 1;
00595         if(buffer[0] == '-'){
00596                 low = 1;
00597                 sign = -1;
00598         }
00599         integer = new char[len];
00600         fractional = new char[len];
00601         for(i = low, j = 0; buffer[i] != '\0'; i++){
00602                 if(buffer[i] == '.'){
00603                         i++;
00604                         hasfrac = true;
00605                         break;
00606                 }
00607                 else{
00608                         integer[j++] = buffer[i];
00609                 }
00610         }
00611         integer[j] = 0;
00612         if(hasfrac){
00613                 for(j = 0; buffer[i] != '\0'; j++){
00614                         fractional[j] = buffer[i++];
00615                 }
00616                 fractional[j] = 0;
00617         }
00618         Int = integer;
00619         numN = Int.toLongLong();
00620         if(hasfrac){
00621                 Frac = fractional;
00622                 num = Frac.toLongLong();
00623                 if(num != 0)
00624                         num = num / (float)__power(Frac.size());
00625         }
00626         else
00627                 num = 0;
00628         delete integer;
00629         delete fractional;
00630         buf = num + numN;
00631         return buf * sign;
00632 }
00633 
00634 const long long String::toLongLong(){
00635         long long num = 0, low = 0;
00636         long long i, j, signo = 1;
00637         if(buffer == 0)
00638                 return 0;
00639         if(buffer[0] == 0)
00640                 return 0;
00641         if(buffer[0] == '+')
00642                 low = 1;
00643         if(buffer[0] == '-'){
00644                 low = 1;
00645                 signo = -1;
00646         }
00647         if(isdigit(buffer[0]) || buffer[0] == '+' || buffer[0] == '-'){
00648                 for(i = (len - 1), j = 0; i >= low; i--, j++){
00649                         if(!isdigit(buffer[i]))
00650                                 throw NumberException();
00651                         num = num + (buffer[i] - '0')* __power(j);
00652                 }
00653         }
00654         else
00655                 if(buffer[0] != '-' && buffer[0] != '+')
00656                         throw NumberException();
00657         return signo * num;
00658 }
00659 
00660 bool String::operator==(String &s){
00661         return equals(s);
00662 }
00663 
00664 bool String::operator==(const char *s){
00665         return equals(s);
00666 }
00667 
00668 bool String::operator==(const int n){
00669         return equals(n);
00670 }
00671 bool String::operator==(const unsigned int n){
00672         return equals(n);
00673 }
00674 
00675 bool String::operator==(const float n){
00676         return equals(n);
00677 }
00678 
00679 bool String::operator==(const double n){
00680         return equals(n);
00681 }
00682 
00683 bool String::operator==(const long double n){
00684         return equals(n);
00685 }
00686 
00687 bool String::operator==(const long long n){
00688         return equals(n);
00689 }
00690 
00691 bool String::equals(String &s){
00692         unsigned int i;
00693         if(size() != s.size())
00694                 return false;
00695         for(i = 0; i < size(); i++)
00696                 if(charAt(i) != s[i])
00697                         return false;
00698         return true;
00699 }
00700 
00701 bool String::equals(const char *s){
00702         unsigned int i;
00703         unsigned int siz;
00704         if(s == 0)
00705                 return false;
00706         siz = strlen(s);
00707         if(size() != siz)
00708                 return false;
00709         for(i = 0; i < size(); i++)
00710                 if(charAt(i) != s[i])
00711                         return false;
00712         return true;
00713 }
00714 
00715 bool String::equals(const int n){
00716         String s(n);
00717         return equals(s);
00718 }
00719 
00720 bool String::equals(const unsigned int n){
00721         String s(n);
00722         return equals(s);
00723 }
00724 
00725 bool String::equals(const float n){
00726         String s(n);
00727         return equals(s);
00728 }
00729 
00730 bool String::equals(const double n){
00731         String s(n);
00732         return equals(s);
00733 }
00734 
00735 bool String::equals(const long double n){
00736         String s(n);
00737         return equals(s);
00738 }
00739 
00740 bool String::equals(const long long n){
00741         String s(n);
00742         return equals(s);
00743 }
00744 
00745 bool String::hasSubstring(String &s){
00746         if(buffer == 0)
00747                 return false;
00748         else
00749                 if(buffer[0] == 0)
00750                         return false;
00751         if(strstr(buffer, s.toCharPtr()) == NULL)
00752                 return false;
00753         return true;
00754 }
00755 
00756 bool String::hasSubstring(const char *s){
00757         if(buffer == 0)
00758                 return false;
00759         else
00760                 if(buffer[0] == 0)
00761                         return false;
00762         if(strstr(buffer, s) == NULL)
00763                 return false;
00764         return true;
00765 }
00766 
00767 String &String::deleteFirst(){
00768         unsigned int i;
00769         if(buffer == 0)
00770                 return *this;
00771         else{
00772                 if(buffer[0] == 0)
00773                         return *this;
00774                 else{
00775                         if(size() == 1){
00776                                 destroy();
00777                                 return *this;
00778                         }
00779                 }
00780         }
00781         for(i = 1; i < size(); i++){
00782                 buffer[i - 1] = buffer[i];
00783         }
00784         buffer[i - 1] = 0;
00785         len--;
00786         return *this;
00787 }
00788 
00789 String &String::deleteLast(){
00790         if(buffer == 0)
00791                 return *this;
00792         else{
00793                 if(buffer[0] == 0)
00794                         return *this;
00795                 if(size() == 1){
00796                         destroy();
00797                         cerr << "Destroyed" << endl;
00798                         return *this;
00799                 }
00800         }
00801         buffer[len - 1] = '\0';
00802         len--;
00803         return *this;
00804 }
00805 
00806 const int String::index(String &s, int start){
00807         unsigned int i, j;
00808         if(size() == 0)
00809                 return -1;
00810         if(s.size() == 0)
00811                 return -1;
00812         for(i = start; i < size() - s.size(); i++){
00813                 if(charAt(i) == s.charAt(0)){
00814                         for(j = 0; j < s.size(); j++){
00815                                 if(charAt(i + j) != s.charAt(j))
00816                                         continue;
00817                         }
00818                         return i;
00819                 }
00820         }
00821         return -1;
00822 }
00823 
00824 const int String::index(const char *s, int start){
00825         unsigned int i, j, ssize;
00826         if(size() == 0)
00827                 return -1;
00828         if(s == 0)
00829                 return -1;
00830         else
00831                 if(s[0] == 0)
00832                         return -1;
00833         ssize = strlen(s);
00834         for(i = start; i < size() - ssize; i++){
00835                 if(charAt(i) == s[0]){
00836                         for(j = 0; j < ssize; j++){
00837                                 if(charAt(i + j) != s[j])
00838                                         continue;
00839                         }
00840                         return i;
00841                 }
00842         }
00843         return -1;
00844 }
00845 
00846 const int String::index(const char c, int start){
00847         unsigned int i;
00848         if(size() == 0)
00849                 return -1;
00850         for(i = start; i < size(); i++){
00851                 if(charAt(i) == c)
00852                         return i;
00853         }
00854         return -1;
00855 }
00856 
00857 const int String::rindex(String &s, int end){
00858         int e;
00859         unsigned int i, j;
00860         if(size() == 0)
00861                 return -1;
00862         if(s == 0)
00863                 return -1;
00864         else
00865                 if(s[0] == 0)
00866                         return -1;
00867         if(end == 0)
00868                 e = size() - 1;
00869         else
00870                 if((unsigned int)end > (size() - s.size()))
00871                         return -1;
00872                 else
00873                         e = end;
00874         for(i = e; i >= 0; i--){
00875                 if(charAt(i) == s.charAt(0)){
00876                         for(j = 0; j < s.size(); j++){
00877                                 if(charAt(i + j) != s.charAt(j))
00878                                         continue;
00879                         }
00880                         return i;
00881                 }
00882         }
00883 }
00884 
00885 const int String::rindex(const char *s, int end){
00886         unsigned int i, j, ssize, e;
00887         if(size() == 0)
00888                 return -1;
00889         if(s == 0)
00890                 return -1;
00891         else
00892                 if(s[0] == 0)
00893                         return -1;
00894         ssize = strlen(s);
00895         if(end == 0)
00896                 e = size() - 1;
00897         else
00898                 if((unsigned int)end > (size() - ssize))
00899                         return -1;
00900                 else
00901                         e = end;
00902         for(i = e; i >= 0; i--){
00903                 if(charAt(i) == s[0]){
00904                         for(j = 0; j < ssize; j++){
00905                                 if(charAt(i + j) != s[j])
00906                                         continue;
00907                         }
00908                         return i;
00909                 }
00910         }
00911         return -1;
00912 }
00913 
00914 const int String::rindex(const char c, int end){
00915         unsigned int i;
00916         if(size() == 0)
00917                 return -1;
00918         if(end == 0)
00919                 end = len - 1;
00920         for(i = end; i >= 0; i--){
00921                 if(charAt(i) == c)
00922                         return i;
00923         }
00924         return -1;
00925 }
00926 
00927 bool String::startsWith(String &s){
00928         unsigned int i;
00929         if(size() == 0)
00930                 return false;
00931         if(s.size() > size())
00932                 return false;
00933         for(i = 0; i < s.size(); i++){
00934                 if(charAt(i) != s[i])
00935                         return false;
00936         }
00937         return true;
00938 }
00939 
00940 bool String::startsWith(const char *s){
00941         unsigned int i;
00942         unsigned ssize;
00943         if(s == 0)
00944                 return true;
00945         else
00946                 if(s[0] == 0)
00947                         return false;
00948         if(size() == 0)
00949                 return false;
00950         ssize = strlen(s);
00951         if(ssize > size())
00952                 return false;
00953         for(i = 0; i < ssize; i++){
00954                 if(charAt(i) != s[i])
00955                         return false;
00956         }
00957         return true;
00958 }
00959 
00960 bool String::endsWith(String &s){
00961         unsigned int i, j;
00962         if(size() == 0)
00963                 return false;
00964         if(s.size() > size())
00965                 return false;
00966         for(i = s.size() - 1, j = size() - 1; (int)i >= 0; i--, j--){
00967                 if(charAt(j) != s[i])
00968                         return false;
00969         }
00970         return true;
00971 }
00972 
00973 bool String::endsWith(const char *s){
00974         unsigned int i, j;
00975         unsigned ssize;
00976         if(s == 0)
00977                 return true;
00978         else
00979                 if(s[0] == 0)
00980                         return false;
00981         if(size() == 0)
00982                 return false;
00983         ssize = strlen(s);
00984         if(ssize > size())
00985                 return false;
00986         for(i = ssize - 1, j = size() - 1; (int)i >= 0; i--, j--){
00987                 if(charAt(j) != s[i])
00988                         return false;
00989         }
00990         return true;
00991 }
00992 
00993 String &String::deleteCharAt(const int pos){
00994         char *tmp;
00995         int i, j;
00996         if(size() == 0)
00997                 return *this;
00998         tmp = new char[size() + 1];
00999         for(i = 0, j = 0; i < (int)size(); i++){
01000                 if(i != pos)
01001                         tmp[j++] = charAt(i);
01002         }
01003         tmp[j] = '\0';
01004         len--;
01005         claw_delete_array(buffer);
01006         buffer = tmp;
01007         return *this;
01008 }
01009 
01010 String &String::insertCharAt(const int pos, const char c){
01011         char *tmp;
01012         int i, j;
01013         if(size() == 0)
01014                 return *this;
01015         tmp = new char[size() + 2];
01016         for(i = 0, j = 0; i < (int)size(); i++){
01017                 if(i == pos)
01018                         tmp[j++] = c;
01019                 tmp[j++] = charAt(i);
01020         }
01021         tmp[j] = '\0';
01022         len++;
01023         claw_delete_array(buffer);
01024         buffer = tmp;
01025         return *this;
01026 }
01027 };

Powered by:

SourceForge Logo