arduino / arduino/ArduinoCore-API

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

未关闭
#70 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement
主要语言
C++
星标
306
派生
150
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 api/String.cpp 中链接的 String::toInt() 和 String::toFloat() 实现附近开始,然后查看相关的 Arduino issues,了解预期的解析行为。当 String::isInt() 和 String::isFloat() 可用,并且能够区分有效表示和无效输入,而不会将转换失败与数值零混为一谈时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
cpp
领域
api, embedded-iot
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。