Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members

fast_atof.h

Go to the documentation of this file.
00001 // Copyright (C) 2002-2008 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
00004 
00005 #ifndef __FAST_A_TO_F_H_INCLUDED__
00006 #define __FAST_A_TO_F_H_INCLUDED__
00007 
00008 #include <stdlib.h>
00009 #include "irrMath.h"
00010 
00011 namespace irr
00012 {
00013 namespace core
00014 {
00015 
00016 // we write [16] here instead of [] to work around a swig bug
00017 const float fast_atof_table[16] = {
00018         0.f,
00019         0.1f,
00020         0.01f,
00021         0.001f,
00022         0.0001f,
00023         0.00001f,
00024         0.000001f,
00025         0.0000001f,
00026         0.00000001f,
00027         0.000000001f,
00028         0.0000000001f,
00029         0.00000000001f,
00030         0.000000000001f,
00031         0.0000000000001f,
00032         0.00000000000001f,
00033         0.000000000000001f
00034 };
00035 
00036 inline u32 strtol10(const char* in, const char** out=0)
00037 {
00038         u32 value = 0;
00039 
00040         while ( 1 )
00041         {
00042                 if ( *in < '0' || *in > '9' )
00043                         break;
00044 
00045                 value = ( value * 10 ) + ( *in - '0' );
00046                 ++in;
00047         }
00048         if (out)
00049                 *out = in;
00050         return value;
00051 }
00052 
00055 // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
00056 inline const char* fast_atof_move( const char* c, float& out)
00057 {
00058         bool inv = false;
00059         const char *t;
00060         float f;
00061 
00062         if (*c=='-')
00063         {
00064                 ++c;
00065                 inv = true;
00066         }
00067 
00068         //f = (float)strtol(c, &t, 10);
00069         f = (float) strtol10 ( c, &c );
00070 
00071         if (*c == '.')
00072         {
00073                 ++c;
00074 
00075                 //float pl = (float)strtol(c, &t, 10);
00076                 float pl = (float) strtol10 ( c, &t );
00077                 pl *= fast_atof_table[t-c];
00078 
00079                 f += pl;
00080 
00081                 c = t;
00082 
00083                 if (*c == 'e')
00084                 {
00085                         ++c;
00086                         //float exp = (float)strtol(c, &t, 10);
00087                         bool einv = (*c=='-');
00088                         if (einv)
00089                                 ++c;
00090 
00091                         float exp = (float)strtol10(c, &c);
00092                         if (einv)
00093                                 exp *= -1.0f;
00094 
00095                         f *= (float)pow(10.0f, exp);
00096                 }
00097         }
00098 
00099         if (inv)
00100                 f *= -1.0f;
00101 
00102         out = f;
00103         return c;
00104 }
00105 
00106 inline float fast_atof(const char* c)
00107 {
00108         float ret;
00109         fast_atof_move(c, ret);
00110         return ret;
00111 }
00112 
00113 } // end namespace core
00114 } // end namespace irr
00115 
00116 #endif
00117 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2008 by Nikolaus Gebhardt. Generated on Sun Sep 21 08:57:41 2008 by Doxygen (1.4.2)