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: Replacer doesn't seem to replace correctly #14470

Closed
sjpotter opened this issue Feb 22, 2016 · 2 comments
Closed

strings: Replacer doesn't seem to replace correctly #14470

sjpotter opened this issue Feb 22, 2016 · 2 comments

Comments

@sjpotter
Copy link

using go 1.5.1 and 1.6 on linux/x64

given this string

var cgroupname = "/machine.slice/machine-rkt\\x2dbad20ebf\\x2de8b8\\x2d48fe\\x2dabba\\x2de2e3f72123fb.scope/system.slice/alpine-sh.service"

trying to use this function to strip out part of it. (yes, can use regex, but that's a different point)

func parseName(name string) (string, error) {
        splits := strings.Split(name, "/")
        if len(splits) == 3 || len(splits) == 5 {
                if splits[1] == "machine.slice" {
                        replacer := strings.NewReplacer("machine-rkt-", "", ".scope", "", "\\x2d", "-")
                        pod := replacer.Replace(splits[2])
                        pod = strings.Replace(pod, "machine-rkt-", "", -1)
                        if len(splits) == 3 {
                                return pod, nil
                        }
                        if splits[3] == "system.slice" {
                                container := strings.Replace(splits[4], ".service", "", -1)
                                return pod + ":" + container, nil
                        }
                }
        }

        return "", fmt.Errorf("%s not handled by rkt handler", name)
}

the strings.Replace for pod shouldn't be necessary, I'd expect the replacer to work correctly, but it doesn't. Either it's buggy or i'm using it incorrectly, but I don't know what I'm doing wrong.

can duplicate with https://play.golang.org/p/p_akq9d3YW by playing with comenting out line 18

@mdempsky
Copy link
Member

I think this is a more minimal example of your issue report: http://play.golang.org/p/EaTEE0Z3vj

s := "ab"
r := strings.NewReplacer("aa", "c", "b", "a")
fmt.Println(r.Replace(s))

Basically you're expecting this to replace the "b" with "a", and then to backtrack and replace the "aa" with "c".

However, the documentation for strings.NewReplacer says "Replacements are performed in order, without overlapping matches." So I don't think your expectation is sound.

If I've misunderstood, feel free to clarify. Thanks!

@mdempsky mdempsky changed the title Strings Replacer doesn't seem to replace correctly strings: Replacer doesn't seem to replace correctly Feb 22, 2016
@sjpotter
Copy link
Author

I agree, in rereading my issue and looking at the variable, I now see the problem, the - after rkt isn't a dash, it's a \x2d. This goes to mangling that I'm trying to work around.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
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

3 participants