mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Added fromString for integer types.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
src/dtoa.cpp
36
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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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") );
|
||||
|
||||
Reference in New Issue
Block a user