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 upRegex.replace does not set the index accurately #483
Comments
jvoigtlaender
referenced this issue
Jan 17, 2016
Closed
Regex.split modifies the state of the regex #482
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
rgrempel
Jan 18, 2016
Contributor
Here's an illustration of the problem, for use in elm-repl. (I would have written a failing test, but had some trouble getting the tests to run at all). The <internal structure> references below are the undefined results from match.index.
import Regex exposing (..)
findComma = regex(",")
replace All findComma (\match -> toString match.index) "a,b,c"
-- "a<internal structure>b<internal structure>c" : String|
Here's an illustration of the problem, for use in elm-repl. (I would have written a failing test, but had some trouble getting the tests to run at all). The import Regex exposing (..)
findComma = regex(",")
replace All findComma (\match -> toString match.index) "a,b,c"
-- "a<internal structure>b<internal structure>c" : String |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 18, 2016
Contributor
Yeah, pretty bad. Also reproducible on http://elm-lang.org/try, using this program:
import Graphics.Element
import Regex exposing (..)
findComma = regex(",")
main = Graphics.Element.show <| replace All findComma (\match -> toString match.index) "a,b,c"Maybe you can revise your original post above to directly include that? Or better yet in a pull request replacing this ticket?
Generally in the line of this comment by Evan.
|
Yeah, pretty bad. Also reproducible on http://elm-lang.org/try, using this program: import Graphics.Element
import Regex exposing (..)
findComma = regex(",")
main = Graphics.Element.show <| replace All findComma (\match -> toString match.index) "a,b,c"Maybe you can revise your original post above to directly include that? Or better yet in a pull request replacing this ticket? Generally in the line of this comment by Evan. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
rgrempel
Jan 18, 2016
Contributor
To do a proper PR, I would want to write a failing test case that the PR fixes. However, I'm having trouble getting the tests to run at all -- I'll try harder at that when I have time.
|
To do a proper PR, I would want to write a failing test case that the PR fixes. However, I'm having trouble getting the tests to run at all -- I'll try harder at that when I have time. |
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.
|
Ah, thanks, I will give those a try. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Sep 22, 2016
Member
Consolidated regex stuff in #722. Follow along there!
Seems like a PR is possible.
|
Consolidated regex stuff in #722. Follow along there! Seems like a PR is possible. |
rgrempel commentedJan 17, 2016
In this line in
Regex.replace:https://github.com/elm-lang/core/blob/1b28ce5ce8f30ddf549e5f454a772f7b623e09b4/src/Native/Regex.js#L90
there are two problems.
First, the variable
iwill inevitably be equal to-1at this point. Thus, sayingi - 1is necessarily equivalent to-2, but obscure.Second, the
argumentsobject is not really an array, and does not support negative indexes. So, theindexproperty is always set to undefined.So, it would be better to replace this line with something like:
index: arguments[arguments.length - 2],