From 6f16175e67270912d08f90fa989aa5f4da8167a8 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 31 Dec 2024 07:29:44 +0100 Subject: [PATCH] add option to parse partial to parseInt() --- src/lib/ebus/symbol.cpp | 6 +++--- src/lib/ebus/symbol.h | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index 3b0e96f7..ae4588c1 100755 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -56,12 +56,12 @@ static const symbol_t CRC_LOOKUP_TABLE[] = { unsigned int parseInt(const char* str, int base, unsigned int minValue, unsigned int maxValue, - result_t* result, size_t* length) { + result_t* result, size_t* length, bool allowIncomplete) { char* strEnd = nullptr; unsigned long ret = strtoul(str, &strEnd, base); - if (strEnd == nullptr || strEnd == str || *strEnd != 0) { + if (strEnd == nullptr || strEnd == str || (!allowIncomplete && *strEnd != 0)) { *result = RESULT_ERR_INVALID_NUM; // invalid value return 0; } @@ -83,7 +83,7 @@ int parseSignedInt(const char* str, int base, int minValue, int maxValue, long ret = strtol(str, &strEnd, base); - if (strEnd == nullptr || (!allowIncomplete && *strEnd != 0)) { + if (strEnd == nullptr || strEnd == str || (!allowIncomplete && *strEnd != 0)) { *result = RESULT_ERR_INVALID_NUM; // invalid value return 0; } diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index a1523cad..4324ea5f 100755 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -91,20 +91,21 @@ enum PredefinedSymbol : symbol_t { /** * Parse an unsigned int value. * @param str the string to parse. - * @param base the numerical base. + * @param base the numerical base (or 0 to determine from the prefix). * @param minValue the minimum resulting value. * @param maxValue the maximum resulting value. * @param result the variable in which to store an error code when parsing failed or the value is out of bounds. * @param length the optional variable in which to store the number of read characters. + * @param allowIncomplete true to allow parsing less than the complete string. * @return the parsed value. */ unsigned int parseInt(const char* str, int base, unsigned int minValue, unsigned int maxValue, - result_t* result, size_t* length = nullptr); + result_t* result, size_t* length = nullptr, bool allowIncomplete = false); /** * Parse a signed int value. * @param str the string to parse. - * @param base the numerical base. + * @param base the numerical base (or 0 to determine from the prefix). * @param minValue the minimum resulting value. * @param maxValue the maximum resulting value. * @param result the variable in which to store an error code when parsing failed or the value is out of bounds.