MCPcopy Create free account
hub / github.com/anjo76/angelscript / asStringScanDouble

Function asStringScanDouble

sdk/angelscript/source/as_string_util.cpp:77–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75}
76
77double asStringScanDouble(const char *string, size_t *numScanned)
78{
79 // I decided to do my own implementation of strtod() because this function
80 // doesn't seem to be present on all systems. iOS 5 for example doesn't appear
81 // to include the function in the standard lib.
82
83 // Another reason is that the standard implementation of strtod() is dependent
84 // on the locale on some systems, i.e. it may use comma instead of dot for
85 // the decimal indicator. This can be avoided by forcing the locale to "C" with
86 // setlocale(), but this is another thing that is highly platform dependent.
87
88 double value = 0;
89 double fraction = 0.1;
90 int exponent = 0;
91 bool negativeExponent = false;
92 int c = 0;
93
94 // The tokenizer separates the sign from the number in
95 // two tokens so we'll never have a sign to parse here
96
97 // Parse the integer value
98 for( ;; )
99 {
100 if( string[c] >= '0' && string[c] <= '9' )
101 value = value*10 + double(string[c] - '0');
102 else
103 break;
104
105 c++;
106 }
107
108 if( string[c] == '.' )
109 {
110 c++;
111
112 // Parse the fraction
113 for( ;; )
114 {
115 if( string[c] >= '0' && string[c] <= '9' )
116 value += fraction * double(string[c] - '0');
117 else
118 break;
119
120 c++;
121 fraction *= 0.1;
122 }
123 }
124
125 if( string[c] == 'e' || string[c] == 'E' )
126 {
127 c++;
128
129 // Parse the sign of the exponent
130 if( string[c] == '-' )
131 {
132 negativeExponent = true;
133 c++;
134 }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected