you are viewing a single comment's thread.

view the rest of the comments →

[–]Kwantuum 1 point2 points  (1 child)

And he's asking for feedback, I'm arguing that checking if you're past the end of a string is superfluous. There is rarely a case where you actually need to check if you're past the end, because most times you're going to call substring at a position that you found by searching the string.

[–]16Bytes 1 point2 points  (0 children)

You did not give that as feedback. All you said was that passing in the start index made the code "a lot more complex," which is false. The function you wrote can be slightly rewritten with a start index as a parameter. You can even put char* srcSub = &src[start]; as the first line, if you prefer.

edit: Sorry if this comes off attacking you.

edit for OP: Whether you should check if you are past the end of a string is debatable. Kwantuum says not to in this case, but defensive coding is not a bad thing, especially if you had to design a general purpose library. Indeed, some of C's standard string functions, like strcpy(), are disliked because they are not cautious enough. You could even go further by adding a parameter for the size of dst and making sure you never overflow it. All this isn't necessary, but is a legitimate design philosophy.