Re:Efficient string copying and concatenation in C
by saagarjha on Thursday September 12, 2019 @07:21PM
(#20957541)
Why can’t NULL serve as the invalid string in this case? It’s clearly not a valid string that an operation will return.
[ Parent
| on HN ]
Re:Efficient string copying and concatenation in C
by codehero on Thursday September 12, 2019 @08:06PM
(#20957788)
If you have studied Computer Science, you should know that the null string is quite a valid string.
Let's take strstr, which finds a matching substring needle in a haystack string.
-returns a NULL string if the needle is not in the haystack. -returns pointer to first matching substring.
Extend strstr with VALIDITY
Understood behaviour if both are valid.
Say the haystack is INVALID...as the return value is NULL or a strict substring of haystack, should return INVALID. A poison haystack should poison dependent strings.
Say the haystack is valid but the needle is INVALID...should return NULL. A valid string never contains an INVALID string as a subsequence.
Re:Efficient string copying and concatenation in C
by saagarjha on Friday September 13, 2019 @12:37AM
(#20959208)
Here's my behavior:
strstr(NULL, /* valid string */)
I can't find the needle in the haystack (actually, I can't find anything in the haystack. I can't find the haystack.) Thus I return NULL. strstr(/* valid string */, NULL)
I can't find the needle in the haystack (actually, I wouldn't be able to find it: I don't know what I'm looking for.) Return NULL.