arduino / arduino/ArduinoCore-API

Make String::copy public

Ouverte
#84 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
C++
Étoiles
306
Forks
150
Métriques de merge des PR
Aucune PR mergée en 30 j

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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez par l’API Arduino String et examinez l’interface et l’implémentation existantes de String::copy. Vérifiez comment elle gère un pointeur char et une longueur, puis confirmez qu’une charge utile PubSubClient non terminée par null peut être copiée sans allocation temporaire et que le comportement existant de String reste inchangé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
cpp
Domaine
api
Type d'issue
Fonctionnalité
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.