arduino / arduino/ArduinoCore-API

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

Đang mở
#70 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
C++
Star
306
Fork
150
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu trong api/String.cpp, tại phần xung quanh các triển khai được liên kết của String::toInt() và String::toFloat(), sau đó xem lại các issue liên quan của Arduino để xác định hành vi parsing mong đợi. Hoàn thành khi String::isInt() và String::isFloat() khả dụng và phân biệt được các biểu diễn hợp lệ với đầu vào không hợp lệ mà không đánh đồng lỗi chuyển đổi với số 0.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
cpp
Lĩnh vực
api, embedded-iot
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.