Re:The Most Expensive One-byte Mistake (2011)
by thedufer on Saturday April 12, 2014 @01:15AM
(#7577050)
That's a far less useful function, since you still have to do a memory copy if you want to pass the return value to a function that operates on strings.
[ Parent
| on HN ]
Re:The Most Expensive One-byte Mistake (2011)
by Karellen on Saturday April 12, 2014 @04:27AM
(#7577316)
Or you could just pass 'needle', which is already that string.
Re:The Most Expensive One-byte Mistake (2011)
by thedufer on Saturday April 12, 2014 @10:37AM
(#7577939)
Only if the first occurrence of needle happens to terminate the string. Otherwise the return value of the real version of strstr returns a string that starts with needle but is followed by more text.
Re:The Most Expensive One-byte Mistake (2011)
by Karellen on Saturday April 12, 2014 @04:06PM
(#7578996)
OK, so for that one particular use case[0], the result of strstr might not be quite as convenient as the NUL-terminated strstr() case. But that's what you get with a completely different string implementation - some operations are more efficient than others, others less so. It's a bunch of trade-offs.
But strstr() finds the position of a substring within a larger string, or indicates that it doesn't exist. My suggested API for the equivalent functionality does exactly that, with no problems or performance issues for the job strstr() itself needs to do that I can see.
[0] A use case I have to say I find a little contrived. I can say that the majority of the times I've used strstr(), it's either to simply check that needle exists in haystack, or to take the part of the haystack following needle - at which point I normally need to make a copy because I can't guarantee that the source string will be around for the lifetime that I need the remainder for.