arduino / arduino/ArduinoCore-API

Make String::copy public

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

描述

I have a small feature request (and I am happy to provide the PR ), but why isn't .copy public? There is a use-case here which makes a lot of sense -- specifically when you are given a pointer to a char* that is not null-terminated (and instead are provided a length).

This is exactly how the PubSubClient MQTT library works. The topic callback provides you a byte *payload and length, but because it's byte * it's not null-terminated.

Currently you have to do this (or something like it) to get a String object:

```
void mqttCallback(char *topic, byte *payload, unsigned int length) {
String action;
char *payloadStr;

payloadStr = (char *)malloc(length + 1);
memcpy(payloadStr, payload, length);
payloadStr[length] = 0x0;
action = String(payloadStr);
free(payloadStr);
}
```

When what would make a lot more sense (since String doesn't provide a constructor for this) would be:

```
void mqttCallback(char *topic, byte *payload, unsigned int length) {
String action;
action.copy((char *)payload, length);
}
```

贡献指南

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

调研方向

从 Arduino String API 开始,检查现有的 String::copy 接口和实现。验证它如何处理 char 指针和长度,然后确认无需临时分配即可复制未以 null 结尾的 PubSubClient 载荷,并且现有的 String 行为保持不变。

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

评估

技术栈
cpp
领域
api
Issue 类型
功能
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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