diff --git a/core/string.cpp b/core/string.cpp index 180cbbf..7ede8e2 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -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; diff --git a/core/string.h b/core/string.h index 444d777..ce87038 100644 --- a/core/string.h +++ b/core/string.h @@ -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; diff --git a/core/variant.cpp b/core/variant.cpp index d171d39..fe9cdc3 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -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 {