Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

String.cpp

Go to the documentation of this file.
00001 
00007 #include "String.h"
00008 
00009 int getAppropriateCapasity(unsigned int capasity, unsigned int min){
00010         do {
00011                 capasity <<= 1;
00012         } while (capasity < min);
00013         return capasity;
00014 }
00015 
00016 String::String() {
00017         length = 0;
00018         str = (char *)malloc(capasity = 32);
00019         str[0] = '\0';
00020 }
00021 
00022 String::String(unsigned int cap) {
00023         length = 0;
00024         str = (char *)malloc(capasity = cap);
00025         str[0] = '\0';
00026 }
00027 
00028 String::String(const char *string) {
00029         length = strlen(string);
00030         str = (char *)malloc(capasity = length + 1);
00031         strncpy(str, string, capasity);
00032 }
00033 
00034 String::String(const char *string, unsigned int len) {
00035         length = len;
00036         str = (char *)malloc(capasity = len + 1);
00037         strncpy(str, string, len);
00038         str[len] = '\0';
00039 }
00040 
00041 String::String(const String &string) {
00042         length = string.length;
00043         capasity = string.capasity;
00044         str = (char *)malloc(capasity);
00045         str = (char *)malloc(capasity);
00046         strncpy(str, string.str, length + 1);
00047 }
00048 
00049 String::~String() {
00050         free(str);
00051 }
00052 
00053 
00054 void String::setLength(const unsigned int newLength) {
00055         str = (char *)realloc(str, capasity = newLength + 1);
00056         str[length = newLength] = '\0';
00057 }
00058 
00059 void String::assign(const char *string, const unsigned int len) {
00060         if (len + 1 >= capasity) {
00061                 capasity = getAppropriateCapasity(capasity, len + 1);
00062                 free(str);
00063                 str = (char *)malloc(capasity);
00064         }
00065 
00066         strncpy(str, string, len);
00067         str[len] = '\0';
00068         length = len;
00069 }
00070 
00071 void String::append(const char *string, const unsigned int len) {
00072         unsigned int newLength = length + len;
00073         if (newLength + 1 >= capasity) {
00074                 capasity = getAppropriateCapasity(capasity, newLength + 1);
00075                 str = (char *)realloc(str, capasity);
00076         }
00077 
00078         strncpy(str+length, string, len);
00079         str[newLength] = '\0';
00080         length = newLength;
00081 }
00082 
00083 bool String::insert(const unsigned int pos, const char *string, const unsigned int len) {
00084         if (pos > length)
00085                 return false;
00086 
00087         unsigned int newLength = length + len;
00088         if (newLength + 1 >= capasity) {
00089                 capasity = getAppropriateCapasity(capasity, newLength + 1);
00090                 str = (char *)realloc(str, capasity);
00091         }
00092 
00093         unsigned int n = length - pos;
00094         for (unsigned int i=0; i<=n; i++) {
00095                 str[newLength-i] = str[length-i];
00096         }
00097         strncpy(str+pos, string, len);
00098         length = newLength;
00099         return true;
00100 }
00101 
00102 bool String::remove(const unsigned int pos, const unsigned int len) {
00103         if (pos + len > length) return false;
00104 
00105         length -= len;
00106         unsigned int n = length - pos;
00107         for (unsigned int i=0; i<=n; i++) {
00108                 str[pos+i] = str[pos+len+i];
00109         }
00110         return true;
00111 }
00112 
00113 void String::operator = (const char *string) {
00114         assign(string, strlen(string));
00115 }
00116 
00117 void String::operator = (const String &string) {
00118         assign(string.str, string.length);
00119 }
00120 
00121 void String::operator += (const char *string) {
00122         append(string, strlen(string));
00123 }
00124 
00125 void String::operator += (const String &string) {
00126         append(string.str, string.length);
00127 }

Generated on Sun Jun 5 15:47:05 2005 for Defacto by  doxygen 1.4.3