mirror of
https://github.com/jimeh/undent.git
synced 2026-02-19 03:56:38 +00:00
fix(perf): minor improvement when no indent is found
This commit is contained in:
10
undent.go
10
undent.go
@@ -22,7 +22,7 @@ func Bytes(s string) []byte {
|
||||
}
|
||||
|
||||
// find smallest indent relative to each line-feed
|
||||
min := 99999999999
|
||||
min := -1
|
||||
count := 0
|
||||
|
||||
lfs := make([]int, 0, strings.Count(s, "\n"))
|
||||
@@ -35,7 +35,7 @@ func Bytes(s string) []byte {
|
||||
if s[i] == lf {
|
||||
lfs = append(lfs, i)
|
||||
indent = 0
|
||||
} else if indent < min {
|
||||
} else if indent < min || min == -1 {
|
||||
switch s[i] {
|
||||
case spc, tab:
|
||||
indent++
|
||||
@@ -43,13 +43,17 @@ func Bytes(s string) []byte {
|
||||
if indent > 0 {
|
||||
count++
|
||||
}
|
||||
if indent < min {
|
||||
if indent < min || min == -1 {
|
||||
min = indent
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if min == -1 {
|
||||
return []byte(s)
|
||||
}
|
||||
|
||||
// extract each line without indentation
|
||||
out := make([]byte, 0, len(s)-(min*count))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user