From 7a9d1bf7e20bd6504f7cdb824fef526847bb7b96 Mon Sep 17 00:00:00 2001 From: "Matr1x." Date: Sun, 17 Nov 2024 00:01:42 +0800 Subject: [PATCH 1/2] Return false in HasWord() when letter is not a valid word --- src/GCodeParser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GCodeParser.cpp b/src/GCodeParser.cpp index 72e9ff1..0137157 100644 --- a/src/GCodeParser.cpp +++ b/src/GCodeParser.cpp @@ -367,9 +367,9 @@ bool GCodeParser::HasWord(char letter) { return false; } + return true; } - - return true; + return false; } char wordLetter[] = { 'A', 'B', 'C', 'D', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' }; From 2937c5a7a01afceb3f86a8350ae389b45978c0b1 Mon Sep 17 00:00:00 2001 From: "Matr1x." Date: Sun, 17 Nov 2024 00:03:55 +0800 Subject: [PATCH 2/2] Store the result from HasWord(), and use it in GetWordValue() This helps to prevent an extra useless while() search loop --- src/GCodeParser.cpp | 13 ++++++++++++- src/GCodeParser.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/GCodeParser.cpp b/src/GCodeParser.cpp index 0137157..bcbb5f6 100644 --- a/src/GCodeParser.cpp +++ b/src/GCodeParser.cpp @@ -32,6 +32,8 @@ SOFTWARE. void GCodeParser::Initialize() { lineCharCount = 0; + lastPointer = -1; + lastWord = '\0'; line[lineCharCount] = '\0'; comments = line; lastComment = comments; @@ -367,6 +369,8 @@ bool GCodeParser::HasWord(char letter) { return false; } + lastPointer = pointer; + lastWord = letter; return true; } return false; @@ -470,7 +474,14 @@ bool GCodeParser::NoWords() /// double GCodeParser::GetWordValue(char letter) { - int pointer = FindWord(letter); + int pointer = 0; + if(lastWord == letter && lastPointer >= 0) pointer = lastPointer; + else + { + pointer = FindWord(letter); + lastWord = letter; + lastPointer = pointer; + } if (line[pointer] != '\0') return (double)strtod(&line[pointer + 1], NULL); diff --git a/src/GCodeParser.h b/src/GCodeParser.h index ddceddf..2f9a60a 100644 --- a/src/GCodeParser.h +++ b/src/GCodeParser.h @@ -51,6 +51,8 @@ class GCodeParser { private: int lineCharCount; + int lastPointer = -1; + char lastWord = '\0'; public: char line[MAX_LINE_SIZE + 2];