arduino / arduino/ArduinoCore-API

Add `String::isInt()` and `String::isFloat()` for more robust String parsing/conversion

Ouverte
#70 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
C++
Étoiles
306
Forks
150
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

### 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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez dans api/String.cpp, autour des implémentations liées de String::toInt() et String::toFloat(), puis examinez les issues Arduino associées pour connaître le comportement d’analyse attendu. Le travail est terminé lorsque String::isInt() et String::isFloat() sont disponibles et distinguent les représentations valides des entrées invalides sans confondre un échec de conversion avec un zéro numérique.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
cpp
Domaine
api, embedded-iot
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.