1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
\r
5 #ifndef __FAST_ATOF_H_INCLUDED__
\r
6 #define __FAST_ATOF_H_INCLUDED__
\r
9 #include "irrString.h"
\r
15 //! Selection of characters which count as decimal point in fast_atof
\r
16 //! By default Irrlicht considers "." as the decimal point in numbers.
\r
17 //! But sometimes you might run into situations where floats were written in
\r
18 //! a local format with another decimal point like ",".
\r
19 //! Best solution is usually to fix those cases by converting the input.
\r
20 //! But if you don't have that choice you can set this to ".,".
\r
21 //! WARNING: This is not thread-safe, so don't change while there's a chance
\r
22 //! of another thread using fast_atof functions at the same time.
\r
23 // TODO: This should probably also be used in irr::core::string, but
\r
24 // the float-to-string code used there has to be rewritten first.
\r
25 IRRLICHT_API extern irr::core::stringc LOCALE_DECIMAL_POINTS;
\r
27 #define IRR_ATOF_TABLE_SIZE 17
\r
28 // we write [IRR_ATOF_TABLE_SIZE] here instead of [] to work around a swig bug
\r
29 const float fast_atof_table[17] = {
\r
49 //! Convert a simple string of base 10 digits into an unsigned 32 bit integer.
\r
50 /** \param[in] in: The string of digits to convert. No leading chars are
\r
51 allowed, only digits 0 to 9. Parsing stops at the first non-digit.
\r
52 \param[out] out: (optional) If provided, it will be set to point at the
\r
53 first character not used in the calculation.
\r
54 \return The unsigned integer value of the digits. If the string specifies
\r
55 too many digits to encode in an u32 then INT_MAX will be returned.
\r
57 inline u32 strtoul10(const char* in, const char** out=0)
\r
66 bool overflow=false;
\r
67 u32 unsignedValue = 0;
\r
68 while ( ( *in >= '0') && ( *in <= '9' ))
\r
70 const u32 tmp = ( unsignedValue * 10 ) + ( *in - '0' );
\r
71 if (tmp<unsignedValue)
\r
73 unsignedValue=(u32)0xffffffff;
\r
77 unsignedValue = tmp;
\r
84 return unsignedValue;
\r
87 //! Convert a simple string of base 10 digits into a signed 32 bit integer.
\r
88 /** \param[in] in: The string of digits to convert. Only a leading - or +
\r
89 followed by digits 0 to 9 will be considered. Parsing stops at the first
\r
91 \param[out] out: (optional) If provided, it will be set to point at the
\r
92 first character not used in the calculation.
\r
93 \return The signed integer value of the digits. If the string specifies
\r
94 too many digits to encode in an s32 then +INT_MAX or -INT_MAX will be
\r
97 inline s32 strtol10(const char* in, const char** out=0)
\r
106 const bool negative = ('-' == *in);
\r
107 if (negative || ('+' == *in))
\r
110 const u32 unsignedValue = strtoul10(in,out);
\r
111 if (unsignedValue > (u32)INT_MAX)
\r
114 return (s32)INT_MIN;
\r
116 return (s32)INT_MAX;
\r
121 return -((s32)unsignedValue);
\r
123 return (s32)unsignedValue;
\r
127 //! Convert a hex-encoded character to an unsigned integer.
\r
128 /** \param[in] in The digit to convert. Only digits 0 to 9 and chars A-F,a-f
\r
129 will be considered.
\r
130 \return The unsigned integer value of the digit. 0xffffffff if the input is
\r
133 inline u32 ctoul16(char in)
\r
135 if (in >= '0' && in <= '9')
\r
137 else if (in >= 'a' && in <= 'f')
\r
138 return 10u + in - 'a';
\r
139 else if (in >= 'A' && in <= 'F')
\r
140 return 10u + in - 'A';
\r
145 //! Convert a simple string of base 16 digits into an unsigned 32 bit integer.
\r
146 /** \param[in] in: The string of digits to convert. No leading chars are
\r
147 allowed, only digits 0 to 9 and chars A-F,a-f are allowed. Parsing stops
\r
148 at the first illegal char.
\r
149 \param[out] out: (optional) If provided, it will be set to point at the
\r
150 first character not used in the calculation.
\r
151 \return The unsigned integer value of the digits. If the string specifies
\r
152 too many digits to encode in an u32 then INT_MAX will be returned.
\r
154 inline u32 strtoul16(const char* in, const char** out=0)
\r
163 bool overflow=false;
\r
164 u32 unsignedValue = 0;
\r
168 if ((*in >= '0') && (*in <= '9'))
\r
169 tmp = (unsignedValue << 4u) + (*in - '0');
\r
170 else if ((*in >= 'A') && (*in <= 'F'))
\r
171 tmp = (unsignedValue << 4u) + (*in - 'A') + 10;
\r
172 else if ((*in >= 'a') && (*in <= 'f'))
\r
173 tmp = (unsignedValue << 4u) + (*in - 'a') + 10;
\r
176 if (tmp<unsignedValue)
\r
178 unsignedValue=(u32)INT_MAX;
\r
182 unsignedValue = tmp;
\r
189 return unsignedValue;
\r
192 //! Convert a simple string of base 8 digits into an unsigned 32 bit integer.
\r
193 /** \param[in] in The string of digits to convert. No leading chars are
\r
194 allowed, only digits 0 to 7 are allowed. Parsing stops at the first illegal
\r
196 \param[out] out (optional) If provided, it will be set to point at the
\r
197 first character not used in the calculation.
\r
198 \return The unsigned integer value of the digits. If the string specifies
\r
199 too many digits to encode in an u32 then INT_MAX will be returned.
\r
201 inline u32 strtoul8(const char* in, const char** out=0)
\r
210 bool overflow=false;
\r
211 u32 unsignedValue = 0;
\r
215 if ((*in >= '0') && (*in <= '7'))
\r
216 tmp = (unsignedValue << 3u) + (*in - '0');
\r
219 if (tmp<unsignedValue)
\r
221 unsignedValue=(u32)INT_MAX;
\r
225 unsignedValue = tmp;
\r
232 return unsignedValue;
\r
235 //! Convert a C-style prefixed string (hex, oct, integer) into an unsigned 32 bit integer.
\r
236 /** \param[in] in The string of digits to convert. If string starts with 0x the
\r
237 hex parser is used, if only leading 0 is used, oct parser is used. In all
\r
238 other cases, the usual unsigned parser is used.
\r
239 \param[out] out (optional) If provided, it will be set to point at the
\r
240 first character not used in the calculation.
\r
241 \return The unsigned integer value of the digits. If the string specifies
\r
242 too many digits to encode in an u32 then INT_MAX will be returned.
\r
244 inline u32 strtoul_prefix(const char* in, const char** out=0)
\r
253 return ('x'==in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out));
\r
254 return strtoul10(in,out);
\r
257 //! Converts a sequence of digits into a whole positive floating point value.
\r
258 /** Only digits 0 to 9 are parsed. Parsing stops at any other character,
\r
259 including sign characters or a decimal point.
\r
260 \param in: the sequence of digits to convert.
\r
261 \param out: (optional) will be set to point at the first non-converted
\r
263 \return The whole positive floating point representation of the digit
\r
266 inline f32 strtof10(const char* in, const char** out = 0)
\r
275 const u32 MAX_SAFE_U32_VALUE = UINT_MAX / 10 - 10;
\r
278 // Use integer arithmetic for as long as possible, for speed
\r
280 while ( ( *in >= '0') && ( *in <= '9' ) )
\r
282 // If it looks like we're going to overflow, bail out
\r
283 // now and start using floating point.
\r
284 if (intValue >= MAX_SAFE_U32_VALUE)
\r
287 intValue = (intValue * 10) + (*in - '0');
\r
291 f32 floatValue = (f32)intValue;
\r
293 // If there are any digits left to parse, then we need to use
\r
294 // floating point arithmetic from here.
\r
295 while ( ( *in >= '0') && ( *in <= '9' ) )
\r
297 floatValue = (floatValue * 10.f) + (f32)(*in - '0');
\r
299 if (floatValue > FLT_MAX) // Just give up.
\r
309 //! Provides a fast function for converting a string into a float.
\r
310 /** This is not guaranteed to be as accurate as atof(), but is
\r
311 approximately 6 to 8 times as fast.
\r
312 \param[in] in The string to convert.
\r
313 \param[out] result The resultant float will be written here.
\r
314 \return Pointer to the first character in the string that wasn't used
\r
315 to create the float value.
\r
317 inline const char* fast_atof_move(const char* in, f32& result)
\r
319 // Please run the regression test when making any modifications to this function.
\r
325 const bool negative = ('-' == *in);
\r
326 if (negative || ('+'==*in))
\r
329 f32 value = strtof10(in, &in);
\r
331 if ( LOCALE_DECIMAL_POINTS.findFirst(*in) >= 0 )
\r
333 const char* afterDecimal = ++in;
\r
334 const f32 decimal = strtof10(in, &afterDecimal);
\r
335 const size_t numDecimals = afterDecimal - in;
\r
336 if (numDecimals < IRR_ATOF_TABLE_SIZE)
\r
338 value += decimal * fast_atof_table[numDecimals];
\r
342 value += decimal * (f32)pow(10.f, -(float)numDecimals);
\r
347 if ('e' == *in || 'E' == *in)
\r
350 // Assume that the exponent is a whole number.
\r
351 // strtol10() will deal with both + and - signs,
\r
352 // but calculate as f32 to prevent overflow at FLT_MAX
\r
353 // Using pow with float cast instead of powf as otherwise accuracy decreases.
\r
354 value *= (f32)pow(10.f, (f32)strtol10(in, &in));
\r
357 result = negative?-value:value;
\r
361 //! Convert a string to a floating point number
\r
362 /** \param floatAsString The string to convert.
\r
363 \param out Optional pointer to the first character in the string that
\r
364 wasn't used to create the float value.
\r
365 \result Float value parsed from the input string
\r
367 inline float fast_atof(const char* floatAsString, const char** out=0)
\r
371 *out=fast_atof_move(floatAsString, ret);
\r
373 fast_atof_move(floatAsString, ret);
\r
377 } // end namespace core
\r
378 } // end namespace irr
\r