Implement parse for variants.

This commit is contained in:
Relintai 2022-01-06 18:47:03 +01:00
parent 3d81df493c
commit b8442280dd
3 changed files with 67 additions and 3 deletions

View File

@ -951,6 +951,44 @@ int String::to_int() const {
return atoi(c_str());
}
bool String::is_bool() const {
if (_size == 0) {
return false;
}
if (_size == 1) {
if (_data[0] == '0') {
return true;
} else if (_data[0] == '1') {
return true;
}
return false;
}
if (_size == 4) {
String l = as_lower();
if (l[0] == 't' && l[1] == 'r' && l[2] == 'u' && l[3] == 'e') {
return true;
} else {
return false;
}
}
if (_size == 5) {
String l = as_lower();
if (l[0] == 'f' && l[1] == 'a' && l[2] == 'l' && l[3] == 's' && l[3] == 'e') {
return true;
} else {
return false;
}
}
return false;
}
bool String::is_numeric() const {
if (_size == 0) {
return false;

View File

@ -102,6 +102,7 @@ public:
double to_double() const;
int to_int() const;
bool is_bool() const;
bool is_numeric() const;
bool is_int() const;
bool is_uint() const;

View File

@ -39,12 +39,12 @@ void Variant::clear() {
break;
case TYPE_POINTER:
_pointer = nullptr;
break;
default:
break;
}
_type = TYPE_NULL;
}
void Variant::zero() {
@ -79,9 +79,34 @@ void Variant::zero() {
}
void Variant::parse(const String &str) {
if (str.is_bool()) {
set_bool(str.to_bool());
return;
}
if (str.is_int()) {
set_int(str.to_int());
return;
}
if (str.is_uint()) {
set_uint(str.to_uint());
return;
}
if (str.is_numeric()) {
set_float(str.to_float());
return;
}
set_string(str);
}
Variant Variant::parse_string(const String &str) {
return Variant();
Variant v = Variant();
v.parse(str);
return v;
}
bool Variant::is_null() const {