Skip to content

Commit

Permalink
Use strings.ReplaceAll instead of strings.Replace(..., -1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jul 17, 2019
1 parent 9e42c3c commit e818e26
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions kobopatch/kobopatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
}

patchfile.Log = func(format string, a ...interface{}) {
k.Debugf(" | %s", strings.Replace(fmt.Sprintf(strings.TrimRight(format, "\n"), a...), "\n", "\n | ", -1))
k.Debugf(" | %s", strings.ReplaceAll(fmt.Sprintf(strings.TrimRight(format, "\n"), a...), "\n", "\n | "))
}

k.Logf("kobopatch %s\nhttps://github.com/geek1011/kobopatch\n", version)
Expand Down Expand Up @@ -793,7 +793,7 @@ func (k *KoboPatch) d(format string, a ...interface{}) {
}

func (k *KoboPatch) dp(prefix string, format string, a ...interface{}) {
k.d("%s%s", prefix, strings.Replace(fmt.Sprintf(format, a...), "\n", "\n"+prefix, -1))
k.d("%s%s", prefix, strings.ReplaceAll(fmt.Sprintf(format, a...), "\n", "\n"+prefix))
}

func wrap(err error, format string, a ...interface{}) error {
Expand All @@ -809,8 +809,8 @@ func jm(v interface{}) string {

func getFormat(filename string) string {
f := strings.TrimLeft(filepath.Ext(filename), ".")
f = strings.Replace(f, "patch", "patch32lsb", -1)
f = strings.Replace(f, "yaml", "kobopatch", -1)
f = strings.ReplaceAll(f, "patch", "patch32lsb")
f = strings.ReplaceAll(f, "yaml", "kobopatch")
return f
}

Expand Down
4 changes: 2 additions & 2 deletions patchfile/kobopatch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (b BaseAddress) ApplyTo(pt *patchlib.Patcher, log func(string, ...interface
func (b FindBaseAddressHex) ApplyTo(pt *patchlib.Patcher, log func(string, ...interface{})) error {
log("FindBaseAddressHex(%#v)", b)
var buf []byte
_, err := fmt.Sscanf(strings.Replace(string(b), " ", "", -1), "%x\n", &buf)
_, err := fmt.Sscanf(strings.ReplaceAll(string(b), " ", ""), "%x\n", &buf)
if err != nil {
return errors.Wrap(err, "FindBaseAddressHex: error parsing hex")
}
Expand Down Expand Up @@ -328,6 +328,6 @@ func expandHex(in *string, out *[]byte) (bool, error) {
if in == nil {
return false, nil
}
_, err := fmt.Sscanf(strings.Replace(*in, " ", "", -1), "%x\n", out)
_, err := fmt.Sscanf(strings.ReplaceAll(*in, " ", ""), "%x\n", out)
return true, errors.Wrapf(err, "error expanding shorthand hex `%s`", *in)
}
8 changes: 4 additions & 4 deletions patchfile/patch32lsb/patch32lsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Parse(buf []byte) (patchfile.PatchSet, error) {
var inPatch bool
curPatch := patch{}
eqRegexp := regexp.MustCompile(" +?= +?")
for i, l := range strings.Split(strings.Replace(string(buf), "\r\n", "\n", -1), "\n") {
for i, l := range strings.Split(strings.ReplaceAll(string(buf), "\r\n", "\n"), "\n") {
l = strings.TrimSpace(l)
switch {
case strings.HasPrefix(l, "#"), l == "":
Expand Down Expand Up @@ -127,7 +127,7 @@ func Parse(buf []byte) (patchfile.PatchSet, error) {
return nil, errors.Errorf("line %d: unexpected patch_enable value '%s' (should be yes or no)", i+1, spl[1])
}
case "replace_bytes":
args := strings.Replace(spl[1], " ", "", -1)
args := strings.ReplaceAll(spl[1], " ", "")
var offset int32
var find, replace []byte
_, err := fmt.Sscanf(args, "%x,%x,%x", &offset, &find, &replace)
Expand All @@ -147,7 +147,7 @@ func Parse(buf []byte) (patchfile.PatchSet, error) {
}
curPatch = append(curPatch, instruction{BaseAddress: &addr})
case "replace_float":
args := strings.Replace(spl[1], " ", "", -1)
args := strings.ReplaceAll(spl[1], " ", "")
var offset int32
var find, replace float64
_, err := fmt.Sscanf(args, "%x,%f,%f", &offset, &find, &replace)
Expand All @@ -160,7 +160,7 @@ func Parse(buf []byte) (patchfile.PatchSet, error) {
Replace float64
}{Offset: offset, Find: find, Replace: replace}})
case "replace_int":
args := strings.Replace(spl[1], " ", "", -1)
args := strings.ReplaceAll(spl[1], " ", "")
var offset int32
var find, replace uint8
_, err := fmt.Sscanf(args, "%x,%d,%d", &offset, &find, &replace)
Expand Down
56 changes: 28 additions & 28 deletions patchlib/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,35 @@ func (p *Patcher) FindZlib(find string) error {
continue
}
// Handle minification from below
zi.CSS = strings.Replace(zi.CSS, "\n ", "\n", -1)
zi.CSS = strings.Replace(zi.CSS, "\n ", "\n", -1)
zi.CSS = strings.Replace(zi.CSS, "\n ", "\n", -1)
findm := strings.Replace(find, "\n ", "\n", -1)
findm = strings.Replace(find, "\n ", "\n", -1)
findm = strings.Replace(findm, "\n ", "\n", -1)
zi.CSS = strings.ReplaceAll(zi.CSS, "\n ", "\n")
zi.CSS = strings.ReplaceAll(zi.CSS, "\n ", "\n")
zi.CSS = strings.ReplaceAll(zi.CSS, "\n ", "\n")
findm := strings.ReplaceAll(find, "\n ", "\n")
findm = strings.ReplaceAll(find, "\n ", "\n")
findm = strings.ReplaceAll(findm, "\n ", "\n")
if strings.Contains(zi.CSS, findm) || strings.Contains(stripWhitespace(zi.CSS), stripWhitespace(findm)) {
if i != 0 {
return errors.New("FindZlib: substring to find is not unique")
}
i = zi.Offset
continue
}
zi.CSS = strings.Replace(zi.CSS, ": ", ":", -1)
zi.CSS = strings.Replace(zi.CSS, " {", "{", -1)
findm = strings.Replace(findm, ": ", ":", -1)
findm = strings.Replace(findm, " {", "{", -1)
zi.CSS = strings.ReplaceAll(zi.CSS, ": ", ":")
zi.CSS = strings.ReplaceAll(zi.CSS, " {", "{")
findm = strings.ReplaceAll(findm, ": ", ":")
findm = strings.ReplaceAll(findm, " {", "{")
if strings.Contains(zi.CSS, findm) || strings.Contains(stripWhitespace(zi.CSS), stripWhitespace(findm)) {
if i != 0 {
return errors.New("FindZlib: substring to find is not unique")
}
i = zi.Offset
continue
}
zi.CSS = strings.Replace(zi.CSS, "\n", "", -1)
zi.CSS = strings.Replace(zi.CSS, "{ ", "", -1)
zi.CSS = strings.Replace(zi.CSS, "; ", "", -1)
findm = strings.Replace(findm, "{ ", "{", -1)
findm = strings.Replace(findm, "; ", ";", -1)
zi.CSS = strings.ReplaceAll(zi.CSS, "\n", "")
zi.CSS = strings.ReplaceAll(zi.CSS, "{ ", "")
zi.CSS = strings.ReplaceAll(zi.CSS, "; ", "")
findm = strings.ReplaceAll(findm, "{ ", "{")
findm = strings.ReplaceAll(findm, "; ", ";")
if strings.Contains(zi.CSS, findm) || strings.Contains(stripWhitespace(zi.CSS), stripWhitespace(findm)) {
if i != 0 {
return errors.New("FindZlib: substring to find is not unique")
Expand Down Expand Up @@ -236,18 +236,18 @@ func (p *Patcher) ReplaceZlibGroup(offset int32, repl []Replacement) error {
for _, r := range repl {
find, replace := r.Find, r.Replace
if !bytes.Contains(dbuf, []byte(find)) {
find = strings.Replace(find, "\n ", "\n", -1)
find = strings.Replace(find, "\n ", "\n", -1)
find = strings.Replace(find, "\n ", "\n", -1)
find = strings.ReplaceAll(find, "\n ", "\n")
find = strings.ReplaceAll(find, "\n ", "\n")
find = strings.ReplaceAll(find, "\n ", "\n")
if !bytes.Contains(dbuf, []byte(find)) {
find = strings.Replace(find, ": ", ":", -1)
find = strings.Replace(find, " {", "{", -1)
find = strings.ReplaceAll(find, ": ", ":")
find = strings.ReplaceAll(find, " {", "{")
if !bytes.Contains(dbuf, []byte(find)) {
find = strings.Replace(find, "\n", "", -1)
find = strings.Replace(find, "; ", ";", -1)
find = strings.Replace(find, "{ ", "{", -1)
find = strings.ReplaceAll(find, "\n", "")
find = strings.ReplaceAll(find, "; ", ";")
find = strings.ReplaceAll(find, "{ ", "{")
if !bytes.Contains(dbuf, []byte(find)) {
return errors.Errorf("ReplaceZlib: find string not found in stream (%s)", strings.Replace(find, "\n", "\\n", -1))
return errors.Errorf("ReplaceZlib: find string not found in stream (%s)", strings.ReplaceAll(find, "\n", "\\n"))
}
}
}
Expand Down Expand Up @@ -471,9 +471,9 @@ func decompress(src []byte) ([]byte, error) {
}

func stripWhitespace(src string) string {
src = strings.Replace(src, " ", "", -1)
src = strings.Replace(src, "\t", "", -1)
src = strings.Replace(src, "\n", "", -1)
src = strings.Replace(src, "\r", "", -1)
src = strings.ReplaceAll(src, " ", "")
src = strings.ReplaceAll(src, "\t", "")
src = strings.ReplaceAll(src, "\n", "")
src = strings.ReplaceAll(src, "\r", "")
return src
}

0 comments on commit e818e26

Please sign in to comment.