arduino / arduino/ArduinoCore-API
Add `String::isInt()` and `String::isFloat()` for more robust String parsing/conversion
- Vorherrschende Sprache
- C++
- Sterne
- 306
- Forks
- 150
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
### Describe the request
I propose adding two new member functions to the `String` class:
```cpp
int String::isInt()
```
Returns a value different from zero if `String` is a valid representation of an integer.
Returns zero otherwise.
```cpp
int String::isFloat()
```
Returns a value different from zero if `String` is a valid representation of a floating point number.
Returns zero otherwise.
🙂 The user will be able to validate a `String` prior to attempting a conversion to `int` or `float`
### Describe the current behavior
The current implementations of `String::toInt()` and `String::toFloat()` rely on `atol()` and `atof()`, respectively:
https://github.com/arduino/ArduinoCore-API/blob/0f4e57ea193a00163ae59f0f0ff478feae7eb5db/api/String.cpp#L733-L748
Both `atol()` and `atof()` have no means of signaling a failed conversion due to improper input `String`s (both return `0` upon error).
🙁 The functions do not provide the user with an indication of a failed conversion.
### ArduinoCore-API version
Last verified with: 0f4e57ea193a00163ae59f0f0ff478feae7eb5db
### Additional context
#### Related
- https://github.com/arduino/Arduino/issues/1796
- https://github.com/arduino/Arduino/issues/7177
- https://github.com/arduino/ArduinoCore-API/issues/88
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Beginne in api/String.cpp bei den verlinkten Implementierungen von String::toInt() und String::toFloat() und prüfe anschließend die zugehörigen Arduino-Issues auf das erwartete Parsing-Verhalten. Die Aufgabe ist abgeschlossen, wenn String::isInt() und String::isFloat() verfügbar sind und gültige Darstellungen von ungültigen Eingaben unterscheiden, ohne einen Konvertierungsfehler mit dem numerischen Wert null gleichzusetzen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- cpp
- Bereich
- api, embedded-iot
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100