improved String

This commit is contained in:
2023-12-05 14:08:31 +03:00
parent 79cf5bc0da
commit 0bd2d3fe36

View File

@@ -4,223 +4,68 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <string>
class String { class String {
friend String operator+(const String& a, const String& b); friend String operator+(const String& a, const String& b);
long length; string body;
char* body;
public: public:
String() { String() { body = ""; }
length = 0; String(const char* s) { body = s; }
body = new char[1];
body[0] = '\0';
}
String(const char* s) {
length = (long)strlen(s);
body = new char[length + 1];
for (long i = 0; i < length; ++i)
body[i] = s[i];
body[length] = '\0';
}
String(const char* s, char ps) { String(const char* s, char ps) {
length = (long)strlen(s); body = s;
body = new char[length + 1]; for (long i = 0; i < getLength(); ++i)
for (long i = 0; i < length; ++i) {
body[i] = (s[i] == ps) ? '\n' : s[i]; body[i] = (s[i] == ps) ? '\n' : s[i];
} }
body[length] = '\0';
}
~String() {
if (body != NULL) {
delete[] body;
}
}
void println() const {
printf("[%s]\n", body);
}
void addChar(char c) {
char* buf = new char[length + 2];
for (long i = 0; i < length; ++i) {
buf[i] = body[i];
}
buf[length] = c;
length++; String(int s) { body = to_string(s); }
//-- String(long s) { body = to_string(s); }
buf[length] = '\0'; String(long long s) { body = to_string(s); }
delete[] body;
body = buf;
buf = NULL;
}
char* getCharArray() const {
return body;
}
void println() const { printf("[%s]\n", body.c_str()); }
String(int s) { void addChar(char c) { body += c; }
length = 0; const char* getCharArray() const { return body.c_str(); }
body = new char[1]; const string& getBody() const { return body; }
body[0] = '\0'; size_t getLength() const { return body.size(); }
if (s >= 0) { bool isEmpty() const { return body.size() != 0; }
int s_ = s; bool contains(const String& s) const { return body.find(s.getBody()) != string::npos; }
int size = 1; bool operator==(const String& s) const { return body == s.getBody(); }
while (s_ >= 10) {
s_ = s_ / 10;
size++;
}
length = size;
body = new char[size + 1];
sprintf(body, "%d", s);
}
}
String(long s) : String((long long)s) { }
String(long long s) {
length = 0;
body = new char[1];
body[0] = '\0';
if (s >= 0) {
long long s_ = s;
long size = 1;
while (s_ >= 10) {
s_ = s_ / 10;
size++;
}
length = size;
body = new char[size + 1];
sprintf(body, "%lld", s);
}
}
const String& operator=(const String& s) { const String& operator=(const String& s) {
if (body != NULL) body = s.getBody();
delete[] body;
length = s.length;
body = new char[length + 1];
for (long i = 0; i < length; ++i)
body[i] = s.body[i];
body[length] = '\0';
return *this; return *this;
} }
static String DQuotes(const String& s) { static String DQuotes(const String& s) {
String res; string tmp = '"' + s.getBody() + '"';
res.length = s.length + 2; return String(tmp.c_str());
res.body = new char[res.length + 1];
res.body[0] = '"';
res.body[res.length - 1] = '"';
res.body[res.length] = '\0';
for (long i = 0; i < s.length; ++i)
res.body[i + 1] = s.body[i];
return res;
} }
String Replace(char f, char t) { String Replace(char f, char t) {
String res; String res(body.c_str());
res.length = length; for (auto i = 0; i < getLength(); ++i)
res.body = new char[length];
res.body[length] = '\0';
for (long i = 0; i < length; ++i)
res.body[i] = (body[i] == f) ? t : body[i]; res.body[i] = (body[i] == f) ? t : body[i];
return res; return res;
} }
String Remove(char f) { String Remove(char f) {
String res; String res;
for (long i = 0; i < length; ++i) { for (auto i = 0; i < getLength(); ++i)
if (body[i] != f) if (body[i] != f)
res.addChar(body[i]); res.addChar(body[i]);
}
return res;
}
bool isEmpty() const {
return length == 0;
}
bool contains(const String& s) const {
bool res = false;
bool search_on = false;
if (s.isEmpty()) return true;
if (s.length > length) return false;
long k = 0;
long start = -1;
for (long i = 0; i < length; ++i) {
if (search_on) {
if (k < s.length) {
if (body[i] == s.body[k]) {
k++;
}
else {
//обрыв поиска.
search_on = false;
k = 0;
start = -1;
}
}
else {
//printf("starts with %ld", start);
return true;
}
}
else {
if (body[i] == s.body[0]) {
k = 1;
start = i;
search_on = true;
//printf("search started %d\n", start);
}
}
}
if (search_on) {
//printf("starts with %ld\n", start);
res = true;
}
return res; return res;
} }
String toUpper() { String toUpper() {
String res = String(this->getCharArray()); String res = String(this->getCharArray());
for (long i = 0; i < length; ++i) for (auto i = 0; i < getLength(); ++i)
res.body[i] = toupper(body[i]); res.body[i] = toupper(body[i]);
res.println(); res.println();
return res; return res;
} }
long getLength() {
return length;
}
bool operator==(const String& s) const {
if (length != s.length) return false;
for (long i = 0; i < length; ++i) {
if (body[i] != s.body[i]) return false;
}
return true;
}
/* регистр.
#include <stdio.h>
#include <ctype.h>
int main(void) {
FILE * f;
int c;
if ( ! ( f = fopen("file.txt", "r") ) )
return -1;
while ( ( c = fgetc(f) ) != EOF )
putchar( isupper(c) ? tolower(c) : toupper(c) );
return ( fclose(f) );
}
*/
}; };
String operator+(const String& a, const String& b) { String operator+(const String& a, const String& b) {
String res = String(); return String((a.getBody() + b.getBody()).c_str());
res.length = a.length + b.length;
res.body = new char[res.length + 1];
for (long i = 0; i < a.length; ++i)
res.body[i] = a.body[i];
for (long i = 0; i < b.length; ++i)
res.body[i + a.length] = b.body[i];
res.body[res.length] = '\0';
return res;
} }