Skip to content

Commit

Permalink
Implement a version of switch_is_number
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Jan 8, 2014
1 parent d988f12 commit 2232362
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions cspeech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ static inline int cspeech_zstr(const char *s)
{
return !s || *s == '\0';
}

static inline bool cspeech_is_number(const char *str)
{
const char *p;
bool r = true;

if (*str == '-' || *str == '+') {
str++;
}

for (p = str; p && *p; p++) {
if (!(*p == '.' || (*p > 47 && *p < 58))) {
r = false;
break;
}
}

return r;
}
1 change: 1 addition & 0 deletions cspeech.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define CSPEECH_H_

static inline int cspeech_zstr(const char *s);
static inline bool cspeech_is_number(const char *str);

typedef enum {
CSPEECH_LOG_DEBUG = 7,
Expand Down
6 changes: 3 additions & 3 deletions cspeech/srgs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ static int process_item(struct srgs_grammar *grammar, char **atts)
}
return IKS_BADXML;
}
if (switch_is_number(repeat)) {
if (cspeech_is_number(repeat)) {
/* single number */
int repeat_val = atoi(repeat);
if (repeat_val < 1) {
Expand All @@ -633,7 +633,7 @@ static int process_item(struct srgs_grammar *grammar, char **atts)
}
return IKS_BADXML;
}
if (switch_is_number(min) && (switch_is_number(max) || cspeech_zstr(max))) {
if (cspeech_is_number(min) && (cspeech_is_number(max) || cspeech_zstr(max))) {
int min_val = atoi(min);
int max_val = cspeech_zstr(max) ? INT_MAX : atoi(max);
/* max must be >= min and > 0
Expand All @@ -655,7 +655,7 @@ static int process_item(struct srgs_grammar *grammar, char **atts)
}
} else if (!strcmp("weight", atts[i])) {
const char *weight = atts[i + 1];
if (cspeech_zstr(weight) || !switch_is_number(weight) || atof(weight) < 0) {
if (cspeech_zstr(weight) || !cspeech_is_number(weight) || atof(weight) < 0) {
if(globals.logging_callback) {
globals.logging_callback(grammar, CSPEECH_LOG_INFO, "<item> weight is not a number >= 0\n");
}
Expand Down

0 comments on commit 2232362

Please sign in to comment.