git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/8] t0021/rot13-filter: refactor packet reading functions

From
Christian Couder <christian.couder@gmail.com>
Date
Nov 7, 2017, 06:34 UTC
Message-ID
<CAP8UFD2vk4jV7jEBx3Axd-dhfcsGSJVFFt+pumdT1j8GD_oM_w@mail.gmail.com>
In-Reply-To
<xmqqo9oehotn.fsf@gitster.mtv.corp.google.com>
On Tue, Nov 7, 2017 at 2:15 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> Christian Couder <christian.couder@gmail.com> writes:
>
>> +sub packet_required_key_val_read {
>> +     my ( $key ) = @_;
>> +     my ( $res, $buf ) = packet_txt_read();
>> +     unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
>> +             die "bad $key: '$buf'";
>> +     }
>> +     return ( $res, $buf );
>> +}
>
> The function calls itself "required", but it does not die when it
> sees an unexpected EOF or an empty line.
If $res is not -1, it will die if $buf is empty.
> Neither of these cases
> gives it a key (nor val), but it is not treated as an error.
>
> That is curious, isn't it?

Yeah it is a bit strange for the unexpected EOF case. I think I will remove "required" from the name and add a comment before the function.

Show 7 quoted lines
> By the way, is it just me who finds this "unless" even less
> unerstandable than the original in packet_txt_read() you modeled
> this one after?  The original depended on packet_bin_read() to die
> on an unexpected EOF, so its "unless" made some sense (we know we
> got some input, and it is an error for the input not to end with LF
> unless it is an empty string).  Negating the condition and making it
> "if" may make it easier to understand, perhaps.  I dunno.
I can remove the "unless" and make it easier to understand like this:
  if ( $res == -1 ) {
          return ( $res, $buf );
  }
  if ( $buf =~ s/^$key=// and $buf ne '' ) ) {
          return ( $res, $buf );
  }
  die "bad $key: '$buf'";
Or to keep it short just:
  if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
          return ( $res, $buf );
  }
  die "bad $key: '$buf'";
Previous: Junio C HamanoNext: Christian Couder
Message 6 of 14 in “Create Git/Packet.pm”
  1. 0/8 Create Git/Packet.pmChristian Couder, Nov 5, 2017
  2. 1/8 t0021/rot13-filter: fix list comparisonChristian Couder, Nov 5, 2017
  3. Junio C HamanoNov 7, 2017
  4. 2/8 t0021/rot13-filter: refactor packet reading functionsChristian Couder, Nov 5, 2017
  5. Junio C HamanoNov 7, 2017
  6. Christian CouderNov 7, 2017
  7. 8/8 Add Git/Packet.pm from parts of t0021/rot13-filter.plChristian Couder, Nov 5, 2017
  8. 5/8 t0021/rot13-filter: add packet_initialize()Christian Couder, Nov 5, 2017
  9. 3/8 t0021/rot13-filter: improve 'if .. elsif .. else' styleChristian Couder, Nov 5, 2017
  10. 7/8 t0021/rot13-filter: add capability functionsChristian Couder, Nov 5, 2017
  11. Junio C HamanoNov 7, 2017
  12. 6/8 t0021/rot13-filter: refactor checking final lfChristian Couder, Nov 5, 2017
  13. Junio C HamanoNov 7, 2017
  14. 4/8 t0021/rot13-filter: improve error messageChristian Couder, Nov 5, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.