diff options
author | jude-lafitteIII <judewrs@gmail.com> | 2019-04-25 13:43:48 +0100 |
---|---|---|
committer | jude-lafitteIII <judewrs@gmail.com> | 2019-04-25 13:44:27 +0100 |
commit | d3cc9c0bf191491ea3beaa47c7ba46dcd0af38e2 (patch) | |
tree | 4dfc6ca76ae74bc0a8bd1ad63636affdd76da261 /core | |
parent | f67c78d9cd777548c28b2adea67232c157b8187f (diff) |
Support for binary literals in GDScript. Added an error that shows if a point is written in a hex literal. Added highlighting for binary literals in GDScript
Diffstat (limited to 'core')
-rw-r--r-- | core/ustring.cpp | 39 | ||||
-rw-r--r-- | core/ustring.h | 1 |
2 files changed, 40 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 78feddb229..954c39c150 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1725,6 +1725,45 @@ int64_t String::hex_to_int64(bool p_with_prefix) const { return hex * sign; } +int64_t String::bin_to_int64(bool p_with_prefix) const { + + if (p_with_prefix && length() < 3) + return 0; + + const CharType *s = ptr(); + + int64_t sign = s[0] == '-' ? -1 : 1; + + if (sign < 0) { + s++; + } + + if (p_with_prefix) { + if (s[0] != '0' || s[1] != 'b') + return 0; + s += 2; + } + + int64_t binary = 0; + + while (*s) { + + CharType c = LOWERCASE(*s); + int64_t n; + if (c == '0' || c == '1') { + n = c - '0'; + } else { + return 0; + } + + binary *= 2; + binary += n; + s++; + } + + return binary * sign; +} + int String::to_int() const { if (length() == 0) diff --git a/core/ustring.h b/core/ustring.h index e2e62874d6..be6300ac5b 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -251,6 +251,7 @@ public: int to_int() const; int64_t hex_to_int64(bool p_with_prefix = true) const; + int64_t bin_to_int64(bool p_with_prefix = true) const; int64_t to_int64() const; static int to_int(const char *p_str, int p_len = -1); static double to_double(const char *p_str); |