diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-05-29 08:41:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-29 08:41:05 +0200 |
commit | 36591b1ae8a8403d7ca2269d996a60a1775a9e94 (patch) | |
tree | 0f158bfe75b8247837e6f051717610b412b5c1e7 /core/ustring.cpp | |
parent | 123edd04c0d8cd560d0396fb349af2a6cd1a3e90 (diff) | |
parent | d3cc9c0bf191491ea3beaa47c7ba46dcd0af38e2 (diff) |
Merge pull request #28416 from JellyWX/binary-literals
Support for binary literals in GDScript
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 39 |
1 files changed, 39 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) |