Skip to content

Commit

Permalink
优化对于 strings.Index 的使用,多数场景可以使用 IndexByte 替代。
Browse files Browse the repository at this point in the history
  • Loading branch information
cmstar committed Sep 12, 2023
1 parent 899694b commit 99c2182
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion basic_api_method_caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func compareError(t *testing.T, expected, got error) {

// Check checkPrefixError.
if pErr, ok := expected.(checkPrefixError); ok {
if strings.Index(got.Error(), pErr.prefix) != 0 {
if !strings.HasPrefix(got.Error(), pErr.prefix) {
t.Errorf("expect error starts with '%s', got %s", pErr.prefix, got)
}
return
Expand Down
6 changes: 3 additions & 3 deletions basic_api_user_host_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func (r *basicApiUserHostResolver) FillUserHost(state *ApiState) {

// 去掉端口部分,仅保留 IP 。
if strings.Contains(ip, ".") { // Is IPv4?
if colonIdx := strings.Index(ip, ":"); colonIdx > 0 {
if colonIdx := strings.IndexByte(ip, ':'); colonIdx > 0 {
ip = ip[:colonIdx]
}
} else { // IPv6
start := strings.Index(ip, "[")
start := strings.IndexByte(ip, '[')
if start < 0 {
goto END
}

end := strings.LastIndex(ip, "]")
end := strings.LastIndexByte(ip, ']')
if end < start {
goto END
}
Expand Down
2 changes: 1 addition & 1 deletion querystring.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func ParseQueryString(queryString string) QueryString {
right := 0

for ; left < length; left = right + 1 {
right = strings.Index(queryString[left:], "&")
right = strings.IndexByte(queryString[left:], '&')
if right == -1 {
right = length
} else {
Expand Down
2 changes: 1 addition & 1 deletion slimapi/slimapi_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (d slimApiMethodStructArgDecoder) paramMap(state *webapi.ApiState) (map[str
// Content-Type 为 multipart/form-data 的,交给框架内置方法解析。
// 为 application/x-www-form-urlencoded 或其他的 Content-Type 的,则单独读取,
// 此时的值类似 URL 上的 query-string ,需要使用同样的规则处理。
if strings.Index(contentType, webapi.ContentTypeMultipartForm) == 0 {
if strings.HasPrefix(contentType, webapi.ContentTypeMultipartForm) {
return d.readMultiPartForm(state)
}

Expand Down
2 changes: 1 addition & 1 deletion slimapi/slimapi_name_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (d *slimApiNameResolver) FillMethod(state *webapi.ApiState) {
contentType := req.Header.Get(webapi.HttpHeaderContentType)

// Content-Type 可以被分号分隔,如 “text/html; charset=UTF-8”,我们只需要前面这段。
if idx := strings.Index(contentType, ";"); idx > 0 {
if idx := strings.IndexByte(contentType, ';'); idx > 0 {
contentType = contentType[:idx]
contentType = strings.TrimSpace(contentType)
}
Expand Down
2 changes: 1 addition & 1 deletion slimauth/slimauth_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ParseAuthorizationHeader(r *http.Request, authScheme string) (Authorization

// Read <Scheme> part.
header := headers[0]
idx := strings.Index(header, " ")
idx := strings.IndexByte(header, ' ')
if idx <= 0 {
return auth, fmt.Errorf("Authorization scheme error")
}
Expand Down

0 comments on commit 99c2182

Please sign in to comment.