Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upAvoiding infinite loop when the search string is empty #646
Conversation
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
You should add a test to this PR. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
@eeue56 Done |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Jul 18, 2016
Member
Thanks, this change makes sense to me! I'm trying to sort out the contributors agreement stuff at the moment. Can you either ping the email you sent previously or fill this out and send it to me? It's looking like we'll be able to do something nicer soon, so I apologize for the inconvenience.
|
Thanks, this change makes sense to me! I'm trying to sort out the contributors agreement stuff at the moment. Can you either ping the email you sent previously or fill this out and send it to me? It's looking like we'll be able to do something nicer soon, so I apologize for the inconvenience. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
@evancz Sent to your email address |
evancz
reviewed
Jul 18, 2016
src/Native/String.js
| @@ -192,7 +192,7 @@ function indexes(sub, str) | ||
| var subLen = sub.length; | ||
| var i = 0; | ||
| var is = []; | ||
| while ((i = str.indexOf(sub, i)) > -1) | ||
| while (subLen > 0 && (i = str.indexOf(sub, i)) > -1) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Jul 18, 2016
Member
Wouldn't it be faster to have the check for subLen > 0 outside the while loop? Seems like it only needs to happen once in an if, not on every loop.
evancz
Jul 18, 2016
Member
Wouldn't it be faster to have the check for subLen > 0 outside the while loop? Seems like it only needs to happen once in an if, not on every loop.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
@evancz Implemented your suggestion |
evancz
reviewed
Jul 18, 2016
src/Native/String.js
| } | ||
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Jul 18, 2016
Member
Better, but it can be optimized a bit more :) If you move the if above allocating i and is you can just write something like this:
if (subLen > 0)
{
return _elm_lang$core$Native_List.Nil;
}Which is cool in two ways. First, it means i and is do not even get allocated. Second, and more importantly, it uses the early return style, which I try to use in any C-like language. As a reader, it means I can be sure, nothing past this point is relevant to this code / some invariant is definitely true past this point.
evancz
Jul 18, 2016
•
Member
Better, but it can be optimized a bit more :) If you move the if above allocating i and is you can just write something like this:
if (subLen > 0)
{
return _elm_lang$core$Native_List.Nil;
}Which is cool in two ways. First, it means i and is do not even get allocated. Second, and more importantly, it uses the early return style, which I try to use in any C-like language. As a reader, it means I can be sure, nothing past this point is relevant to this code / some invariant is definitely true past this point.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
@evancz done |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Great, thank you! |
lorenzo commentedJun 13, 2016
The
String.indexesfunction enters an infinite loop when doingString.indexes "" "Some string", this is because theivariable is no advanced on each step of the loop since it will always return 0.I chose to return an empty list for this cases, as I think it is the most sensible thing to do IMO, but another valid interpretation is that the empty string is contained in all of the indexes of the string.
In case the second interpretation is preferred, I can gladly change it :)