You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

341 line
8.9 KiB

  1. // Copyright (C) 2002-2007 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
  4. // ------------------------------------------------------------------------------------
  5. // Original description: (Schrompf)
  6. // Adapted to the ASSIMP library because the builtin atof indeed takes AGES to parse a
  7. // float inside a large string. Before parsing, it does a strlen on the given point.
  8. // Changes:
  9. // 22nd October 08 (Aramis_acg): Added temporary cast to double, added strtoul10_64
  10. // to ensure long numbers are handled correctly
  11. // ------------------------------------------------------------------------------------
  12. #ifndef __FAST_A_TO_F_H_INCLUDED__
  13. #define __FAST_A_TO_F_H_INCLUDED__
  14. #include <math.h>
  15. #include <limits.h>
  16. namespace Assimp
  17. {
  18. const double fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
  19. 0.0,
  20. 0.1,
  21. 0.01,
  22. 0.001,
  23. 0.0001,
  24. 0.00001,
  25. 0.000001,
  26. 0.0000001,
  27. 0.00000001,
  28. 0.000000001,
  29. 0.0000000001,
  30. 0.00000000001,
  31. 0.000000000001,
  32. 0.0000000000001,
  33. 0.00000000000001,
  34. 0.000000000000001
  35. };
  36. // ------------------------------------------------------------------------------------
  37. // Convert a string in decimal format to a number
  38. // ------------------------------------------------------------------------------------
  39. inline unsigned int strtoul10( const char* in, const char** out=0)
  40. {
  41. unsigned int value = 0;
  42. bool running = true;
  43. while ( running )
  44. {
  45. if ( *in < '0' || *in > '9' )
  46. break;
  47. value = ( value * 10 ) + ( *in - '0' );
  48. ++in;
  49. }
  50. if (out)*out = in;
  51. return value;
  52. }
  53. // ------------------------------------------------------------------------------------
  54. // Convert a string in octal format to a number
  55. // ------------------------------------------------------------------------------------
  56. inline unsigned int strtoul8( const char* in, const char** out=0)
  57. {
  58. unsigned int value = 0;
  59. bool running = true;
  60. while ( running )
  61. {
  62. if ( *in < '0' || *in > '7' )
  63. break;
  64. value = ( value << 3 ) + ( *in - '0' );
  65. ++in;
  66. }
  67. if (out)*out = in;
  68. return value;
  69. }
  70. // ------------------------------------------------------------------------------------
  71. // Convert a string in hex format to a number
  72. // ------------------------------------------------------------------------------------
  73. inline unsigned int strtoul16( const char* in, const char** out=0)
  74. {
  75. unsigned int value = 0;
  76. bool running = true;
  77. while ( running )
  78. {
  79. if ( *in >= '0' && *in <= '9' )
  80. {
  81. value = ( value << 4u ) + ( *in - '0' );
  82. }
  83. else if (*in >= 'A' && *in <= 'F')
  84. {
  85. value = ( value << 4u ) + ( *in - 'A' ) + 10;
  86. }
  87. else if (*in >= 'a' && *in <= 'f')
  88. {
  89. value = ( value << 4u ) + ( *in - 'a' ) + 10;
  90. }
  91. else break;
  92. ++in;
  93. }
  94. if (out)*out = in;
  95. return value;
  96. }
  97. // ------------------------------------------------------------------------------------
  98. // Convert just one hex digit
  99. // Return value is UINT_MAX if the input character is not a hex digit.
  100. // ------------------------------------------------------------------------------------
  101. inline unsigned int HexDigitToDecimal(char in)
  102. {
  103. unsigned int out = UINT_MAX;
  104. if (in >= '0' && in <= '9')
  105. out = in - '0';
  106. else if (in >= 'a' && in <= 'f')
  107. out = 10u + in - 'a';
  108. else if (in >= 'A' && in <= 'F')
  109. out = 10u + in - 'A';
  110. // return value is UINT_MAX if the input is not a hex digit
  111. return out;
  112. }
  113. // ------------------------------------------------------------------------------------
  114. // Convert a hex-encoded octet (2 characters, i.e. df or 1a).
  115. // ------------------------------------------------------------------------------------
  116. inline uint8_t HexOctetToDecimal(const char* in)
  117. {
  118. return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
  119. }
  120. // ------------------------------------------------------------------------------------
  121. // signed variant of strtoul10
  122. // ------------------------------------------------------------------------------------
  123. inline int strtol10( const char* in, const char** out=0)
  124. {
  125. bool inv = (*in=='-');
  126. if (inv || *in=='+')
  127. ++in;
  128. int value = strtoul10(in,out);
  129. if (inv) {
  130. value = -value;
  131. }
  132. return value;
  133. }
  134. // ------------------------------------------------------------------------------------
  135. // Parse a C++-like integer literal - hex and oct prefixes.
  136. // 0xNNNN - hex
  137. // 0NNN - oct
  138. // NNN - dec
  139. // ------------------------------------------------------------------------------------
  140. inline unsigned int strtoul_cppstyle( const char* in, const char** out=0)
  141. {
  142. if ('0' == in[0])
  143. {
  144. return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
  145. }
  146. return strtoul10(in, out);
  147. }
  148. // ------------------------------------------------------------------------------------
  149. // Special version of the function, providing higher accuracy and safety
  150. // It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
  151. // ------------------------------------------------------------------------------------
  152. inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0)
  153. {
  154. unsigned int cur = 0;
  155. uint64_t value = 0;
  156. if ( *in < '0' || *in > '9' )
  157. throw std::invalid_argument(std::string("The string \"") + in + "\" cannot be converted into a value.");
  158. bool running = true;
  159. while ( running )
  160. {
  161. if ( *in < '0' || *in > '9' )
  162. break;
  163. const uint64_t new_value = ( value * 10 ) + ( *in - '0' );
  164. if (new_value < value) /* numeric overflow, we rely on you */
  165. throw std::overflow_error(std::string("Converting the string \"") + in + "\" into a value resulted in overflow.");
  166. value = new_value;
  167. ++in;
  168. ++cur;
  169. if (max_inout && *max_inout == cur) {
  170. if (out) { /* skip to end */
  171. while (*in >= '0' && *in <= '9')
  172. ++in;
  173. *out = in;
  174. }
  175. return value;
  176. }
  177. }
  178. if (out)
  179. *out = in;
  180. if (max_inout)
  181. *max_inout = cur;
  182. return value;
  183. }
  184. // Number of relevant decimals for floating-point parsing.
  185. #define AI_FAST_ATOF_RELAVANT_DECIMALS 15
  186. // ------------------------------------------------------------------------------------
  187. //! Provides a fast function for converting a string into a float,
  188. //! about 6 times faster than atof in win32.
  189. // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
  190. // ------------------------------------------------------------------------------------
  191. template <typename Real>
  192. inline const char* fast_atoreal_move( const char* c, Real& out, bool check_comma = true)
  193. {
  194. Real f;
  195. bool inv = (*c=='-');
  196. if (inv || *c=='+') {
  197. ++c;
  198. }
  199. f = static_cast<Real>( strtoul10_64 ( c, &c) );
  200. if (*c == '.' || (check_comma && c[0] == ',' && c[1] >= '0' && c[1] <= '9')) // allow for commas, too
  201. {
  202. ++c;
  203. // NOTE: The original implementation is highly inaccurate here. The precision of a single
  204. // IEEE 754 float is not high enough, everything behind the 6th digit tends to be more
  205. // inaccurate than it would need to be. Casting to double seems to solve the problem.
  206. // strtol_64 is used to prevent integer overflow.
  207. // Another fix: this tends to become 0 for long numbers if we don't limit the maximum
  208. // number of digits to be read. AI_FAST_ATOF_RELAVANT_DECIMALS can be a value between
  209. // 1 and 15.
  210. unsigned int diff = AI_FAST_ATOF_RELAVANT_DECIMALS;
  211. double pl = static_cast<double>( strtoul10_64 ( c, &c, &diff ));
  212. pl *= fast_atof_table[diff];
  213. f += static_cast<Real>( pl );
  214. }
  215. // A major 'E' must be allowed. Necessary for proper reading of some DXF files.
  216. // Thanks to Zhao Lei to point out that this if() must be outside the if (*c == '.' ..)
  217. if (*c == 'e' || *c == 'E') {
  218. ++c;
  219. const bool einv = (*c=='-');
  220. if (einv || *c=='+') {
  221. ++c;
  222. }
  223. // The reason float constants are used here is that we've seen cases where compilers
  224. // would perform such casts on compile-time constants at runtime, which would be
  225. // bad considering how frequently fast_atoreal_move<float> is called in Assimp.
  226. Real exp = static_cast<Real>( strtoul10_64(c, &c) );
  227. if (einv) {
  228. exp = -exp;
  229. }
  230. f *= pow(static_cast<Real>(10.0), exp);
  231. }
  232. if (inv) {
  233. f = -f;
  234. }
  235. out = f;
  236. return c;
  237. }
  238. // ------------------------------------------------------------------------------------
  239. // The same but more human.
  240. inline float fast_atof(const char* c)
  241. {
  242. float ret;
  243. fast_atoreal_move<float>(c, ret);
  244. return ret;
  245. }
  246. inline float fast_atof( const char* c, const char** cout)
  247. {
  248. float ret;
  249. *cout = fast_atoreal_move<float>(c, ret);
  250. return ret;
  251. }
  252. inline float fast_atof( const char** inout)
  253. {
  254. float ret;
  255. *inout = fast_atoreal_move<float>(*inout, ret);
  256. return ret;
  257. }
  258. inline double fast_atod(const char* c)
  259. {
  260. double ret;
  261. fast_atoreal_move<double>(c, ret);
  262. return ret;
  263. }
  264. inline double fast_atod( const char* c, const char** cout)
  265. {
  266. double ret;
  267. *cout = fast_atoreal_move<double>(c, ret);
  268. return ret;
  269. }
  270. inline double fast_atod( const char** inout)
  271. {
  272. double ret;
  273. *inout = fast_atoreal_move<double>(*inout, ret);
  274. return ret;
  275. }
  276. } // end of namespace Assimp
  277. #endif