Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #111493

Re: Parsing based on 1st and 2nd spaces in a string

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Parsing based on 1st and 2nd spaces in a string
Date 2017-06-04 22:39 +0100
Organization A noiseless patient Spider
Message-ID <8737bfo2j8.fsf@bsb.me.uk> (permalink)
References <oh0ro2$12p$1@dont-email.me> <m2tw3vamfr.fsf@despina.home> <oh1582$vqs$2@dont-email.me>

Show all headers | View raw


DFS <nospam@dfs.com> writes:

> On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote:
>
>
>> You have a third case that you didn't specify:
>
> ?
>
> As the first line of my post said:
> "I have text input in only two formats:"
>
> There is no third case.

The format is not exactly clear.  If it is as you show then there is no
need to search for the space when seeing a digit -- you know exactly
where it it.  The fact that you do search for it suggests that you
expect some flexibility or unusual input.

Also, when looking for a second space, you parse nothing at all if a
space is not found into the area (withing 30 characters).  In that sense
you already parse three different formats.

Another point: are you absolutely sure that strncpy is the function you
need?  Unlike the other string functions it does append a null byte so
you can end up with unexpected result.  And if it is the right function
to use, the modern way is to call memcpy.  It's clearer in the the
reader has fewer things to think about -- it's a straight data copy.

> But thanks for showing isspace().  That might be handy.
>
>>      if(isdigit(line[0]) && isspace(line[1])){
>>         parse_digit_and_value(line);
>>      }else if(isdigit(line[0])){
>>         parse_error(line,"Invalid syntax");
>>      }else{
>>         parse_value(line);
>>      }

You really should consider making the code as clear as this.  There is
almost not cost to covering all the options and lot to be gained by not
causing your readers to wonder about the other case.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 07:48 -0400
  Re: Parsing based on 1st and 2nd spaces in a string "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-06-04 15:53 +0200
    Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 10:29 -0400
      Re: Parsing based on 1st and 2nd spaces in a string "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-06-04 21:52 +0200
        Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 16:43 -0400
      Re: Parsing based on 1st and 2nd spaces in a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-04 22:39 +0100
        Re: Parsing based on 1st and 2nd spaces in a string Keith Thompson <kst-u@mib.org> - 2017-06-04 19:40 -0700
        Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-05 00:08 -0400
          Re: Parsing based on 1st and 2nd spaces in a string raltbos@xs4all.nl (Richard Bos) - 2017-06-05 08:52 +0000
            Re: Parsing based on 1st and 2nd spaces in a string Robert Wessel <robertwessel2@yahoo.com> - 2017-06-05 23:51 -0500
          Re: Parsing based on 1st and 2nd spaces in a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-05 12:37 +0100
          Re: Parsing based on 1st and 2nd spaces in a string "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-05 12:53 -0400
  Re: Parsing based on 1st and 2nd spaces in a string Barry Schwarz <schwarzb@dqel.com> - 2017-06-04 11:24 -0700
    Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 16:43 -0400

csiph-web