a shorter strchr

Discussion in 'C Programming' started by Servé Lau, Feb 17, 2004.

  1. Servé Lau

    Servé Lau Guest

    Sometimes I code like this:

    char *p = strchr(buf,'\n');
    if (p)
    *p = 0;

    But I've also seen the shorter expression:

    buf[strcspn(buf, '\n')] = 0;

    Are their advantages to the second one except shorter code?
     
    Servé Lau, Feb 17, 2004
    #1
    1. Advertising

  2. "Servé Lau" <> wrote in message
    news:...
    > Sometimes I code like this:
    >
    > char *p = strchr(buf,'\n');
    > if (p)
    > *p = 0;
    >
    > But I've also seen the shorter expression:
    >
    > buf[strcspn(buf, '\n')] = 0;
    >
    > Are their advantages to the second one except shorter code?


    I can't think of any. strcspn might be a bit slower than strchr, because it
    is designed to look for a set of characters in a string.

    What you have in the second case is slightly wrong, a typo perhaps. strcspn
    takes a constant character pointer as it's second argument.
    So it should be:
    buf[ strcspn(buf, "\n") ] = 0;
     
    Spacen Jasset, Feb 17, 2004
    #2
    1. Advertising

  3. Servé Lau

    David Rubin Guest

    Spacen Jasset wrote:

    >>char *p = strchr(buf,'\n');
    >>if (p)
    >> *p = 0;


    [or...]
    >>buf[strcspn(buf, '\n')] = 0;


    >>Are their advantages to the second one except shorter code?


    > I can't think of any. strcspn might be a bit slower than strchr, because it
    > is designed to look for a set of characters in a string.


    Indeed. Also, "shorter" is relative. What appears shorter to you may be
    many times longer to the CPU. Just try to strike a reasonable balance
    between terseness and readability. Another idiom is

    if((p=strchr(buf, '\n')))
    *p = 0;

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
     
    David Rubin, Feb 17, 2004
    #3
  4. Servé Lau

    nrk Guest

    Serv�Lau wrote:

    > Sometimes I code like this:
    >
    > char *p = strchr(buf,'\n');
    > if (p)
    > *p = 0;
    >
    > But I've also seen the shorter expression:
    >
    > buf[strcspn(buf, '\n')] = 0;
    >
    > Are their advantages to the second one except shorter code?


    The call to strcspn is incorrect. It should be:
    buf[strcspn(buf, "\n")] = 0;

    One (likely inconsequential) difference is that the second call will always
    write a null to the string. There might be some performance trade-offs as
    strcspn will be scanning the string for a *set* of characters that it
    shouldn't find, while strchr searches for exactly one character which is
    considerably more straightforward. Also, as a style issue, I would
    probably not go for embedding a function call like that into the index of
    an array in the assignment statement.

    -nrk.

    --
    Remove devnull for email
     
    nrk, Feb 17, 2004
    #4
  5. Servé Lau

    pete Guest

    Spacen Jasset wrote:
    >
    > "Servé Lau" <> wrote in message
    > news:...
    > > Sometimes I code like this:
    > >
    > > char *p = strchr(buf,'\n');
    > > if (p)
    > > *p = 0;
    > >
    > > But I've also seen the shorter expression:
    > >
    > > buf[strcspn(buf, '\n')] = 0;
    > >
    > > Are their advantages to the second one except shorter code?

    >
    > I can't think of any.
    > strcspn might be a bit slower than strchr, because it
    > is designed to look for a set of characters in a string.


    Conceptually, strcspn can be described as looping calls to strchr.

    size_t strcspn(const char *s1, const char *s2)
    {
    const char *const p1 = s1;

    while (strchr(s2, *s1) == NULL) {
    ++s1;
    }
    return s1 - p1;
    }

    > What you have in the second case is slightly wrong,
    > a typo perhaps. strcspn
    > takes a constant character pointer as it's second argument.
    > So it should be:
    > buf[ strcspn(buf, "\n") ] = 0;


    A briefer, more computationally intensive way of writing it would be:

    strtok(buf, "\n");

    --
    pete
     
    pete, Feb 18, 2004
    #5
  6. Servé Lau

    Dan Pop Guest

    In <> "Servé Lau" <> writes:

    >Sometimes I code like this:
    >
    >char *p = strchr(buf,'\n');
    >if (p)
    > *p = 0;
    >
    >But I've also seen the shorter expression:
    >
    >buf[strcspn(buf, '\n')] = 0;
    >
    >Are their advantages to the second one except shorter code?


    It's neither more readable nor likely to be faster at run time.

    If you're fond of oneliners, yours can be turned into one, too:

    (p = strchr(buf,'\n')) ? (*p = 0) : 0;
    or
    if ((p = strchr(buf,'\n')) != NULL) *p = 0;

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Feb 18, 2004
    #6
  7. Servé Lau

    David Rubin Guest

    pete wrote:

    >>>char *p = strchr(buf,'\n');
    >>>if (p)
    >>> *p = 0;


    [or]
    >>>buf[strcspn(buf, '\n')] = 0;


    [snip]
    > A briefer, more computationally intensive way of writing it would be:
    >
    > strtok(buf, "\n");


    However, this is not readable at all since the intent is unclear and the
    effect is merely a byproduct of the function's semantics.

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
     
    David Rubin, Feb 18, 2004
    #7
  8. Servé Lau

    Dan Pop Guest

    In <c0vv6a$> David Rubin <> writes:

    >pete wrote:
    >
    >> A briefer, more computationally intensive way of writing it would be:
    >>
    >> strtok(buf, "\n");

    >
    >However, this is not readable at all since the intent is unclear and the
    >effect is merely a byproduct of the function's semantics.


    I disagree: the return value being ignored, it is (or should be) OBVIOUS
    that this function was called *exclusively* for its side effect(s).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Feb 18, 2004
    #8
  9. "pete" <> wrote in message
    news:...

    ....

    > A briefer, more computationally intensive way of writing it would be:
    >
    > strtok(buf, "\n");
    >


    That does not work how the other methods do though. In particular, it would
    not remove '\n' at the begining of the string. From the NetBSD man pages:

    " The strtok() function returns a pointer to the beginning of each subse-
    quent token in the string, after replacing the separator character
    itself
    with a NUL character. Separator characters at the beginning of the
    string or at the continuation point are skipped so that zero length to-
    kens are not returned. When no more tokens remain, a null pointer is
    re-
    turned."
     
    Spacen Jasset, Feb 18, 2004
    #9
  10. Servé Lau

    pete Guest

    Spacen Jasset wrote:
    >
    > "pete" <> wrote in message
    > news:...
    >
    > ...
    >
    > > A briefer, more computationally intensive way of writing it would be:
    > >
    > > strtok(buf, "\n");
    > >

    >
    > That does not work how the other methods do though.
    > In particular, it would
    > not remove '\n' at the begining of the string.


    Good catch. Thank you.

    --
    pete
     
    pete, Feb 19, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    32
    Views:
    7,140
    Default User
    Aug 22, 2003
  2. Martin Dickopp

    Re: Question about strchr() / strrchr()

    Martin Dickopp, Aug 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    2,267
    Irrwahn Grausewitz
    Aug 16, 2003
  3. David Warner

    strchr & finding multiple occurances of a char

    David Warner, Apr 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,389
    CBFalconer
    Apr 26, 2005
  4. ts

    strchr declaration

    ts, Mar 14, 2007, in forum: C Programming
    Replies:
    2
    Views:
    413
    Florian Weingarten
    Mar 14, 2007
  5. earthling

    strchr

    earthling, Nov 15, 2008, in forum: C Programming
    Replies:
    2
    Views:
    451
    earthling
    Nov 16, 2008
Loading...

Share This Page