String::String() {
data = new char[1];
data[0] = 0;
len = 0;
handle = true;
}
String::String(const char *str) {
if (str == NULL) {
data = new char[1];
data[0] = 0;
len = 0;
} else {
len = strlen(str);
data = new char[len + 1];
strcpy(data, str);
}
handle = true;
}
String::String(const String & other) {
handle = false;
data = other.data;
len = other.len;
}
String::String(String & other,bool willHandle) {
if (other.handle&&willHandle){
handle =true;
other.handle=false;
data = other.data;
len = other.len;
}else{
handle = true;
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
}
String::String(int num) {
handle = true;
char buff[8];
sprintf(buff, "%d", num);
len = strlen(buff);
data = new char[len + 1];
strcpy(data, buff);
}
String::String(float num) {
handle = true;
char buff[16];
sprintf(buff, "%f", num);
len = strlen(buff);
data = new char[len + 1];
strcpy(data, buff);
}
String::~String() {
if (data && handle) {
delete[]data;
data = NULL;
}
}
String & String::operator =(const String & other) {
this->~String();
handle = false;
data = other.data;
len = other.len;
return *this;
}
String String::operator+(const String & other) {
char buff[len + other.len + 1];
strcpy(buff, data);
strcat(buff, other.data);
return String(buff);
}
bool String::operator==(const String & other) {
return strcmp(data, other.data) == 0;
}
char &String::operator [] (unsigned int index) {
return *(data + index);
}
void String::operator+=(const String & other) {
if (this!=&other){
char buff[len + 1];
strcpy(buff, data);
this->~String();
handle = true;
len += other.len;
data = new char[len + 1];
strcpy(data, buff);
strcat(data, other.data);
}
}
void String::operator<<(String & other) {
if (&other != this) {
this->~String();
handle = true;
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
}
char *String::operator*() {
return data;
}
char *String::str() {
return data;
}
inline int String::lenth() {
return len;
}
void String::fill(char c) {
for (int i = 0; i < len; i++) {
*(data + i) = c;
}
}
inline int String::print() {
return printf(data);
}
inline int String::println() {
return puts(data);
}
data = new char[1];
data[0] = 0;
len = 0;
handle = true;
}
String::String(const char *str) {
if (str == NULL) {
data = new char[1];
data[0] = 0;
len = 0;
} else {
len = strlen(str);
data = new char[len + 1];
strcpy(data, str);
}
handle = true;
}
String::String(const String & other) {
handle = false;
data = other.data;
len = other.len;
}
String::String(String & other,bool willHandle) {
if (other.handle&&willHandle){
handle =true;
other.handle=false;
data = other.data;
len = other.len;
}else{
handle = true;
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
}
String::String(int num) {
handle = true;
char buff[8];
sprintf(buff, "%d", num);
len = strlen(buff);
data = new char[len + 1];
strcpy(data, buff);
}
String::String(float num) {
handle = true;
char buff[16];
sprintf(buff, "%f", num);
len = strlen(buff);
data = new char[len + 1];
strcpy(data, buff);
}
String::~String() {
if (data && handle) {
delete[]data;
data = NULL;
}
}
String & String::operator =(const String & other) {
this->~String();
handle = false;
data = other.data;
len = other.len;
return *this;
}
String String::operator+(const String & other) {
char buff[len + other.len + 1];
strcpy(buff, data);
strcat(buff, other.data);
return String(buff);
}
bool String::operator==(const String & other) {
return strcmp(data, other.data) == 0;
}
char &String::operator [] (unsigned int index) {
return *(data + index);
}
void String::operator+=(const String & other) {
if (this!=&other){
char buff[len + 1];
strcpy(buff, data);
this->~String();
handle = true;
len += other.len;
data = new char[len + 1];
strcpy(data, buff);
strcat(data, other.data);
}
}
void String::operator<<(String & other) {
if (&other != this) {
this->~String();
handle = true;
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
}
char *String::operator*() {
return data;
}
char *String::str() {
return data;
}
inline int String::lenth() {
return len;
}
void String::fill(char c) {
for (int i = 0; i < len; i++) {
*(data + i) = c;
}
}
inline int String::print() {
return printf(data);
}
inline int String::println() {
return puts(data);
}