Added a set of generic append helpers to String.

This commit is contained in:
Relintai 2022-02-15 12:05:51 +01:00
parent 58eb9f8252
commit a68cbb96df
2 changed files with 82 additions and 31 deletions

View File

@ -2,6 +2,7 @@
#include "core/math/math.h" #include "core/math/math.h"
#include "error_macros.h" #include "error_macros.h"
#include "variant.h"
#include <stdlib.h> #include <stdlib.h>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@ -1430,6 +1431,41 @@ void String::print() const {
::printf("%s\n", c_str()); ::printf("%s\n", c_str());
} }
// Generic set of append helpers
void String::append(const char *str) {
append_str(str);
}
void String::append(const wchar_t *str) {
append_str(str);
}
void String::append(const String &other) {
append_str(other);
}
void String::append(const std::string &str) {
append_str(str);
}
void String::append(const char chr) {
push_back(chr);
}
void String::append(const wchar_t chr) {
push_back(chr);
}
void String::append(const int num) {
append_str(String::num(num));
}
void String::append(const unsigned int num) {
append_str(String::num(num));
}
void String::append(const float num) {
append_str(String::num(num));
}
void String::append(const double num) {
append_str(String::num(num));
}
void String::append(const Variant &variant) {
append_str(variant.to_string());
}
String String::bool_num(bool val) { String String::bool_num(bool val) {
if (val) { if (val) {
return String("1", 2); return String("1", 2);

View File

@ -11,7 +11,9 @@
#define DEFAULT_DIRECTORY_SEPARATOR '/' #define DEFAULT_DIRECTORY_SEPARATOR '/'
#endif #endif
//TODO move to wchar_t! class Variant;
// TODO move to wchar_t!
class String { class String {
public: public:
@ -42,9 +44,9 @@ public:
bool contains(const char val) const; bool contains(const char val) const;
bool contains(const String &val) const; bool contains(const String &val) const;
bool is_word_at(const int index, const char* str) const; bool is_word_at(const int index, const char *str) const;
bool is_word_at(const int index, const String &val) const; bool is_word_at(const int index, 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);
void replace(const String &find_str, const String &with, const int count); void replace(const String &find_str, const String &with, const int count);
@ -95,21 +97,21 @@ public:
void append_int64_bytes(const int64_t val); void append_int64_bytes(const int64_t val);
float read_float_bytes_at(int &index, bool advance_index = true); float read_float_bytes_at(int &index, bool advance_index = true);
void append_float_bytes(const float val); void append_float_bytes(const float val);
double read_double_bytes_at(int &index, bool advance_index = true); double read_double_bytes_at(int &index, bool advance_index = true);
void append_double_bytes(const double val); void append_double_bytes(const double val);
void append_str(const char* str); void append_str(const char *str);
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 String &other, const int from);
void append_str(const std::string &str, 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 char *path);
void append_path(const String &path); void append_path(const String &path);
void path_clean_end_slash(); void path_clean_end_slash();
void path_ensure_end_slash(); void path_ensure_end_slash();
@ -136,13 +138,26 @@ public:
uint32_t to_uint() const; uint32_t to_uint() const;
std::string to_string() const; std::string to_string() const;
void print() const; void print() const;
// Generic set of append helpers
void append(const char *str);
void append(const wchar_t *str);
void append(const String &other);
void append(const std::string &str);
void append(const char chr);
void append(const wchar_t chr);
void append(const int num);
void append(const unsigned int num);
void append(const float num);
void append(const double num);
void append(const Variant &variant);
static String bool_num(bool val); static String bool_num(bool val);
static String bool_str(bool val); static String bool_str(bool val);
//Taken from the Godot Engine (MIT License) // Taken from the Godot Engine (MIT License)
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). // Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
static String num(double p_num, int p_decimals = -1); static String num(double p_num, int p_decimals = -1);
static String num_scientific(double p_num); static String num_scientific(double p_num);
static String num_real(double p_num, bool p_trailing = true); static String num_real(double p_num, bool p_trailing = true);
@ -150,17 +165,17 @@ public:
static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false); static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
static String chr(char32_t p_char); static String chr(char32_t p_char);
//Taken from the Godot Engine (MIT License) // Taken from the Godot Engine (MIT License)
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). // Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
String ascii(bool p_allow_extended = false) const; String ascii(bool p_allow_extended = false) const;
String utf8() const; String utf8() const;
bool parse_utf8(const char *p_utf8, int p_len = -1); //return true on error bool parse_utf8(const char *p_utf8, int p_len = -1); // return true on error
static String utf8(const char *p_utf8, int p_len = -1); static String utf8(const char *p_utf8, int p_len = -1);
//Taken from the Godot Engine (MIT License) // Taken from the Godot Engine (MIT License)
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). // Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
static uint32_t hash(const wchar_t *p_cstr, int p_len); /* hash the string */ static uint32_t hash(const wchar_t *p_cstr, int p_len); /* hash the string */
static uint32_t hash(const wchar_t *p_cstr); /* hash the string */ static uint32_t hash(const wchar_t *p_cstr); /* hash the string */
static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */ static uint32_t hash(const char *p_cstr, int p_len); /* hash the string */
@ -216,17 +231,17 @@ public:
operator std::string() { return to_string(); } operator std::string() { return to_string(); }
operator std::string() const { return to_string(); } operator std::string() const { return to_string(); }
String& operator=(const String &other); String &operator=(const String &other);
String& operator=(const std::string &other); String &operator=(const std::string &other);
String& operator=(const char* other); String &operator=(const char *other);
String& operator=(const wchar_t* other); String &operator=(const wchar_t *other);
String(); String();
String(const String &other); String(const String &other);
String(const String &other, const int grow_by); String(const String &other, const int grow_by);
String(const char* p_c_str); String(const char *p_c_str);
String(const char* p_c_str, const int grow_by); String(const char *p_c_str, const int grow_by);
String(const wchar_t* p_c_str); String(const wchar_t *p_c_str);
String(const int prealloc); String(const int prealloc);
String(const int prealloc, const int grow_by); String(const int prealloc, const int grow_by);
String(const std::string &str); String(const std::string &str);
@ -239,9 +254,9 @@ private:
int _grow_by; int _grow_by;
}; };
//Taken from the Godot Engine (MIT License) // Taken from the Godot Engine (MIT License)
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. // Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). // Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
template <typename L, typename R> template <typename L, typename R>
bool is_str_less(const L *l_ptr, const R *r_ptr) { bool is_str_less(const L *l_ptr, const R *r_ptr) {
while (true) { while (true) {