From 2232362cb171569d595be421bcdab7acf56fcb97 Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Tue, 7 Jan 2014 22:34:35 -0200 Subject: [PATCH] Implement a version of switch_is_number --- cspeech.cc | 19 +++++++++++++++++++ cspeech.h | 1 + cspeech/srgs.cc | 6 +++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cspeech.cc b/cspeech.cc index 295f926..6fe01c9 100644 --- a/cspeech.cc +++ b/cspeech.cc @@ -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; +} diff --git a/cspeech.h b/cspeech.h index a3f34e7..5ca1004 100644 --- a/cspeech.h +++ b/cspeech.h @@ -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, diff --git a/cspeech/srgs.cc b/cspeech/srgs.cc index 7209df0..435205d 100644 --- a/cspeech/srgs.cc +++ b/cspeech/srgs.cc @@ -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) { @@ -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 @@ -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, " weight is not a number >= 0\n"); }