Patched engine with non-constructing string

This commit is contained in:
David Anderson 2005-07-29 21:30:52 +00:00
parent 559d8bc7eb
commit fa46d2e1b5

View File

@ -40,7 +40,7 @@ public:
{ {
v = NULL; v = NULL;
a_size = 0; a_size = 0;
assign(""); //assign("");
} }
~String() ~String()
@ -69,13 +69,13 @@ public:
void append(const char *t) void append(const char *t)
{ {
Grow(strlen(v) + strlen(t) + 1); Grow(size() + strlen(t) + 1);
strcat(v, t); strcat(v, t);
} }
void append(const char c) void append(const char c)
{ {
size_t len = strlen(v); size_t len = size();
Grow(len + 2); Grow(len + 2);
v[len] = c; v[len] = c;
v[len + 1] = '\0'; v[len + 1] = '\0';
@ -104,17 +104,24 @@ public:
void clear() void clear()
{ {
v[0] = '\0'; if (v)
v[0] = '\0';
} }
int compare (const char *d) int compare (const char *d)
{ {
return strcmp(v, d); if (!v)
return strcmp("", d);
else
return strcmp(v, d);
} }
//Added this for amxx inclusion //Added this for amxx inclusion
bool empty() bool empty()
{ {
if (!v)
return false;
if (v[0] == '\0') if (v[0] == '\0')
return true; return true;
@ -123,19 +130,17 @@ public:
size_t size() size_t size()
{ {
return strlen(v); if (v)
} return strlen(v);
else
const char * _fread(FILE *fp) return 0;
{
Grow(512);
char * ret = fgets(v, 511, fp);
return ret;
} }
int find(const char c, int index = 0) int find(const char c, int index = 0)
{ {
size_t len = strlen(v); size_t len = size();
if (len < 1)
return npos;
if (index >= (int)len || index < 0) if (index >= (int)len || index < 0)
return npos; return npos;
unsigned int i = 0; unsigned int i = 0;
@ -164,6 +169,9 @@ public:
void trim() void trim()
{ {
if (!v)
return;
unsigned int i = 0; unsigned int i = 0;
unsigned int j = 0; unsigned int j = 0;
size_t len = strlen(v); size_t len = strlen(v);
@ -224,6 +232,8 @@ public:
void erase(unsigned int start, int num = npos) void erase(unsigned int start, int num = npos)
{ {
if (!v)
return;
unsigned int i = 0; unsigned int i = 0;
size_t len = size(); size_t len = size();
//check for bounds //check for bounds
@ -257,6 +267,9 @@ public:
String substr(unsigned int index, int num = npos) String substr(unsigned int index, int num = npos)
{ {
if (!v)
return String("");
String ns; String ns;
size_t len = size(); size_t len = size();
@ -284,6 +297,8 @@ public:
void toLower() void toLower()
{ {
if (!v)
return;
unsigned int i = 0; unsigned int i = 0;
size_t len = strlen(v); size_t len = strlen(v);
for (i=0; i<len; i++) for (i=0; i<len; i++)
@ -308,7 +323,7 @@ public:
char operator [] (unsigned int index) char operator [] (unsigned int index)
{ {
if (index > size()) if (index > size() || !v)
{ {
return -1; return -1;
} else { } else {
@ -318,7 +333,7 @@ public:
int at(int a) int at(int a)
{ {
if (a < 0 || a >= (int)size()) if (a < 0 || a >= (int)size() || !v)
return -1; return -1;
return v[a]; return v[a];
@ -326,7 +341,7 @@ public:
bool at(int at, char c) bool at(int at, char c)
{ {
if (at < 0 || at >= (int)size()) if (at < 0 || at >= (int)size() || !v)
return false; return false;
v[at] = c; v[at] = c;
@ -340,9 +355,10 @@ private:
if (d <= a_size) if (d <= a_size)
return; return;
char *n = new char[d + 1]; char *n = new char[d + 1];
if (copy) if (copy && v)
strcpy(n, v); strcpy(n, v);
delete [] v; if (v)
delete [] v;
v = n; v = n;
a_size = d + 1; a_size = d + 1;
} }