Skip to content

Commit 7402d3a

Browse files
committed
Added String::IsInteger
1 parent d5e7976 commit 7402d3a

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

lib/public/StormByte/string.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ namespace StormByte::String {
246246
return out;
247247
}
248248

249+
bool IsInteger(const std::string& str) noexcept {
250+
if (str.empty()) return false;
251+
252+
size_t start = 0;
253+
if (str[0] == '-' || str[0] == '+') {
254+
if (str.size() == 1) return false; // Only sign, no digits
255+
start = 1;
256+
}
257+
258+
for (size_t i = start; i < str.size(); ++i) {
259+
if (!std::isdigit(static_cast<unsigned char>(str[i]))) {
260+
return false;
261+
}
262+
}
263+
264+
return true;
265+
}
266+
249267
// Explicit instantiations for `HumanReadable` (ordered by category).
250268
// Note: `wchar_t`, `char16_t`, and `char32_t` are excluded because they are not
251269
// streamed to `std::ostringstream` on many standard library implementations.

lib/public/StormByte/string.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,14 @@ namespace StormByte::String {
179179
* @return The string with all whitespace characters removed.
180180
*/
181181
STORMBYTE_PUBLIC std::string RemoveWhitespace(const std::string& str) noexcept;
182+
183+
/**
184+
* @brief Checks if a string represents a valid integer.
185+
*
186+
* This function determines whether the given string can be interpreted as an integer.
187+
*
188+
* @param str The string to check.
189+
* @return `true` if the string represents an integer, `false` otherwise.
190+
*/
191+
STORMBYTE_PUBLIC bool IsInteger(const std::string& str) noexcept;
182192
}

0 commit comments

Comments
 (0)