From 1b84807eb07e430e623202c8978c5c35530face5 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 23 Jan 2018 20:04:50 -0600 Subject: [PATCH] Fix has_value with FASTER_GCODE_PARSER --- Marlin/gcode.cpp | 9 +-------- Marlin/gcode.h | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Marlin/gcode.cpp b/Marlin/gcode.cpp index 4f82c7537..240b1cff9 100644 --- a/Marlin/gcode.cpp +++ b/Marlin/gcode.cpp @@ -189,14 +189,7 @@ void GCodeParser::parse(char *p) { while (*p == ' ') p++; // Skip spaces between parameters & values - const bool has_num = NUMERIC(p[0]) // [0-9] - || (p[0] == '.' && NUMERIC(p[1])) // .[0-9] - || ( - (p[0] == '-' || p[0] == '+') && ( // [-+] - NUMERIC(p[1]) // [0-9] - || (p[1] == '.' && NUMERIC(p[2])) // .[0-9] - ) - ); + const bool has_num = valid_float(p); #if ENABLED(DEBUG_GCODE_PARSER) if (debug) { diff --git a/Marlin/gcode.h b/Marlin/gcode.h index 3ad211390..474a47dd9 100644 --- a/Marlin/gcode.h +++ b/Marlin/gcode.h @@ -108,6 +108,18 @@ public: #if ENABLED(FASTER_GCODE_PARSER) + FORCE_INLINE static bool valid_signless(const char * const p) { + return NUMERIC(p[0]) || (p[0] == '.' && NUMERIC(p[1])); // .?[0-9] + } + + FORCE_INLINE static bool valid_float(const char * const p) { + return valid_signless(p) || ((p[0] == '-' || p[0] == '+') && valid_signless(&p[1])); // [-+]?.?[0-9] + } + + FORCE_INLINE static bool valid_int(const char * const p) { + return NUMERIC(p[0]) || ((p[0] == '-' || p[0] == '+') && NUMERIC(p[1])); // [-+]?[0-9] + } + // Set the flag and pointer for a parameter static void set(const char c, char * const ptr #if ENABLED(DEBUG_GCODE_PARSER) @@ -133,7 +145,10 @@ public: const uint8_t ind = LETTER_OFF(c); if (ind >= COUNT(param)) return false; // Only A-Z const bool b = TEST(codebits[PARAM_IND(ind)], PARAM_BIT(ind)); - if (b) value_ptr = param[ind] ? command_ptr + param[ind] : (char*)NULL; + if (b) { + const char * const ptr = command_ptr + param[ind]; + value_ptr = param[ind] && valid_float(ptr) ? ptr : (char*)NULL; + } return b; } @@ -148,7 +163,7 @@ public: static bool seen(const char c) { const char *p = strchr(command_args, c); const bool b = !!p; - if (b) value_ptr = DECIMAL_SIGNED(p[1]) ? &p[1] : (char*)NULL; + if (b) value_ptr = valid_float(&p[1]) ? &p[1] : (char*)NULL; return b; } @@ -212,7 +227,7 @@ public: inline static uint8_t value_byte() { return (uint8_t)constrain(value_long(), 0, 255); } // Bool is true with no value or non-zero - inline static bool value_bool() { return !has_value() || value_byte(); } + inline static bool value_bool() { return !has_value() || !!value_byte(); } // Units modes: Inches, Fahrenheit, Kelvin