[Info-vax] ChatGPT solved it for me, again...

Dan Cross cross at spitfire.i.gajendra.net
Mon Feb 20 09:20:20 EST 2023


In article <k5hc43Ftd0kU6 at mid.individual.net>,
bill  <bill.gunshannon at gmail.com> wrote:
>On 2/20/2023 5:09 AM, Johnny Billquist wrote:
>> On 2023-02-17 15:16, Simon Clubley wrote:
>>> On 2023-02-17, Arne Vajhøj <arne at vajhoej.dk> wrote:
>>>>
>>>> strcspn will stop when it reach the terminating null byte.
>>>>
>>>
>>> Are you sure about that ?
>>>
>>> https://linux.die.net/man/3/strcspn
>>>
>>> There's no mention of stopping at a null and no mention of what the
>>> return value is when the byte is not found within the string if it
>>> were to stop at a terminating null.
>> 
>> Quoting the man-page I have locally:
>> 
>>   The strcspn() function spans the initial part of the null-terminated
>>   string s as long as the characters from s do not occur in the null-
>>   terminated string charset (it spans the complement of charset). In 
>>   other words, it computes the string array index of the first character
>>   of s which is also in charset, else the index of the first null character.
>
>Is yours from Linux?  Seems the Linux and BSD man pages are quite
>different.  Have to wonder if this is a documentation problem or
>if Linux has a broken implementation of strcspn().

Broken in what way?  This seems entirely consistent with the
definition in the BSD man page.  The OpenBSD 7.2 man page even
gives an example surprisingly close to what was posted earlier:

--->BEGIN<---
EXAMPLES
     [snip...]

     The following removes the first (if any) newline character from string
     line.  This is useful for trimming the newline after a fgets(3) call.

           char line[BUFSIZ];

           if (fgets(line, sizeof(line), fp) != NULL)
                   line[strcspn(line, "\n")] = '\0';
--->END<---

>From the C11 standard, section 7.24.5.3:

   "size_t strcspn(const char *s1, const char *s2);

    The strcspn function computes the length of the maximum
    initial segment of the string pointed to by s1 which
    consists entirely of characters not from the string
    pointed to by s2.

   Returns
    The strcspn function returns the length of the segment."

Note the use of the term "string", which is defined as used here
in section 7.1.1:

   "A string is a contiguous sequence of characters terminated
    by and including the first null character."

The idea that `strcspn` would return the index of the
terminating NUL character for its first argument, should the
string it points to not contain any non-NUL characters from
the second argument, follows from these two definitions.
Since both strings are considered to contain their terminating
NUL characters, then that would necessarily match if nothing
else did.

	- Dan C.




More information about the Info-vax mailing list