Skip to content

Commit

Permalink
Avoid unnecessary []byte -> string -> []byte conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
adotkhan committed May 10, 2023
1 parent 88e20b4 commit b4e6c8a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions psiphon/common/transforms/transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (spec Spec) ApplyString(seed *prng.Seed, input string) (string, error) {
if err != nil {
return "", errors.Trace(err)
}
value = re.ReplaceAllString(value, replacement)
value = re.ReplaceAllString(value, string(replacement))
}
return value, nil
}
Expand All @@ -173,15 +173,15 @@ func (spec Spec) Apply(seed *prng.Seed, input []byte) ([]byte, error) {
if err != nil {
return nil, errors.Trace(err)
}
value = re.ReplaceAll(value, []byte(replacement))
value = re.ReplaceAll(value, replacement)
}
return value, nil
}

// makeRegexAndRepl generates the regex and replacement for a given seed and
// transform. The same seed can be supplied to produce the same output, for
// replay.
func makeRegexAndRepl(seed *prng.Seed, transform [2]string) (*regexp.Regexp, string, error) {
func makeRegexAndRepl(seed *prng.Seed, transform [2]string) (*regexp.Regexp, []byte, error) {

// TODO: the compiled regexp and regen could be cached, but the seed is an
// issue with caching the regen.
Expand All @@ -192,18 +192,18 @@ func makeRegexAndRepl(seed *prng.Seed, transform [2]string) (*regexp.Regexp, str
}
rg, err := regen.NewGenerator(transform[1], args)
if err != nil {
return nil, "", errors.Trace(err)
return nil, nil, errors.Trace(err)
}

replacement, err := rg.Generate()
if err != nil {
return nil, "", errors.Trace(err)
return nil, nil, errors.Trace(err)
}

re, err := regexp.Compile(transform[0])
if err != nil {
return nil, "", errors.Trace(err)
return nil, nil, errors.Trace(err)
}

return re, string(replacement), nil
return re, replacement, nil
}

0 comments on commit b4e6c8a

Please sign in to comment.