arduino / arduino/ArduinoCore-API

Make String::copy public

Open
#84 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

Description

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);
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the Arduino String API and inspect the existing String::copy interface and implementation. Verify how it handles a char pointer and length, then confirm that a non-null-terminated PubSubClient payload can be copied without temporary allocation and that the existing String behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.