mirror of
https://github.com/jimeh/undent.git
synced 2026-02-19 11:56:39 +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
|
// find smallest indent relative to each line-feed
|
||||||
min := 99999999999
|
min := -1
|
||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
lfs := make([]int, 0, strings.Count(s, "\n"))
|
lfs := make([]int, 0, strings.Count(s, "\n"))
|
||||||
@@ -35,7 +35,7 @@ func Bytes(s string) []byte {
|
|||||||
if s[i] == lf {
|
if s[i] == lf {
|
||||||
lfs = append(lfs, i)
|
lfs = append(lfs, i)
|
||||||
indent = 0
|
indent = 0
|
||||||
} else if indent < min {
|
} else if indent < min || min == -1 {
|
||||||
switch s[i] {
|
switch s[i] {
|
||||||
case spc, tab:
|
case spc, tab:
|
||||||
indent++
|
indent++
|
||||||
@@ -43,13 +43,17 @@ func Bytes(s string) []byte {
|
|||||||
if indent > 0 {
|
if indent > 0 {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
if indent < min {
|
if indent < min || min == -1 {
|
||||||
min = indent
|
min = indent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if min == -1 {
|
||||||
|
return []byte(s)
|
||||||
|
}
|
||||||
|
|
||||||
// extract each line without indentation
|
// extract each line without indentation
|
||||||
out := make([]byte, 0, len(s)-(min*count))
|
out := make([]byte, 0, len(s)-(min*count))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user