Skip to content

Commit cf24a7d

Browse files
authored
optimize header detect (#117)
Signed-off-by: Loong <loong.dai@intel.com>
1 parent 308a6cb commit cf24a7d

File tree

5 files changed

+15
-36
lines changed

5 files changed

+15
-36
lines changed

pkg/constants/sequences.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/gci/gci.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,17 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
140140
}
141141

142142
var head []byte
143-
if src[headEnd-1] == '\t' || src[headEnd-1] == utils.Linebreak {
144-
head = src[:headEnd]
145-
} else {
146-
// handle multiple import blocks
147-
// cover `import ` to `import (`
148-
head = make([]byte, headEnd)
149-
copy(head, src[:headEnd])
150-
head = append(head, []byte{40, 10, 9}...)
151-
}
152-
143+
// use `import` as key to locate
144+
importEnd := bytes.LastIndex(src[:headEnd], []byte{'t'})
145+
head = make([]byte, importEnd+1)
146+
copy(head, src[:importEnd+1])
147+
// append `(\n\t`
148+
head = append(head, []byte{utils.LeftParenthesis, utils.Linebreak, utils.Indent}...)
149+
log.L().Debug(fmt.Sprintf("head: %s", head))
153150
tail := src[tailStart:]
154151
// for test
155152
if len(tail) == 0 {
156-
tail = []byte(")\n")
153+
tail = []byte{utils.RightParenthesis, utils.Linebreak}
157154
}
158155

159156
firstWithIndex := true
@@ -176,6 +173,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
176173
if tail[0] != utils.Linebreak {
177174
body = append(body, utils.Linebreak)
178175
}
176+
log.L().Debug(fmt.Sprintf("body: %s", body))
179177

180178
var totalLen int
181179
slices := [][]byte{head, body, tail}
@@ -187,7 +185,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
187185
for _, s := range slices {
188186
i += copy(dist[i:], s)
189187
}
190-
191188
dist, err = goFormat.Source(dist)
192189
if err != nil {
193190
return nil, nil, err

pkg/gci/internal/testdata/no-format.in.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package main
22

3-
import (
3+
import(
44
"fmt"
55

66
g "github.com/golang"

pkg/section/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func (s SectionParsingError) Is(err error) bool {
2525
return ok
2626
}
2727

28-
var MissingParameterClosingBracketsError = fmt.Errorf("section parameter is missing closing %q", utils.ParameterClosingBrackets)
28+
var MissingParameterClosingBracketsError = fmt.Errorf("section parameter is missing closing %q", utils.RightParenthesis)
2929

30-
var MoreThanOneOpeningQuotesError = fmt.Errorf("found more than one %q parameter start sequences", utils.ParameterClosingBrackets)
30+
var MoreThanOneOpeningQuotesError = fmt.Errorf("found more than one %q parameter start sequences", utils.RightParenthesis)
3131

3232
var SectionTypeDoesNotAcceptParametersError = errors.New("section type does not accept a parameter")
3333

pkg/utils/constants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const (
44
Indent = '\t'
55
Linebreak = '\n'
66

7-
SectionSeparator = ":"
7+
Colon = ":"
88

9-
ParameterOpeningBrackets = "("
10-
ParameterClosingBrackets = ")"
9+
LeftParenthesis = '('
10+
RightParenthesis = ')'
1111
)

0 commit comments

Comments
 (0)