Date: Mon, 6 Nov 2023 10:46:01 +0100
niedz., 5 lis 2023 o 23:55 Bjorn Reese via Std-Proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
> 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;
> }
> }
>
There is idiom like this with `erase`:
```
if (condition)
{
p = v.erase(p);
}
else
{
++p;
}
```
but do not know if this is good code practice as it is very easy to
make mistakes.
A lot better is make helper function that will handle all tricky iterations:
```
process_and_errase_if(v, [&](auto& a) { /*procesing*/ return condition; });
```
And this could be done initial example:
```
process<Serial>(buff, [&](auto val){
/*procesing*/
if (condition1) return Process::Continue;
else if (condition2) return Process::Repeat;
else return Process::Break;
});
```
all ugly and comlex code will be hidden in `process<Serial>` function.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
<std-proposals_at_[hidden]> napisaĆ(a):
>
> 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;
> }
> }
>
There is idiom like this with `erase`:
```
if (condition)
{
p = v.erase(p);
}
else
{
++p;
}
```
but do not know if this is good code practice as it is very easy to
make mistakes.
A lot better is make helper function that will handle all tricky iterations:
```
process_and_errase_if(v, [&](auto& a) { /*procesing*/ return condition; });
```
And this could be done initial example:
```
process<Serial>(buff, [&](auto val){
/*procesing*/
if (condition1) return Process::Continue;
else if (condition2) return Process::Repeat;
else return Process::Break;
});
```
all ugly and comlex code will be hidden in `process<Serial>` function.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2023-11-06 09:46:11