old code new question: can you spot the undefined behaviour? omg! and how can I ever recover from this?

Show thread
Follow

also noticed that it loops infinitely if the needle is empty, but the only meaningful thing to do here is to assert it being not empty as a precondition. You could maybe split character by character in that case, however that would be inconsistent with the overall behaviour of this split, as it does not ignore consecutive separators (it's a feature not a bug) and there are infinity consecutive empty strings between any two characters, or at the beginning of any string. You could also detect it and return some sentinel value, but to me that's an optional accessory that should go outside the main algorithm, because it can.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.