Date: Sun, 5 Nov 2023 23:54:59 +0100
On 11/5/23 21:38, Frederick Virchanza Gotham via Std-Proposals wrote:
> Today I was programming an Arduino microcontroller and I wrote the
> following loop:
>
> for ( ; Serial.available(); ++p )
> {
> if ( p >= &buf[sizeof(buf) - 1u] ) break;
> p[0u] = Serial.read();
> p[1u] = '\0';
> unsigned const len = std::strlen(buf);
> char *ending = nullptr;
> if ( nullptr != (ending = std::strstr(buf, "\r\n")) )
> {
> ending[0u] = '\0';
> ending[1u] = '\0'; // Overwrite the "\r\n" with "\0\0"
> if ( ending != buf ) this->ProcessInput();
> buf[0u] = '\0';
> p = buf - 1u; // subtract 1 because it will be
> incremented upon continuing
> }
> }
The usual solution is to move the increment into the loop
while (Serial.available()) {
// Do stuff
if (condition) {
// Do conditional stuff
p = buf;
} else {
++p;
}
}
> Today I was programming an Arduino microcontroller and I wrote the
> following loop:
>
> for ( ; Serial.available(); ++p )
> {
> if ( p >= &buf[sizeof(buf) - 1u] ) break;
> p[0u] = Serial.read();
> p[1u] = '\0';
> unsigned const len = std::strlen(buf);
> char *ending = nullptr;
> if ( nullptr != (ending = std::strstr(buf, "\r\n")) )
> {
> ending[0u] = '\0';
> ending[1u] = '\0'; // Overwrite the "\r\n" with "\0\0"
> if ( ending != buf ) this->ProcessInput();
> buf[0u] = '\0';
> p = buf - 1u; // subtract 1 because it will be
> incremented upon continuing
> }
> }
The usual solution is to move the increment into the loop
while (Serial.available()) {
// Do stuff
if (condition) {
// Do conditional stuff
p = buf;
} else {
++p;
}
}
Received on 2023-11-05 22:55:08