Skip to content
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

strings.NewReplacer position dependent bug for single substitutions #6660

Closed
gopherbot opened this issue Oct 24, 2013 · 1 comment
Closed

Comments

@gopherbot
Copy link

by orthopteroid:

The string replacer appears to have a problem with single replacements that would happen
at the start of a string:

s := "Hello World"
fmt.Println(strings.NewReplacer("Hello","").Replace(s))
fmt.Println(strings.NewReplacer("World","").Replace(s))

Produces:

Hello World
Hello 

Which is incorrect. I would expect:

 World
Hello 

Whereas, when I add another substitution for replacement:

s := "Hello World"
fmt.Println(strings.NewReplacer("Hello","","l","").Replace(s))
fmt.Println(strings.NewReplacer("World","","l","").Replace(s))

Producing a correct result:

 Word
Heo 

Alternatively, if I perform a single replacement which is not at the start of the string:

s := "Hello World"
fmt.Println(strings.NewReplacer("ello","").Replace(s))
fmt.Println(strings.NewReplacer("orld","").Replace(s))

Produces the correct result:

H World
Hello W

So, the problem appears to be with single substitution when the match is at the start of
the string. I have noticed that strings.NewReplacer tries to handle single substitutions
as a special case... perhaps that code has a problem?

--------------
http://play.golang.org/p/Hj1V9d8fAj
go version devel +f4d1cb8d9a91 Thu Sep 19 22:34:33 2013 +1000 linux/386
@robpike
Copy link
Contributor

robpike commented Oct 24, 2013

Comment 1:

Status changed to Duplicate.

Merged into issue #6659.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants