Optimized string-to-number functions (#21484)

This commit is contained in:
Ramiro Polla
2021-03-31 04:20:33 +02:00
committed by GitHub
parent df297b6ca3
commit 20d2061f22
4 changed files with 153 additions and 26 deletions

View File

@@ -42,6 +42,10 @@
typedef enum : uint8_t { LINEARUNIT_MM, LINEARUNIT_INCH } LinearUnit;
#endif
int32_t parse_int32(const char *buf);
float parse_float(const char *buf);
/**
* GCode parser
*
@@ -256,29 +260,12 @@ public:
// The value as a string
static inline char* value_string() { return value_ptr; }
// Float removes 'E' to prevent scientific notation interpretation
static inline float value_float() {
if (value_ptr) {
char *e = value_ptr;
for (;;) {
const char c = *e;
if (c == '\0' || c == ' ') break;
if (c == 'E' || c == 'e') {
*e = '\0';
const float ret = strtof(value_ptr, nullptr);
*e = c;
return ret;
}
++e;
}
return strtof(value_ptr, nullptr);
}
return 0;
}
// Code value as float
static inline float value_float() { return value_ptr ? parse_float(value_ptr) : 0.0; }
// Code value as a long or ulong
static inline int32_t value_long() { return value_ptr ? strtol(value_ptr, nullptr, 10) : 0L; }
static inline uint32_t value_ulong() { return value_ptr ? strtoul(value_ptr, nullptr, 10) : 0UL; }
static inline int32_t value_long() { return value_ptr ? parse_int32(value_ptr) : 0L; }
static inline uint32_t value_ulong() { return value_ptr ? parse_int32(value_ptr) : 0UL; }
// Code value for use as time
static inline millis_t value_millis() { return value_ulong(); }