Added bool_num, and bool_str to the String.

This commit is contained in:
Relintai 2021-11-02 12:24:26 +01:00
parent 212a144215
commit dadd431f1c
2 changed files with 30 additions and 4 deletions

View File

@ -1,10 +1,10 @@
#include "string.h" #include "string.h"
#include "core/math.h"
#include "error_macros.h" #include "error_macros.h"
#include <stdlib.h> #include <stdlib.h>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include "core/math.h"
static const int MAX_DECIMALS = 32; static const int MAX_DECIMALS = 32;
@ -143,7 +143,7 @@ int String::find(const String &val, const int from) const {
if (found) { if (found) {
return i; return i;
} }
} }
return -1; return -1;
} }
@ -568,6 +568,21 @@ void String::print() const {
::printf("%s\n", c_str()); ::printf("%s\n", c_str());
} }
String String::bool_num(bool val) {
if (val) {
return String("1", 2);
} else {
return String("0", 2);
}
}
String String::bool_str(bool val) {
if (val) {
return String("true", 5);
} else {
return String("false", 6);
}
}
//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).
@ -985,8 +1000,12 @@ bool operator!=(const String &a, const String &b) {
} }
bool operator==(const String &a, const char *b) { bool operator==(const String &a, const char *b) {
if (a._size == 0) {
return b[0] == '\0';
}
int i = 0; int i = 0;
while (b[i] != '\0' && i < a._size) { while (i < a._size && b[i] != '\0') {
if (a[i] != b[i]) { if (a[i] != b[i]) {
return false; return false;
} }
@ -1006,8 +1025,12 @@ bool operator!=(const String &a, const char *b) {
} }
bool operator==(const char *b, const String &a) { bool operator==(const char *b, const String &a) {
if (a._size == 0) {
return b[0] == '\0';
}
int i = 0; int i = 0;
while (b[i] != '\0' && i < a._size) { while (i < a._size && b[i] != '\0') {
if (a[i] != b[i]) { if (a[i] != b[i]) {
return false; return false;
} }

View File

@ -68,6 +68,9 @@ 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;
static String bool_num(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.