浏览代码

Added fromString for integer types.

Branimir Karadžić 8 年之前
父节点
当前提交
11a8624dd8
共有 6 个文件被更改,包括 88 次插入8 次删除
  1. 6 0
      include/bx/string.h
  2. 4 4
      src/commandline.cpp
  3. 6 4
      src/crtnone.cpp
  4. 36 0
      src/dtoa.cpp
  5. 31 0
      tests/string_test.cpp
  6. 5 0
      tests/tokenizecmd_test.cpp

+ 6 - 0
include/bx/string.h

@@ -269,6 +269,12 @@ namespace bx
 	///
 	bool fromString(double* _out, const char* _str);
 
+	///
+	bool fromString(int32_t* _out, const char* _str);
+
+	///
+	bool fromString(uint32_t* _out, const char* _str);
+
 } // namespace bx
 
 #include "inline/string.inl"

+ 4 - 4
src/commandline.cpp

@@ -197,7 +197,7 @@ namespace bx
 		const char* arg = findOption(_short, _long, 1);
 		if (NULL != arg)
 		{
-			_value = atoi(arg);
+			fromString(&_value, arg);
 			return true;
 		}
 
@@ -209,7 +209,7 @@ namespace bx
 		const char* arg = findOption(_short, _long, 1);
 		if (NULL != arg)
 		{
-			_value = atoi(arg);
+			fromString(&_value, arg);
 			return true;
 		}
 
@@ -221,7 +221,7 @@ namespace bx
 		const char* arg = findOption(_short, _long, 1);
 		if (NULL != arg)
 		{
-			_value = float(atof(arg));
+			fromString(&_value, arg);
 			return true;
 		}
 
@@ -233,7 +233,7 @@ namespace bx
 		const char* arg = findOption(_short, _long, 1);
 		if (NULL != arg)
 		{
-			_value = atof(arg);
+			fromString(&_value, arg);
 			return true;
 		}
 

+ 6 - 4
src/crtnone.cpp

@@ -244,14 +244,16 @@ extern "C" float fmodf(float _numer, float _denom)
 
 extern "C" int atoi(const char* _str)
 {
-	BX_UNUSED(_str);
-	return 0;
+	int32_t result = 0;
+	bx::fromString(&result, _str);
+	return result;
 }
 
 extern "C" double atof(const char* _str)
 {
-	BX_UNUSED(_str);
-	return 0.0;
+	double result = 0.0;
+	bx::fromString(&result, _str);
+	return result;
 }
 
 extern "C" struct DIR* opendir(const char* dirname)

+ 36 - 0
src/dtoa.cpp

@@ -3,6 +3,8 @@
  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  */
 
+#include <stdio.h>
+
 #include <bx/cpu.h>
 #include <bx/math.h>
 #include <bx/string.h>
@@ -1081,4 +1083,38 @@ namespace bx
 		return true;
 	}
 
+	bool fromString(int32_t* _out, const char* _str)
+	{
+		_str = strws(_str);
+		char ch = *_str++;
+		bool neg = false;
+		switch (ch)
+		{
+		case '-':
+		case '+': neg = '-' == ch;
+			break;
+
+		default:
+			--_str;
+			break;
+		}
+
+		int32_t result = 0;
+
+		for (ch = *_str++; isNumeric(ch); ch = *_str++)
+		{
+			result = 10*result - (ch - '0');
+		}
+
+		*_out = neg ? result : -result;
+
+		return true;
+	}
+
+	bool fromString(uint32_t* _out, const char* _str)
+	{
+		fromString( (int32_t*)_out, _str);
+		return true;
+	}
+
 } // namespace bx

+ 31 - 0
tests/string_test.cpp

@@ -291,6 +291,37 @@ TEST_CASE("fromString double", "")
 	REQUIRE(testFromString(0.0000000001,            "1e-10") );
 }
 
+static bool testFromString(int32_t _value, const char* _input)
+{
+	char tmp[1024];
+	bx::toString(tmp, BX_COUNTOF(tmp), _value);
+
+	double lhs;
+	bx::fromString(&lhs, tmp);
+
+	double rhs;
+	bx::fromString(&rhs, _input);
+
+	if (lhs == rhs)
+	{
+		return true;
+	}
+
+	printf("result '%d', input '%s'\n", _value, _input);
+	return false;
+}
+
+TEST_CASE("fromString int32_t", "")
+{
+	REQUIRE(testFromString(1389,   "1389") );
+	REQUIRE(testFromString(1389,   "  1389") );
+	REQUIRE(testFromString(1389,   "+1389") );
+	REQUIRE(testFromString(-1389,   "-1389") );
+	REQUIRE(testFromString(-1389,   " -1389") );
+	REQUIRE(testFromString(555333, "555333") );
+	REQUIRE(testFromString(-21,    "-021") );
+}
+
 TEST_CASE("StringView", "")
 {
 	bx::StringView sv("test");

+ 5 - 0
tests/tokenizecmd_test.cpp

@@ -15,6 +15,7 @@ TEST_CASE("commandLine", "")
 		"--long",
 		"--platform",
 		"x",
+		"--num", "1389",
 		"--foo",
 		"--", // it should not parse arguments after argument terminator
 		"--bar",
@@ -25,6 +26,10 @@ TEST_CASE("commandLine", "")
 	REQUIRE( cmdLine.hasArg("long") );
 	REQUIRE( cmdLine.hasArg('s') );
 
+	int32_t num;
+	REQUIRE(cmdLine.hasArg(num, '\0', "num") );
+	REQUIRE(1389 == num);
+
 	// test argument terminator
 	REQUIRE( cmdLine.hasArg("foo") );
 	REQUIRE(!cmdLine.hasArg("bar") );