From 946c799a3b02a8a9447dc6067a2aae00674a7a97 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 9 Nov 2021 22:54:04 +0100 Subject: [PATCH] is_numeric, is_int, is_uint,and is_zero helpers for string. --- core/string.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++++- core/string.h | 6 +++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/core/string.cpp b/core/string.cpp index e556993..4779e63 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -554,6 +554,108 @@ int String::to_int() const { return atoi(c_str()); } +bool String::is_numeric() const { + if (_size == 0) { + return false; + } + + int starti = 0; + + if (_data[0] == '-') { + starti += 1; + } + + bool had_dot = false; + for (int i = starti; i < _size; ++i) { + if (_data[i] == '.') { + if (!had_dot) { + had_dot = true; + continue; + } else { + return false; + } + } + + char c = _data[i]; + + if (c < '0' || c > '9') { + return false; + } + } + + return true; +} + +bool String::is_int() const { + if (_size == 0) { + return false; + } + + int starti = 0; + + if (_data[0] == '-') { + starti += 1; + } + + for (int i = starti; i < _size; ++i) { + char c = _data[i]; + + if (c < '0' || c > '9') { + return false; + } + } + + return true; +} + +bool String::is_uint() const { + if (_size == 0) { + return false; + } + + for (int i = 0; i < _size; ++i) { + char c = _data[i]; + + if (c < '0' || c > '9') { + return false; + } + } + + return true; +} + +bool String::is_zero() const { + if (_size == 0) { + return false; + } + + int starti = 0; + + if (_data[0] == '-') { + starti += 1; + } + + bool had_dot = false; + for (int i = starti; i < _size; ++i) { + if (_data[i] == '.') { + if (!had_dot) { + had_dot = true; + continue; + } else { + return false; + } + } + + char c = _data[i]; + + if (c != '0') { + return false; + } + } + + return true; +} + uint32_t String::to_uint() const { return static_cast(atoll(c_str())); } @@ -930,7 +1032,7 @@ String String::utf8(const char *p_utf8, int p_len) { //Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. //Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). bool String::parse_utf8(const char *p_utf8, int p_len) { -//#define _UNICERROR(m_err) print_line("Unicode error: " + String(m_err)); + //#define _UNICERROR(m_err) print_line("Unicode error: " + String(m_err)); if (!p_utf8) { return true; diff --git a/core/string.h b/core/string.h index b65e4bc..1694c69 100644 --- a/core/string.h +++ b/core/string.h @@ -65,6 +65,12 @@ public: float to_float() const; double to_double() const; int to_int() const; + + bool is_numeric() const; + bool is_int() const; + bool is_uint() const; + bool is_zero() const; + uint32_t to_uint() const; std::string to_string() const; void print() const;