arduino / arduino/ArduinoCore-API

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

オープン
#70 コメント 1 件 リアクション 0 件 担当者 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 の issue を確認します。String::isInt() と String::isFloat() が利用可能になり、変換失敗を数値のゼロと混同せずに、有効な表現と無効な入力を区別できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
cpp
領域
api, embedded-iot
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。