Added lots of helpers to the string.

This commit is contained in:
Relintai 2021-11-19 20:02:16 +01:00
parent f1965f4bd5
commit 47545dc181
4 changed files with 266 additions and 2 deletions

View File

@ -1,5 +1,7 @@
#include "logger.h" #include "logger.h"
#include "core/string.h"
#include <cstdio> #include <cstdio>
void Logger::log_trace(const String &str) void Logger::log_trace(const String &str)

View File

@ -1,7 +1,7 @@
#ifndef LOGGER_H #ifndef LOGGER_H
#define LOGGER_H #define LOGGER_H
#include "core/string.h" class String;
class Logger { class Logger {
public: public:

View File

@ -184,6 +184,29 @@ String String::substr(const int start_index, const int len) const {
return str; return str;
} }
String String::substr_index(const int start_index, const int end_index) const {
ERR_FAIL_INDEX_V(start_index, _size, String());
ERR_FAIL_INDEX_V(end_index, _size, String());
ERR_FAIL_COND_V(start_index > end_index, String());
String str;
str.ensure_capacity(end_index - start_index + 1);
for (int i = start_index; i <= end_index; ++i) {
str._data[str._size++] = _data[i];
}
str._data[str._size] = '\0';
return str;
}
bool String::contains(const char val) const {
return find(val) != -1;
}
bool String::contains(const String &val) const {
return find(val) != -1;
}
void String::replace_from(const int start_index, const int length, const String &with) { void String::replace_from(const int start_index, const int length, const String &with) {
ERR_FAIL_INDEX(start_index, _size); ERR_FAIL_INDEX(start_index, _size);
@ -250,6 +273,216 @@ int String::compare(const String &other) const {
} }
} }
void String::to_lower() {
for (int i = 0; i < _size; ++i) {
char c = _data[i];
if (c >= 56 && c <= 90) {
_data[i] = c + 32;
}
}
}
String String::as_lower() const {
String a = *this;
a.to_lower();
return a;
}
void String::trim() {
trim_end();
trim_beginning();
}
void String::trim_beginning() {
if (_size == 0) {
return;
}
bool found = false;
int last_index = 0;
for (int i = 0; i < _size; ++i) {
char c = _data[i];
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
found = true;
last_index == i;
} else {
break;
}
}
if (!found) {
return;
}
++last_index;
if (last_index == _size) {
_size = 0;
_data[_size] = '\0';
return;
}
for (int i = 0; i < _size - last_index; ++i) {
_data[i] = _data[i + last_index];
}
_size -= last_index;
_data[_size] = '\0';
}
void String::trim_end() {
if (_size == 0) {
return;
}
int last_index = _size;
for (int i = _size - 1; i <= 0; --i) {
char c = _data[i];
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
last_index == i;
} else {
break;
}
}
if (last_index == _size) {
return;
}
_data[last_index] = '\0';
_size = last_index;
}
int String::get_slice_count(const char splitter) const {
int count = 0;
for (int i = 0; i < _size; ++i) {
if (_data[i] == splitter) {
++count;
}
}
return count;
}
int String::get_slice_count(const String &splitter) const {
int count = 0;
int n = find(splitter, n);
while (n != -1) {
++count;
n = find(splitter, n);
}
return count;
}
String String::get_slice(const char splitter, int index) {
int count = 0;
int start_index = 0;
for (int i = 0; i < _size; ++i) {
if (_data[i] == splitter) {
++count;
if (count == index) {
start_index = i + 1;
}
if (count == index + 1) {
return substr_index(start_index, i - 1);
}
}
}
if (count == 0) {
return "";
}
return substr_index(start_index, _size - 1);
}
String String::get_slice(const String &splitter, int index) {
int count = 0;
int start_index = 0;
int n = find(splitter, n);
while (n != -1) {
++count;
n = find(splitter, n);
if (count == index) {
start_index = n + splitter.size();
}
if (count == index + 1) {
return substr_index(start_index, n - 1);
}
}
if (count == 0) {
return "";
}
return substr_index(start_index, _size - 1);
}
Vector<String> String::split(const char splitter) const {
Vector<String> v;
if (_size == 0) {
return v;
}
int start_index = 0;
for (int i = 1; i < _size; ++i) {
if (_data[i] == splitter) {
if (start_index == i) {
v.push_back(String());
} else {
v.push_back(substr_index(start_index, i - 1));
}
start_index = i + 1;
}
}
if (start_index < _size - 1) {
v.push_back(substr_index(start_index, _size - 1));
}
return v;
}
Vector<String> String::split(const String &splitter) const {
Vector<String> v;
if (_size == 0) {
return v;
}
int start_index = 0;
int n = 0;
while (n != -1) {
n = find(splitter, n);
v.push_back(substr_index(start_index, n - 1));
start_index = n + splitter.size();
}
if (start_index < _size - 1) {
v.push_back(substr_index(start_index, _size - 1));
}
return v;
}
uint8_t String::read_uint8_bytes_at(int &index, bool advance_index) { uint8_t String::read_uint8_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index, _size, 0); ERR_FAIL_INDEX_V(index, _size, 0);
@ -553,6 +786,14 @@ void String::append_repeat(const String &other, const int times) {
} }
} }
bool String::to_bool() const {
if (is_numeric()) {
return to_int() != 0;
}
return as_lower() == "true";
}
float String::to_float() const { float String::to_float() const {
return atof(c_str()); return atof(c_str());
} }

View File

@ -5,6 +5,8 @@
#include <string> #include <string>
#include "core/containers/vector.h"
class String { class String {
public: public:
void push_back(const char element); void push_back(const char element);
@ -27,12 +29,30 @@ public:
void get_substr(char *into_buf, const int start_index, const int len); void get_substr(char *into_buf, const int start_index, const int len);
void get_substr_nt(char *into_buf, const int start_index, const int len); void get_substr_nt(char *into_buf, const int start_index, const int len);
String substr(const int start_index, const int len) const; String substr(const int start_index, const int len) const;
String substr_index(const int start_index, const int end_index) const;
bool contains(const char val) const;
bool contains(const String &val) const;
void replace_from(const int start_index, const int length, const String &with); void replace_from(const int start_index, const int length, const String &with);
void replace(const String &find_str, const String &with); void replace(const String &find_str, const String &with);
int compare(const String &other) const; int compare(const String &other) const;
void to_lower();
String as_lower() const;
void trim();
void trim_beginning();
void trim_end();
int get_slice_count(const char splitter) const;
int get_slice_count(const String &splitter) const;
String get_slice(const char splitter, int index);
String get_slice(const String &splitter, int index);
Vector<String> split(const char splitter) const;
Vector<String> split(const String &splitter) const;
uint8_t read_uint8_bytes_at(int &index, bool advance_index = true); uint8_t read_uint8_bytes_at(int &index, bool advance_index = true);
uint16_t read_uint16_bytes_at(int &index, bool advance_index = true); uint16_t read_uint16_bytes_at(int &index, bool advance_index = true);
uint32_t read_uint32_bytes_at(int &index, bool advance_index = true); uint32_t read_uint32_bytes_at(int &index, bool advance_index = true);
@ -65,6 +85,7 @@ public:
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);
bool to_bool() const;
float to_float() const; float to_float() const;
double to_double() const; double to_double() const;
int to_int() const; int to_int() const;