Skip to content

Commit

Permalink
[Rules] Fix matching events with first eventvalue being zero (letscon…
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Aug 16, 2022
1 parent 9934853 commit 69632bf
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/src/Helpers/Numerical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ bool mustConsiderAsJSONString(const String& value) {
return !isBool && (!isNum || value.isEmpty() || mustConsiderAsString(detectedType));
}

String getNumerical(const String& tBuf, NumericalType requestedType, NumericalType& detectedType) {
String doGetNumerical(const String& tBuf, NumericalType requestedType, NumericalType& detectedType) {
const unsigned int bufLength = tBuf.length();
unsigned int firstDec = 0;
String result;
Expand All @@ -183,26 +183,6 @@ String getNumerical(const String& tBuf, NumericalType requestedType, NumericalTy
++firstDec;
}

// Strip leading zeroes until next char is a '.' or one of the "0x" and "0b" prefixes
// or the zero remains as last character of the string
// See: https://github.com/letscontrolit/ESPEasy/issues/4134
bool doneStrippingZeroes = false;

while ((firstDec + 1) < bufLength && tBuf.charAt(firstDec) == '0' && !doneStrippingZeroes) {
const char nextChar = tBuf.charAt(firstDec + 1);

if ((nextChar != '.') &&
(nextChar != '+') && (nextChar != '-') && // +/-
(nextChar != 'x') && (nextChar != 'X') && // Matching "0x"
(nextChar != 'b') && (nextChar != 'B')) // Matching "0b"
{
++firstDec;
}
else {
doneStrippingZeroes = true;
}
}

if (firstDec >= bufLength) {
detectedType = NumericalType::Not_a_number;
return result;
Expand Down Expand Up @@ -327,6 +307,38 @@ String getNumerical(const String& tBuf, NumericalType requestedType, NumericalTy
return result;
}

String getNumerical(const String& tBuf, NumericalType requestedType, NumericalType& detectedType) {
String numString = doGetNumerical(tBuf, requestedType, detectedType);
const unsigned int numStringLength = numString.length();
unsigned int firstDec = 0;


// Strip leading zeroes until next char is a '.' or one of the "0x" and "0b" prefixes
// or the zero remains as last character of the string
// See: https://github.com/letscontrolit/ESPEasy/issues/4134
bool doneStrippingZeroes = numStringLength <= 1;

while ((firstDec + 1) < numStringLength && numString.charAt(firstDec) == '0' && !doneStrippingZeroes) {
const char nextChar = numString.charAt(firstDec + 1);

if ((nextChar != '\0') && (nextChar != '.') &&
(nextChar != '+') && (nextChar != '-') && // +/-
(nextChar != 'x') && (nextChar != 'X') && // Matching "0x"
(nextChar != 'b') && (nextChar != 'B')) // Matching "0b"
{
++firstDec;
}
else {
doneStrippingZeroes = true;
}
}
if (firstDec == 0) {
return numString;
}
return numString.substring(firstDec);
}


bool isNumerical(const String& tBuf, NumericalType& detectedType) {
NumericalType requestedType = NumericalType::FloatingPoint;
const String result = getNumerical(tBuf, requestedType, detectedType);
Expand Down

0 comments on commit 69632bf

Please sign in to comment.