/**** * * Implementation of String. * */ #include "std-macros.h" #include #if MACOSX || gcc_3_1 #include #endif #include #include #include "str.h" String::String(const char* s) { Init(s); } String::String(int i) { int size; char* s; if (i == 0) size = 1; else if (i < 0) size = ((int)log10((double)(abs(i))) + 3); else size = ((int)log10((double)i) + 2); s = new char[size]; sprintf(s, "%d", i); Init((const char*) s); delete[] s; } void String::Init(const char* s) { if (s) { len = strlen(s); data = strcpy(new char[len + 1], s); } else { len = 0; data = new char[1]; data[0] = '\0'; } } String::String() : data(null) {} String::~String() { delete data; } int String::Len() { return len; } int String::Compare(String* s) { if (s) return strcmp(data, s->data); else return 1; } int String::Compare(char* s) { if (s) return strcmp(data, s); else return 1; } bool String::operator==(String& s) { return Compare(&s) == 0; } bool String::operator==(char* cs) { return Compare(cs) == 0; } bool String::operator<(String& s) { return Compare(&s) < 0; } bool String::operator<(char* cs) { return Compare(cs) < 0; } bool String::operator>(String& s) { return Compare(&s) > 0; } bool String::operator>(char* cs) { return Compare(cs) > 0; } bool String::operator<=(String& s) { return Compare(&s) <= 0; } bool String::operator<=(char* cs) { return Compare(cs) <= 0; } bool String::operator>=(String& s) { return Compare(&s) >= 0; } bool String::operator>=(char* cs) { return Compare(cs) >= 0; } String* String::Concat(String* s) { String* newstr; int newlen; if (not s) return Copy(); newstr = new String(); newlen = len + s->len; newstr->data = strcpy(new char[newlen + 1], this->data); strcat(newstr->data, s->data); newstr->len = newlen; return newstr; } String* String::Concat(char* cs) { String* newstr; int newlen; if (not cs) return Copy(); newstr = new String(); newlen = len + strlen(cs); newstr->data = strcpy(new char[newlen + 1], this->data); strcat(newstr->data, cs); newstr->len = newlen; return newstr; } String* String::Prepend(String* s) { String* newstr; int newlen; if (not s) return Copy(); newstr = new String(); newlen = len + s->len; newstr->data = strcpy(new char[newlen + 1], s->data); strcat(newstr->data, this->data); newstr->len = newlen; return newstr; } String* String::Prepend(char* cs) { String* newstr; int newlen; if (not cs) return Copy(); newstr = new String(); newlen = len + strlen(cs); newstr->data = strcpy(new char[newlen + 1], cs); strcat(newstr->data, this->data); newstr->len = newlen; return newstr; } String* String::Copy() { return new String(data); } String* String::Substring(int startpos, int endpos) { String* newstr; int sublen; if ((startpos < 1) or (endpos > len) or (startpos > endpos)) return new String(""); newstr = new String(); sublen = endpos - startpos + 1;; newstr->data = strncpy(new char[sublen + 1], &(this->data[startpos - 1]), sublen); newstr->data[sublen] = '\0'; newstr->len = sublen; return newstr; } char String::GetNthChar(int n) { if ((n > len) or (n < 1)) return '\0'; return data[n - 1]; } char String::operator[](int n) { return GetNthChar(n); } char* String::Convert() { char* newstr; if (not this) { newstr = new char[1]; newstr[0] = '\0'; return newstr; } return strcpy(new char[len +1], data); } const char* String::ConstConvert() { return data; } char* String::EvilConvert() { return data; } String* String::operator+(String& s) { return Concat(&s); } String* String::operator+(char* cs) { return Concat(cs); } String* operator+(char* cs, String& s) { return s.Prepend(cs); } bool operator==(char* cs, String& s) { return s == cs; } bool operator<(char* cs, String& s) { return s >= cs; } bool operator>(char* cs, String& s) { return s <= cs; } bool operator<=(char* cs, String& s) { return s > cs; } bool operator>=(char* cs, String& s) { return s < cs; }