summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorV. Guruprasad <prasad@inspiredresearch.com>2020-09-10 06:29:07 -0400
committerV. Guruprasad <prasad@inspiredresearch.com>2020-09-10 07:35:26 -0400
commitb6890d4a860da1345c1d2b8abfe2590776e3ef1e (patch)
tree56856b975fab4e842e06e688699c85979a8e521a
parentbdd936a12c68f74419e3307760439335e1b18133 (diff)
fix isnumeric test for floats: affects copy/paste
-rwxr-xr-xsrc/utils/string.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/utils/string.c b/src/utils/string.c
index 45b269e..f3280dc 100755
--- a/src/utils/string.c
+++ b/src/utils/string.c
@@ -350,10 +350,11 @@ char * ltrim(char * string, char junk) {
int isnumeric(char * string) {
int i, len = strlen(string);
- int res = true;
+ int result = true;
bool has_dot = false;
- bool has_dash = false;
+ bool has_sign = false;
bool has_digit = false;
+ bool has_exponent = false;
for (i=0; i<len; i++) {
if ( string[i] == '.' && ! has_dot ) {
@@ -361,15 +362,21 @@ int isnumeric(char * string) {
continue;
}
- if ( string[i] == '-' ) {
- if (has_digit) {
- res = false;
+ if ( (string[i] == 'e' || string[i] == 'E') && ! has_exponent ) {
+ has_exponent = true;
+ has_sign = false; // allow sign for exponent
+ continue;
+ }
+
+ if ( (string[i] == '-' || string[i] == '+') ) {
+ if (has_digit && ! has_exponent ) {
+ result = false;
break;
}
- if (! has_dash) {
- has_dash = true;
+ if (! has_sign) {
+ has_sign = true;
} else {
- res = false;
+ result = false;
break;
}
continue;
@@ -380,10 +387,10 @@ int isnumeric(char * string) {
continue;
}
- res = false;
+ result = false;
break;
}
- return res;
+ return result;
}
/**