diff mbox series

[FFmpeg-devel,v2,10/13] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop

Message ID 20220203184450.5491-11-scott.the.elm@gmail.com
State New
Headers show
Series rewrite avpriv_find_start_code() for clarity | expand

Commit Message

Scott Theisen Feb. 3, 2022, 6:44 p.m. UTC
This enhances the clarity of the code.
---
 libavcodec/cbs_mpeg2.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Comments

Andreas Rheinhardt Feb. 5, 2022, 1:48 a.m. UTC | #1
Scott Theisen:
> This enhances the clarity of the code.
> ---
>  libavcodec/cbs_mpeg2.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
> index d41477620e..afe78eef9a 100644
> --- a/libavcodec/cbs_mpeg2.c
> +++ b/libavcodec/cbs_mpeg2.c
> @@ -148,7 +148,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
>      CodedBitstreamUnitType unit_type;
>      uint32_t start_code = -1;
>      size_t unit_size;
> -    int err, i, final = 0;
> +    int err, i = 0, final = 0;
>  
>      start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
>                                     &start_code, 1);
> @@ -157,7 +157,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
>          return AVERROR_INVALIDDATA;
>      }
>  
> -    for (i = 0;; i++) {
> +    while (!final) {
>          unit_type = start_code & 0xff;
>  
>          if (start == frag->data + frag->data_size) {
> @@ -190,10 +190,8 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
>          if (err < 0)
>              return err;
>  
> -        if (final)
> -            break;
> -
>          start = end;
> +        i++;
>      }
>  
>      return 0;

I disagree that this enhances clarity: When using a counter, a for loop
is the most natural.

- Andreas
Scott Theisen Feb. 5, 2022, 3:47 a.m. UTC | #2
On 2/4/22 20:48, Andreas Rheinhardt wrote:
> I disagree that this enhances clarity: When using a counter, a for loop
> is the most natural.

The counter is not used as the loop condition, so it is not natural.  
Also, you removed the counter and made this a do while loop in your own 
patch series.
Andreas Rheinhardt Feb. 5, 2022, 4:06 a.m. UTC | #3
Scott Theisen:
> On 2/4/22 20:48, Andreas Rheinhardt wrote:
>> I disagree that this enhances clarity: When using a counter, a for loop
>> is the most natural.
> 
> The counter is not used as the loop condition, so it is not natural. 
> Also, you removed the counter and made this a do while loop in your own
> patch series.

Yes, after the counter has been removed, using a for-loop stopped being
natural.

- Andreas
Scott Theisen Feb. 5, 2022, 4:09 a.m. UTC | #4
On 2/4/22 23:06, Andreas Rheinhardt wrote:
> Scott Theisen:
>> On 2/4/22 20:48, Andreas Rheinhardt wrote:
>>> I disagree that this enhances clarity: When using a counter, a for loop
>>> is the most natural.
>> The counter is not used as the loop condition, so it is not natural.
>> Also, you removed the counter and made this a do while loop in your own
>> patch series.
> Yes, after the counter has been removed, using a for-loop stopped being
> natural.

It is not really a for-loop, though, since it has no loop condition and 
is thus an infinite loop that just happens to be abusing the for-loop 
syntax.

-Scott
diff mbox series

Patch

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index d41477620e..afe78eef9a 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -148,7 +148,7 @@  static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
     CodedBitstreamUnitType unit_type;
     uint32_t start_code = -1;
     size_t unit_size;
-    int err, i, final = 0;
+    int err, i = 0, final = 0;
 
     start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
                                    &start_code, 1);
@@ -157,7 +157,7 @@  static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
         return AVERROR_INVALIDDATA;
     }
 
-    for (i = 0;; i++) {
+    while (!final) {
         unit_type = start_code & 0xff;
 
         if (start == frag->data + frag->data_size) {
@@ -190,10 +190,8 @@  static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
         if (err < 0)
             return err;
 
-        if (final)
-            break;
-
         start = end;
+        i++;
     }
 
     return 0;