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

Regex.replace does not set the index accurately #483

Closed
rgrempel opened this Issue Jan 17, 2016 · 6 comments

Comments

Projects
None yet
3 participants
@rgrempel
Contributor

rgrempel commented Jan 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 i will inevitably be equal to -1 at this point. Thus, saying i - 1 is necessarily equivalent to -2, but obscure.

Second, the arguments object is not really an array, and does not support negative indexes. So, the index property is always set to undefined.

So, it would be better to replace this line with something like:

index: arguments[arguments.length - 2],

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

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
Contributor

rgrempel commented Jan 18, 2016

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
@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

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.

Contributor

jvoigtlaender commented Jan 18, 2016

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.

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

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.

Contributor

rgrempel commented Jan 18, 2016

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.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 18, 2016

Contributor

Yeah, it's a pity that the master branch is not in shape to run tests at the moment. Maybe #446 or #447 would help you locally?

Contributor

jvoigtlaender commented Jan 18, 2016

Yeah, it's a pity that the master branch is not in shape to run tests at the moment. Maybe #446 or #447 would help you locally?

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

rgrempel Jan 18, 2016

Contributor

Ah, thanks, I will give those a try.

Contributor

rgrempel commented Jan 18, 2016

Ah, thanks, I will give those a try.

@evancz evancz referenced this issue Sep 22, 2016

Closed

Regex #722

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Sep 22, 2016

Member

Consolidated regex stuff in #722. Follow along there!

Seems like a PR is possible.

Member

evancz commented Sep 22, 2016

Consolidated regex stuff in #722. Follow along there!

Seems like a PR is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment