New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Common: Fix a potential infinite loop in ReplaceAll #983
Conversation
Prior to this change, it was possible to cause an infinite loop by making the string to be replaced and the replacing string the same thing. e.g. std::string some_str = "test"; ReplaceAll(some_str, "test", "test"); This also changes the replacing in a way that doesn't require starting from the beginning of the string on each replacement iteration.
Tests please :) |
Does this successfully replace all occurrences? The |
assignment in conditionals are generally gross. maybe just strcmp()(stl equiv) before? |
This will change behavior for |
@delroth Sure, I'll add a basic test when I get home. @BhaaLseN Example: http://ideone.com/07X7CC @shuffle2 strcmp's C++ equivalent is std::string's == operator, so I don't see how that would work (and not be messier). @magcius Yeah, that's expected behavior. It should only be used to process the given string as is without modifying the already replaced portions of it during the function's execution. |
If its expected, it should be documented somewhere. Tho, I don't know how its used in the Dolphin codebase, but I usually have the use-case of normalizing multiple spaces into a single space when I call a method called "ReplaceAll". With the current implementation, I'd still end up with multiple spaces for something like If we don't want/need that here (or don't care about that case), its fine by me. |
@BhaaLseN I think I'm misunderstanding what you're saying or something. From my POV, ReplaceAll should usually mean "Replace, in the given string, all occurrences of [target] with [replacement]" not "Replace, in the given string and replacements in the string, ...". In regards to Even C# does this behavior in its replacing method for example: http://ideone.com/ZPRqlz |
Yea, I haven't used a |
Exactly, but the C# method is called Replace; not ReplaceAll (which might be odd, considering that Replace replaces all occurrences, but not those that have already been replaced - which is what my usual C# ReplaceAll methods do). But with that cleared up, fine by me as it is. |
Oops, I totally missed that line of the patch, nvm. lgtm. |
Common: Fix a potential infinite loop in ReplaceAll
Prior to this change, it was possible to cause an infinite loop by making the string to be replaced and the replacing string the same thing. Came across this while writing the string unit-tests.
e.g.
std::string some_str = "test";
ReplaceAll(some_str, "test", "test");
This also changes the replacing in a way that doesn't require starting from the beginning of the string on each replacement iteration.