arduino / arduino/ArduinoCore-API

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

Open
#70 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in api/String.cpp around the linked String::toInt() and String::toFloat() implementations, then review the related Arduino issues for expected parsing behavior. Done means String::isInt() and String::isFloat() are available and distinguish valid representations from invalid input without conflating conversion failure with a numeric zero.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, embedded-iot
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.