2017-05-31 19:43:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Copyright (C) 2002-2007 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Original description: (Schrompf)
|
|
|
|
// Adapted to the ASSIMP library because the builtin atof indeed takes AGES to parse a
|
|
|
|
// float inside a large string. Before parsing, it does a strlen on the given point.
|
|
|
|
// Changes:
|
|
|
|
// 22nd October 08 (Aramis_acg): Added temporary cast to double, added strtoul10_64
|
|
|
|
// to ensure long numbers are handled correctly
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
|
2019-10-11 11:27:36 +00:00
|
|
|
#pragma once
|
2018-02-23 17:14:46 +00:00
|
|
|
#ifndef FAST_A_TO_F_H_INCLUDED
|
|
|
|
#define FAST_A_TO_F_H_INCLUDED
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-10-11 11:27:36 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC system_header
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdexcept>
|
2016-07-16 02:14:36 +00:00
|
|
|
#include <assimp/defs.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#include "StringComparison.h"
|
2017-05-31 19:43:41 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
#ifdef _MSC_VER
|
2015-05-19 03:48:29 +00:00
|
|
|
# include <stdint.h>
|
2015-05-19 03:52:10 +00:00
|
|
|
#else
|
2016-06-07 09:51:09 +00:00
|
|
|
# include <assimp/Compiler/pstdint.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-23 17:59:38 +00:00
|
|
|
namespace Assimp {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const double fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
|
|
|
|
0.0,
|
|
|
|
0.1,
|
|
|
|
0.01,
|
|
|
|
0.001,
|
|
|
|
0.0001,
|
|
|
|
0.00001,
|
|
|
|
0.000001,
|
|
|
|
0.0000001,
|
|
|
|
0.00000001,
|
|
|
|
0.000000001,
|
|
|
|
0.0000000001,
|
|
|
|
0.00000000001,
|
|
|
|
0.000000000001,
|
|
|
|
0.0000000000001,
|
|
|
|
0.00000000000001,
|
|
|
|
0.000000000000001
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Convert a string in decimal format to a number
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-12 20:45:47 +00:00
|
|
|
inline
|
|
|
|
unsigned int strtoul10( const char* in, const char** out=0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int value = 0;
|
|
|
|
|
2018-02-12 20:45:47 +00:00
|
|
|
for ( ;; ) {
|
2018-02-23 17:59:38 +00:00
|
|
|
if ( *in < '0' || *in > '9' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2018-02-23 17:59:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
value = ( value * 10 ) + ( *in - '0' );
|
|
|
|
++in;
|
|
|
|
}
|
2018-02-12 20:45:47 +00:00
|
|
|
if ( out ) {
|
|
|
|
*out = in;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Convert a string in octal format to a number
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-12 20:45:47 +00:00
|
|
|
inline
|
|
|
|
unsigned int strtoul8( const char* in, const char** out=0) {
|
|
|
|
unsigned int value( 0 );
|
|
|
|
for ( ;; ) {
|
|
|
|
if ( *in < '0' || *in > '7' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2018-02-12 20:45:47 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
value = ( value << 3 ) + ( *in - '0' );
|
|
|
|
++in;
|
|
|
|
}
|
2018-02-12 20:45:47 +00:00
|
|
|
if ( out ) {
|
|
|
|
*out = in;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Convert a string in hex format to a number
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-12 20:45:47 +00:00
|
|
|
inline
|
|
|
|
unsigned int strtoul16( const char* in, const char** out=0) {
|
|
|
|
unsigned int value( 0 );
|
|
|
|
for ( ;; ) {
|
|
|
|
if ( *in >= '0' && *in <= '9' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
value = ( value << 4u ) + ( *in - '0' );
|
2018-02-12 20:45:47 +00:00
|
|
|
} else if (*in >= 'A' && *in <= 'F') {
|
2015-05-19 03:57:13 +00:00
|
|
|
value = ( value << 4u ) + ( *in - 'A' ) + 10;
|
2018-02-12 20:45:47 +00:00
|
|
|
} else if (*in >= 'a' && *in <= 'f') {
|
2015-05-19 03:57:13 +00:00
|
|
|
value = ( value << 4u ) + ( *in - 'a' ) + 10;
|
2018-02-23 17:59:38 +00:00
|
|
|
} else {
|
2018-02-12 20:45:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
++in;
|
|
|
|
}
|
2018-02-12 20:45:47 +00:00
|
|
|
if ( out ) {
|
|
|
|
*out = in;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Convert just one hex digit
|
|
|
|
// Return value is UINT_MAX if the input character is not a hex digit.
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 16:49:29 +00:00
|
|
|
inline
|
|
|
|
unsigned int HexDigitToDecimal(char in) {
|
2018-02-12 20:45:47 +00:00
|
|
|
unsigned int out( UINT_MAX );
|
|
|
|
if ( in >= '0' && in <= '9' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out = in - '0';
|
2018-02-12 20:45:47 +00:00
|
|
|
} else if ( in >= 'a' && in <= 'f' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out = 10u + in - 'a';
|
2018-02-12 20:45:47 +00:00
|
|
|
} else if ( in >= 'A' && in <= 'F' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out = 10u + in - 'A';
|
2018-02-12 20:45:47 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// return value is UINT_MAX if the input is not a hex digit
|
|
|
|
return out;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Convert a hex-encoded octet (2 characters, i.e. df or 1a).
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
uint8_t HexOctetToDecimal(const char* in) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// signed variant of strtoul10
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
int strtol10( const char* in, const char** out=0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
bool inv = (*in=='-');
|
2018-02-12 20:45:47 +00:00
|
|
|
if ( inv || *in == '+' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
++in;
|
2018-02-12 20:45:47 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
int value = strtoul10(in,out);
|
|
|
|
if (inv) {
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Parse a C++-like integer literal - hex and oct prefixes.
|
|
|
|
// 0xNNNN - hex
|
|
|
|
// 0NNN - oct
|
|
|
|
// NNN - dec
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
unsigned int strtoul_cppstyle( const char* in, const char** out=0) {
|
|
|
|
if ('0' == in[0]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
|
|
|
|
}
|
|
|
|
return strtoul10(in, out);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Special version of the function, providing higher accuracy and safety
|
|
|
|
// It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int cur = 0;
|
|
|
|
uint64_t value = 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if ( *in < '0' || *in > '9' ) {
|
|
|
|
throw std::invalid_argument( std::string( "The string \"" ) + in + "\" cannot be converted into a value." );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-02-12 20:45:47 +00:00
|
|
|
for ( ;; ) {
|
2018-02-23 17:14:46 +00:00
|
|
|
if ( *in < '0' || *in > '9' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2018-02-23 17:14:46 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-09-21 14:07:09 +00:00
|
|
|
const uint64_t new_value = ( value * (uint64_t) 10 ) + ( (uint64_t) ( *in - '0' ) );
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2017-05-31 19:43:41 +00:00
|
|
|
// numeric overflow, we rely on you
|
|
|
|
if ( new_value < value ) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN_F( "Converting the string \"", in, "\" into a value resulted in overflow." );
|
2017-05-31 19:43:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
value = new_value;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
++in;
|
|
|
|
++cur;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (max_inout && *max_inout == cur) {
|
|
|
|
if (out) { /* skip to end */
|
2018-02-23 17:14:46 +00:00
|
|
|
while ( *in >= '0' && *in <= '9' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
++in;
|
2018-02-23 17:14:46 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
*out = in;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 17:14:46 +00:00
|
|
|
if ( out ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
*out = in;
|
2018-02-23 17:14:46 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if ( max_inout ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
*max_inout = cur;
|
2018-02-23 17:14:46 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// signed variant of strtoul10_64
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
int64_t strtol10_64(const char* in, const char** out = 0, unsigned int* max_inout = 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
bool inv = (*in == '-');
|
2018-02-23 17:14:46 +00:00
|
|
|
if ( inv || *in == '+' ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
++in;
|
2018-02-23 17:14:46 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
int64_t value = strtoul10_64(in, out, max_inout);
|
|
|
|
if (inv) {
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
return value;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Number of relevant decimals for floating-point parsing.
|
|
|
|
#define AI_FAST_ATOF_RELAVANT_DECIMALS 15
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
//! Provides a fast function for converting a string into a float,
|
|
|
|
//! about 6 times faster than atof in win32.
|
|
|
|
// If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
|
|
|
|
// ------------------------------------------------------------------------------------
|
2018-02-23 17:59:38 +00:00
|
|
|
template<typename Real>
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
const char* fast_atoreal_move(const char* c, Real& out, bool check_comma = true) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Real f = 0;
|
|
|
|
|
|
|
|
bool inv = (*c == '-');
|
|
|
|
if (inv || *c == '+') {
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if ((c[0] == 'N' || c[0] == 'n') && ASSIMP_strincmp(c, "nan", 3) == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out = std::numeric_limits<Real>::quiet_NaN();
|
|
|
|
c += 3;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if ((c[0] == 'I' || c[0] == 'i') && ASSIMP_strincmp(c, "inf", 3) == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out = std::numeric_limits<Real>::infinity();
|
|
|
|
if (inv) {
|
|
|
|
out = -out;
|
|
|
|
}
|
|
|
|
c += 3;
|
2018-02-23 17:14:46 +00:00
|
|
|
if ((c[0] == 'I' || c[0] == 'i') && ASSIMP_strincmp(c, "inity", 5) == 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
c += 5;
|
|
|
|
}
|
|
|
|
return c;
|
2018-02-23 17:59:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (!(c[0] >= '0' && c[0] <= '9') &&
|
2018-02-23 17:59:38 +00:00
|
|
|
!((c[0] == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9')) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw std::invalid_argument("Cannot parse string "
|
|
|
|
"as real number: does not start with digit "
|
|
|
|
"or decimal point followed by digit.");
|
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if (*c != '.' && (! check_comma || c[0] != ',')) {
|
2015-05-19 03:57:13 +00:00
|
|
|
f = static_cast<Real>( strtoul10_64 ( c, &c) );
|
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
if ((*c == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9') {
|
2015-05-19 03:57:13 +00:00
|
|
|
++c;
|
|
|
|
|
|
|
|
// NOTE: The original implementation is highly inaccurate here. The precision of a single
|
|
|
|
// IEEE 754 float is not high enough, everything behind the 6th digit tends to be more
|
|
|
|
// inaccurate than it would need to be. Casting to double seems to solve the problem.
|
|
|
|
// strtol_64 is used to prevent integer overflow.
|
|
|
|
|
|
|
|
// Another fix: this tends to become 0 for long numbers if we don't limit the maximum
|
|
|
|
// number of digits to be read. AI_FAST_ATOF_RELAVANT_DECIMALS can be a value between
|
|
|
|
// 1 and 15.
|
|
|
|
unsigned int diff = AI_FAST_ATOF_RELAVANT_DECIMALS;
|
|
|
|
double pl = static_cast<double>( strtoul10_64 ( c, &c, &diff ));
|
|
|
|
|
|
|
|
pl *= fast_atof_table[diff];
|
|
|
|
f += static_cast<Real>( pl );
|
|
|
|
}
|
|
|
|
// For backwards compatibility: eat trailing dots, but not trailing commas.
|
|
|
|
else if (*c == '.') {
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A major 'E' must be allowed. Necessary for proper reading of some DXF files.
|
|
|
|
// Thanks to Zhao Lei to point out that this if() must be outside the if (*c == '.' ..)
|
|
|
|
if (*c == 'e' || *c == 'E') {
|
|
|
|
++c;
|
|
|
|
const bool einv = (*c=='-');
|
|
|
|
if (einv || *c=='+') {
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The reason float constants are used here is that we've seen cases where compilers
|
|
|
|
// would perform such casts on compile-time constants at runtime, which would be
|
|
|
|
// bad considering how frequently fast_atoreal_move<float> is called in Assimp.
|
|
|
|
Real exp = static_cast<Real>( strtoul10_64(c, &c) );
|
|
|
|
if (einv) {
|
|
|
|
exp = -exp;
|
|
|
|
}
|
|
|
|
f *= std::pow(static_cast<Real>(10.0), exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inv) {
|
|
|
|
f = -f;
|
|
|
|
}
|
|
|
|
out = f;
|
|
|
|
return c;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// The same but more human.
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
ai_real fast_atof(const char* c) {
|
2017-10-17 16:01:36 +00:00
|
|
|
ai_real ret(0.0);
|
2016-07-16 02:14:36 +00:00
|
|
|
fast_atoreal_move<ai_real>(c, ret);
|
2018-02-23 17:14:46 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return ret;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
ai_real fast_atof( const char* c, const char** cout) {
|
2017-10-17 16:01:36 +00:00
|
|
|
ai_real ret(0.0);
|
2016-07-16 02:14:36 +00:00
|
|
|
*cout = fast_atoreal_move<ai_real>(c, ret);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return ret;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
inline
|
|
|
|
ai_real fast_atof( const char** inout) {
|
2017-10-17 16:01:36 +00:00
|
|
|
ai_real ret(0.0);
|
2016-07-16 02:14:36 +00:00
|
|
|
*inout = fast_atoreal_move<ai_real>(*inout, ret);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return ret;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 20:45:47 +00:00
|
|
|
} //! namespace Assimp
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-02-23 17:14:46 +00:00
|
|
|
#endif // FAST_A_TO_F_H_INCLUDED
|