Follow

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

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.

@namark just a guess but assuming the seperator is a multi-character value and not a single or two letter value then it will ignore the content in the middle of the seperator and misidentify seperators.

@freemo nah, std::search takes care of that, it works with ranges that you specify using begin and end iterators/pointers, a very c++ thing. The split itself works fine and handles all the edge cases, it just has undefined behaviour, which you can't identify in any way other than sheer unbridled pedantry.

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.