Added 2 append_path helper methods to string, and 2 append_str variants.

This commit is contained in:
Relintai 2022-02-05 14:53:19 +01:00
parent bfec926e60
commit 4ca0d7032a
2 changed files with 116 additions and 0 deletions

View File

@ -925,6 +925,33 @@ void String::append_str(const std::string &str) {
_data[_size] = '\0'; _data[_size] = '\0';
} }
void String::append_str(const String &other, const int from) {
if (other.size() <= from) {
return;
}
ensure_capacity(_size + other._size + 1 - from); // +1 for the null terminator
for (int i = from; i < other._size; ++i) {
_data[_size++] = other._data[i];
}
_data[_size] = '\0';
}
void String::append_str(const std::string &str, const int from) {
if (str.size() <= from) {
return;
}
ensure_capacity(_size + str.size() + 1 - from); // +1 for the null terminator
for (int i = from; i < str.size(); ++i) {
_data[_size++] = str[i];
}
_data[_size] = '\0';
}
void String::append_repeat(const char *str, const int times) { void String::append_repeat(const char *str, const int times) {
for (int i = 0; i < times; ++i) { for (int i = 0; i < times; ++i) {
append_str(str); append_str(str);
@ -936,6 +963,86 @@ void String::append_repeat(const String &other, const int times) {
} }
} }
void String::append_path(const char *path) {
if (path[0] == '\0') {
return;
}
if (_size == 0) {
append_str(path);
return;
}
int sindex = 0;
char ch = path[sindex];
while (ch == '/' || ch == '\\') {
if (ch == '\0') {
return;
}
ch = path[++sindex];
}
// /////folder
// ^ (sindex)
if (ends_with('/') || ends_with('\\')) {
append_str(&path[sindex]);
} else {
if (sindex > 0) {
append_str(&path[sindex - 1]);
} else {
if (_actual_size > 0) {
_data[_size++] = DEFAULT_DIRECTORY_SEPARATOR;
append_str(&path[sindex]);
} else {
push_back(DEFAULT_DIRECTORY_SEPARATOR);
append_str(&path[sindex]);
}
}
}
}
void String::append_path(const String &path) {
if (path._size == 0) {
return;
}
if (_size == 0) {
append_str(path);
return;
}
int sindex = 0;
int ts = path.size() - 1;
char ch = path[sindex];
while (ch == '/' || ch == '\\') {
if (sindex == ts) {
return;
}
ch = path[++sindex];
}
// /////folder
// ^ (sindex)
if (ends_with('/') || ends_with('\\')) {
append_str(path, sindex);
} else {
if (sindex > 0) {
append_str(path, sindex - 1);
} else {
if (_actual_size > 0) {
_data[_size++] = DEFAULT_DIRECTORY_SEPARATOR;
append_str(path, sindex);
} else {
push_back(DEFAULT_DIRECTORY_SEPARATOR);
append_str(path, sindex);
}
}
}
}
void String::to_html_special_chars() { void String::to_html_special_chars() {
replace("&", "&amp;"); replace("&", "&amp;");
replace("\"", "&quot;"); replace("\"", "&quot;");

View File

@ -7,6 +7,10 @@
#include "core/containers/vector.h" #include "core/containers/vector.h"
#ifndef DEFAULT_DIRECTORY_SEPARATOR
#define DEFAULT_DIRECTORY_SEPARATOR '/'
#endif
//TODO move to wchar_t! //TODO move to wchar_t!
class String { class String {
@ -97,10 +101,15 @@ public:
void append_str(const wchar_t* str); void append_str(const wchar_t* str);
void append_str(const String &other); void append_str(const String &other);
void append_str(const std::string &str); void append_str(const std::string &str);
void append_str(const String &other, const int from);
void append_str(const std::string &str, const int from);
void append_repeat(const char* str, const int times); void append_repeat(const char* str, const int times);
void append_repeat(const String &other, const int times); void append_repeat(const String &other, const int times);
void append_path(const char* path);
void append_path(const String &path);
void to_html_special_chars(); void to_html_special_chars();
void from_html_special_chars(); void from_html_special_chars();