Branimir Karadžić 8 years ago
parent
commit
ad17de5811
3 changed files with 21 additions and 21 deletions
  1. 1 1
      include/bx/string.h
  2. 14 18
      src/dtoa.cpp
  3. 6 2
      tests/string_test.cpp

+ 1 - 1
include/bx/string.h

@@ -240,7 +240,7 @@ namespace bx
 	int32_t toString(char* _out, int32_t _max, uint64_t _value, uint32_t _base = 10);
 	int32_t toString(char* _out, int32_t _max, uint64_t _value, uint32_t _base = 10);
 
 
 	///
 	///
-	double fromString(const char* _str);
+	bool fromString(double* _out, const char* _str);
 
 
 } // namespace bx
 } // namespace bx
 
 

+ 14 - 18
src/dtoa.cpp

@@ -707,7 +707,7 @@ namespace bx
 
 
 #define GETC(s) *s++
 #define GETC(s) *s++
 
 
-	static int parser(const char *s, struct PrepNumber *pn)
+	static int parser(const char *s, PrepNumber *pn)
 	{
 	{
 		int state = FSM_A;
 		int state = FSM_A;
 		int digx = 0, c = ' ';            /* initial value for kicking off the state machine */
 		int digx = 0, c = ' ';            /* initial value for kicking off the state machine */
@@ -921,10 +921,10 @@ namespace bx
 		return result;
 		return result;
 	}
 	}
 
 
-	static double converter(struct PrepNumber *pn)
+	static double converter(PrepNumber *pn)
 	{
 	{
 		int binexp = 92;
 		int binexp = 92;
-		union HexDouble hd;
+		HexDouble hd;
 		uint32_t s2, s1, s0; /* 96-bit precision integer */
 		uint32_t s2, s1, s0; /* 96-bit precision integer */
 		uint32_t q2, q1, q0; /* 96-bit precision integer */
 		uint32_t q2, q1, q0; /* 96-bit precision integer */
 		uint32_t r2, r1, r0; /* 96-bit precision integer */
 		uint32_t r2, r1, r0; /* 96-bit precision integer */
@@ -1037,47 +1037,43 @@ namespace bx
 		return hd.d;
 		return hd.d;
 	}
 	}
 
 
-	double fromString(const char* _str)
+	bool fromString(double* _out, const char* _str)
 	{
 	{
-		struct PrepNumber pn;
-		union HexDouble hd;
-		int i;
-		double result;
-
+		PrepNumber pn;
 		pn.mantissa = 0;
 		pn.mantissa = 0;
 		pn.negative = 0;
 		pn.negative = 0;
 		pn.exponent = 0;
 		pn.exponent = 0;
-		hd.u = DOUBLE_PLUS_ZERO;
 
 
-		i = parser(_str, &pn);
+		HexDouble hd;
+		hd.u = DOUBLE_PLUS_ZERO;
 
 
-		switch (i)
+		switch (parser(_str, &pn) )
 		{
 		{
 		case PARSER_OK:
 		case PARSER_OK:
-			result = converter(&pn);
+			*_out = converter(&pn);
 			break;
 			break;
 
 
 		case PARSER_PZERO:
 		case PARSER_PZERO:
-			result = hd.d;
+			*_out = hd.d;
 			break;
 			break;
 
 
 		case PARSER_MZERO:
 		case PARSER_MZERO:
 			hd.u = DOUBLE_MINUS_ZERO;
 			hd.u = DOUBLE_MINUS_ZERO;
-			result = hd.d;
+			*_out = hd.d;
 			break;
 			break;
 
 
 		case PARSER_PINF:
 		case PARSER_PINF:
 			hd.u = DOUBLE_PLUS_INFINITY;
 			hd.u = DOUBLE_PLUS_INFINITY;
-			result = hd.d;
+			*_out = hd.d;
 			break;
 			break;
 
 
 		case PARSER_MINF:
 		case PARSER_MINF:
 			hd.u = DOUBLE_MINUS_INFINITY;
 			hd.u = DOUBLE_MINUS_INFINITY;
-			result = hd.d;
+			*_out = hd.d;
 			break;
 			break;
 		}
 		}
 
 
-		return result;
+		return true;
 	}
 	}
 
 
 } // namespace bx
 } // namespace bx

+ 6 - 2
tests/string_test.cpp

@@ -186,8 +186,12 @@ static bool testFromString(double _value, const char* _input)
 {
 {
 	char tmp[1024];
 	char tmp[1024];
 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
-	const double lhs = bx::fromString(tmp);
-	const double rhs = bx::fromString(_input);
+
+	double lhs;
+	bx::fromString(&lhs, tmp);
+
+	double rhs;
+	bx::fromString(&rhs, _input);
 
 
 	if (lhs == rhs)
 	if (lhs == rhs)
 	{
 	{