arduino / arduino/ArduinoCore-API

Make String::copy public

Offen
#84 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
306
Forks
150
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginnen Sie bei der Arduino String API und prüfen Sie die vorhandene Schnittstelle und Implementierung von String::copy. Überprüfen Sie, wie sie einen char-Zeiger und eine Länge verarbeitet, und bestätigen Sie anschließend, dass eine nicht nullterminierte PubSubClient-Nutzlast ohne temporäre Speicherzuweisung kopiert werden kann und das bestehende String-Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp
Bereich
api
Issue-Typ
Feature
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.