瀏覽代碼

Forgot supporting method

Added string manipulation functions upload, requires these changes to
compile.
Nathan Bowhay 10 年之前
父節點
當前提交
c98e95e6ff
共有 2 個文件被更改,包括 101 次插入0 次删除
  1. 100 0
      Engine/source/core/util/str.cpp
  2. 1 0
      Engine/source/core/util/str.h

+ 100 - 0
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);
+}

+ 1 - 0
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);
 
    /// @}