rewrote isNumber using range-for

Sat, 25 May 2013 01:44:11 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 25 May 2013 01:44:11 +0300
changeset 256
9f7e6e288953
parent 255
67d4aedf1041
child 257
481566b60ecd

rewrote isNumber using range-for

src/misc.cpp file | annotate | diff | comparison | revisions
src/misc.h file | annotate | diff | comparison | revisions
--- a/src/misc.cpp	Fri May 24 20:38:55 2013 +0300
+++ b/src/misc.cpp	Sat May 25 01:44:11 2013 +0300
@@ -137,43 +137,40 @@
 	// turn into anything weird (like commas)
 	setlocale (LC_NUMERIC, "C");
 	
-	str zRep = fmt ("%f", num);
+	str rep = fmt ("%f", num);
 	
 	// Remove trailing zeroes
-	while (zRep[~zRep - 1] == '0')
-		zRep -= 1;
+	while (rep[~rep - 1] == '0')
+		rep -= 1;
 	
 	// If there was only zeroes in the decimal place, remove
 	// the decimal point now.
-	if (zRep[~zRep - 1] == '.')
-		zRep -= 1;
+	if (rep[~rep - 1] == '.')
+		rep -= 1;
 	
-	return zRep;
+	return rep;
 }
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-bool isNumber (str& tok) {
-	char* cpPointer = &tok[0];
-	bool bGotDot = false;
+bool isNumber (const str& tok) {
+	char* ptr = &tok[0];
+	bool gotDot = false;
 	
-	// Allow leading hyphen for negatives
-	if (*cpPointer == '-')
-		cpPointer++;
-	
-	while (*cpPointer != '\0') {
-		if (*cpPointer == '.' && !bGotDot) {
-			// Decimal point
-			bGotDot = true;
-			cpPointer++;
+	for (const char& c : tok) {
+		// Allow leading hyphen for negatives
+		if (&c == &tok[0] && c == '-')
+			continue;
+		
+		// Check for decimal point
+		if (!gotDot && c == '.') {
+			gotDot = true;
 			continue;
 		}
 		
-		if (*cpPointer >= '0' && *cpPointer <= '9') {
-			cpPointer++;
+		if (c >= '0' && c <= '9')
 			continue; // Digit
-		}
 		
 		// If the above cases didn't catch this character, it was
 		// illegal and this is therefore not a number.
--- a/src/misc.h	Fri May 24 20:38:55 2013 +0300
+++ b/src/misc.h	Sat May 25 01:44:11 2013 +0300
@@ -31,7 +31,7 @@
 extern const ushort g_primes[NUM_PRIMES];
 
 // Returns whether a given string represents a floating point number.
-bool isNumber (str& tok);
+bool isNumber (const str& tok);
 
 // Converts a float value to a string value.
 str ftoa (double num);

mercurial