fix issue in gstr.RepliaceI; add command gen service for cli

This commit is contained in:
John Guo 2022-04-24 22:23:56 +08:00
parent 215a50675e
commit c256d2d4af
2 changed files with 14 additions and 14 deletions

View File

@ -3,6 +3,7 @@ package utils
import (
"fmt"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
"github.com/gogf/gf/v2/os/gproc"
)
@ -13,14 +14,17 @@ var (
// GoFmt formats the source file using command `gofmt -w -s PATH`.
func GoFmt(path string) {
if gofmtPath != "" {
gproc.ShellExec(fmt.Sprintf(`%s -w -s %s`, gofmtPath, path))
if gofmtPath == "" {
mlog.Debug(`command "gofmt" not found`)
return
}
gproc.ShellExec(fmt.Sprintf(`%s -w -s %s`, gofmtPath, path))
}
// GoImports formats the source file using command `goimports -w PATH`.
func GoImports(path string) {
if goimportsPath != "" {
gproc.ShellExec(fmt.Sprintf(`%s -w %s`, goimportsPath, path))
if goimportsPath == "" {
mlog.Debug(`command "goimports" not found`)
}
gproc.ShellExec(fmt.Sprintf(`%s -w %s`, goimportsPath, path))
}

View File

@ -33,21 +33,17 @@ func ReplaceI(origin, search, replace string, count ...int) string {
return origin
}
var (
searchLength = len(search)
searchLower = strings.ToLower(search)
originLower string
pos int
diff = len(replace) - searchLength
searchLength = len(search)
replaceLength = len(replace)
searchLower = strings.ToLower(search)
originLower string
pos int
)
for {
originLower = strings.ToLower(origin)
if pos = Pos(originLower, searchLower, pos); pos != -1 {
origin = origin[:pos] + replace + origin[pos+searchLength:]
if diff < 0 {
pos += -diff
} else {
pos += diff + 1
}
pos += replaceLength
if n--; n == 0 {
break
}