src/misc.cpp

changeset 256
9f7e6e288953
parent 223
4f95d7f2e9ef
child 257
481566b60ecd
equal deleted inserted replaced
255:67d4aedf1041 256:9f7e6e288953
135 str ftoa (double num) { 135 str ftoa (double num) {
136 // Disable the locale first so that the decimal point will not 136 // Disable the locale first so that the decimal point will not
137 // turn into anything weird (like commas) 137 // turn into anything weird (like commas)
138 setlocale (LC_NUMERIC, "C"); 138 setlocale (LC_NUMERIC, "C");
139 139
140 str zRep = fmt ("%f", num); 140 str rep = fmt ("%f", num);
141 141
142 // Remove trailing zeroes 142 // Remove trailing zeroes
143 while (zRep[~zRep - 1] == '0') 143 while (rep[~rep - 1] == '0')
144 zRep -= 1; 144 rep -= 1;
145 145
146 // If there was only zeroes in the decimal place, remove 146 // If there was only zeroes in the decimal place, remove
147 // the decimal point now. 147 // the decimal point now.
148 if (zRep[~zRep - 1] == '.') 148 if (rep[~rep - 1] == '.')
149 zRep -= 1; 149 rep -= 1;
150 150
151 return zRep; 151 return rep;
152 } 152 }
153 153
154 // ============================================================================= 154 // =============================================================================
155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
156 // ============================================================================= 156 // =============================================================================
157 bool isNumber (str& tok) { 157 bool isNumber (const str& tok) {
158 char* cpPointer = &tok[0]; 158 char* ptr = &tok[0];
159 bool bGotDot = false; 159 bool gotDot = false;
160 160
161 // Allow leading hyphen for negatives 161 for (const char& c : tok) {
162 if (*cpPointer == '-') 162 // Allow leading hyphen for negatives
163 cpPointer++; 163 if (&c == &tok[0] && c == '-')
164 164 continue;
165 while (*cpPointer != '\0') { 165
166 if (*cpPointer == '.' && !bGotDot) { 166 // Check for decimal point
167 // Decimal point 167 if (!gotDot && c == '.') {
168 bGotDot = true; 168 gotDot = true;
169 cpPointer++;
170 continue; 169 continue;
171 } 170 }
172 171
173 if (*cpPointer >= '0' && *cpPointer <= '9') { 172 if (c >= '0' && c <= '9')
174 cpPointer++;
175 continue; // Digit 173 continue; // Digit
176 }
177 174
178 // If the above cases didn't catch this character, it was 175 // If the above cases didn't catch this character, it was
179 // illegal and this is therefore not a number. 176 // illegal and this is therefore not a number.
180 return false; 177 return false;
181 } 178 }

mercurial