diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-14 09:54:59 -0400 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-14 09:54:59 -0400 |
commit | abac70416024801c7936c6cdada504cdf2f2fe61 (patch) | |
tree | a380fce849bba8224f5c4e0ea705ab08b83ab82e /string_utils.c | |
parent | 90a44417bb18ef3bfd5cf6c028a47add217fb339 (diff) |
use pcre regex to implement String_is_it_numeric
Diffstat (limited to 'string_utils.c')
-rw-r--r-- | string_utils.c | 56 |
1 files changed, 6 insertions, 50 deletions
diff --git a/string_utils.c b/string_utils.c index cbe310f..67ad827 100644 --- a/string_utils.c +++ b/string_utils.c @@ -183,56 +183,12 @@ int String_trim_excess_digits(char *parameter) /*----------------------------------------------------------------------------------------------------------*/
-int String_is_it_numeric(char *parameter)
-{
- /* this function takes a parameter like "1e+6" or "on" and determines if it is numeric or not */
- /* it is similar to the Parser_get_unit function */
-
- int i;
- int j;
- int is_number;
-
- is_number=0;
- i=0;
-
-
- if (isdigit(parameter[0]) || parameter[0]=='+' || parameter[0] == '-' || parameter[0] == '.') {
- for (i=1; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
-
- if (i < strlen(parameter))
- if ( parameter[i]=='.' )
- for (++i; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
+gboolean String_is_it_numeric(char *parameter) {
- /* suck out spaces */
- while ( (i<strlen(parameter)) && (isspace(parameter[i])) ) {
- for(j=i; j<strlen(parameter); ++j) {
- parameter[j]=parameter[j+1];
- }
- parameter[j]=0;
- }
-
- if (i < strlen(parameter))
- if ( (parameter[i]=='e' && parameter[i+1]=='-')
- || (parameter[i]=='e' && parameter[i+1]=='+') )
- for (i+=2; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
- else if (parameter[i]=='e' && isdigit(parameter[i+1]) )
- for (i+=2; (i < strlen(parameter)) && isdigit(parameter[i]); ++i) {}
-
- /* suck out spaces */
- while ( (i<strlen(parameter)) && (isspace(parameter[i])) ) {
- for(j=i; j<strlen(parameter); ++j) {
- parameter[j]=parameter[j+1];
- }
- parameter[j]=0;
- }
-
- if (i = strlen(parameter)) {
- is_number=1;
- parameter[i]=0;
- }
- }
+ GRegex *numeric_regex = g_regex_new ( "\\s*[+-]?(\\d*\\.\\d+|\\d+(\\.\\d*)?)\\s*(e\\s*[+-]?\\d+)?\\s*",
+ G_REGEX_CASELESS,
+ 0,
+ NULL);
- return is_number;
+ return g_regex_match (numeric_regex, parameter, 0, NULL);
}
-
-
|