diff --git a/Engine/source/core/util/str.cpp b/Engine/source/core/util/str.cpp index 3f4f3d8f9..59fd0ab46 100644 --- a/Engine/source/core/util/str.cpp +++ b/Engine/source/core/util/str.cpp @@ -1624,3 +1624,103 @@ String String::GetTrailingNumber(const char* str, S32& number) return base.substr(0, p - base.c_str()); } + +String String::GetFirstNumber(const char* str, U32& startPos, U32& endPos) +{ + // Check for trivial strings + if (!str || !str[0]) + return String::EmptyString; + + // Find the number at the end of the string + String base(str); + const char* p = base.c_str(); + const char* end = base.c_str() + base.length() - 1; + bool dec = false; + startPos = 0; + + //Check if we are just a digit + if(p == end && isdigit(*p)) + return base; + + //Look for the first digit + while ((p != end) && (dIsspace(*p) || !isdigit(*p))) + { + p++; + startPos++; + } + + //Handle if we are at the end and found nothing + if(p == end && !isdigit(*p)) + return ""; + + //update our end position at least to the start of our number + endPos = startPos; + + //Backup our ptr + const char* backup = p; + + //Check for any negative or decimal values + if(startPos > 0) + { + p--; + startPos--; + if(*p == '.') + { + dec = true; + + //ignore any duplicate periods + while ((p != base.c_str()) && (*p == '.')) + { + p--; + startPos--; + } + + //Found a decimal lets still check for negative sign + if(startPos > 0) + { + p--; + startPos--; + if((*p != '-') && (*p != '_')) + { + startPos++; + p++; + } + } + } + else if((*p != '-') && (*p != '_')) + { + //go back to where we where cause no decimal or negative sign found + startPos++; + p++; + } + } + + //Restore where we were + p = backup; + + //look for the end of the digits + bool justFoundDec = false; + while (p != end) + { + if(*p == '.') + { + if(dec && !justFoundDec) + break; + else + { + dec = true; + justFoundDec = true; + } + } + else if(!isdigit(*p)) + break; + else if(justFoundDec) + justFoundDec = false; + + p++; + endPos++; + } + + U32 len = (!isdigit(*p)) ? endPos - startPos : (endPos + 1) - startPos; + return base.substr(startPos, len); +} diff --git a/Engine/source/core/util/str.h b/Engine/source/core/util/str.h index 009484451..33fdb819d 100644 --- a/Engine/source/core/util/str.h +++ b/Engine/source/core/util/str.h @@ -190,6 +190,7 @@ public: static String ToUpper(const String &string); static String GetTrailingNumber(const char* str, S32& number); + static String GetFirstNumber(const char* str, U32& startPos, U32& endPos); /// @}