C++ Logo

std-proposals

Advanced search

Re: [std-proposals] !continue

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Thu, 9 Nov 2023 17:12:35 +0100
czw., 9 lis 2023 o 16:17 sasho648 via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> Sorry if my previous reply went through - either way you still need the continue in the while loop.
>
> On Thu, Nov 9, 2023 at 5:16 PM sasho648 <sasho648_at_[hidden]> wrote:
>>
>> Do:
>>
>> for ( ; ; ++p )
>> while(SerialSimGsm.available())
>> {
>> if ( p >= &buf[sizeof(buf) - 1u] ) break;
>> p[0u] = SerialSimGsm.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;
>> continue;
>> }
>> break;
>> }
>>
>> On Thu, Nov 9, 2023 at 5:10 PM sasho648 <sasho648_at_[hidden]> wrote:
>>>
>>> Yeah that's fair - didn't think this through - well just don't use for then.
>>>
>>> On Thu, Nov 9, 2023 at 2:24 PM Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>>
>>>> On Tue, Nov 7, 2023 at 12:14 PM sasho648 wrote:
>>>> >
>>>> > Anyone mentioned goto? - With goto you can do exactly what the OP is asking for:
>>>> >
>>>> > for ( ; SerialSimGsm.available(); ++p )
>>>> > rev_con:
>>>> > {
>>>> > if ( p >= &buf[sizeof(buf) - 1u] ) break;
>>>> > p[0u] = SerialSimGsm.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;
>>>> > goto rev_con; // Don't allow 'p' to be incremented
>>>> > }
>>>> > }
>>>>
>>>>
>>>>
>>>> Your strategy causes an assertion failure in my code:
>>>>
>>>> https://godbolt.org/z/v6Px1Tao4
>>>>
>>>> because your 'goto' is skipping the pre-iterative condition check.
>>>>
>>>> Perhaps there should be four ways of continuing a loop:
>>>>
>>>> continue; // Don't skip anything
>>>> continue(skip post); // Skip the post-iterative step
>>>> continue(skip pre); // Skip the pre-iterative condition check
>>>> continue(skip both); // Skip both the post-iterative
>>>> step and the pre-iterative condition check
>>>>
>>>> The four words 'skip, pre, post, both' would not be keywords in the
>>>> C++ language, but would be words with special meaning only when
>>>> encountered in parentheses following 'continue'.
>>>> --
>>>> Std-Proposals mailing list
>>>> Std-Proposals_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

This is broken code as `if ( p >= &buf[sizeof(buf) - 1u] ) break;`
will make an infinite loop.

Beside as like I write in previous email, whole problem in this part
of code is in
first place wrong place where `p` is incremented, it should
be done after `p[0u] = SerialSimGsm.read();` not in loop increment.
This fix lot of problems in this code and even suggests a refactor like
I showed in my previous email.

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-11-09 16:12:48