Skip to content

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

Description

@sjpotter

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions