refactor: replace HasPrefix+TrimPrefix with CutPrefix (#22520)

Signed-off-by: dulanting <dulanting@outlook.jp>
This commit is contained in:
dulanting 2025-11-05 15:05:31 +08:00 committed by GitHub
parent 985d5c3f0c
commit 80daa2dfcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 8 additions and 8 deletions

View File

@ -31,8 +31,8 @@ func FromRequest(req *http.Request) string {
return ""
}
auth := req.Header.Get("Authorization")
if strings.HasPrefix(auth, HeaderPrefix) {
return strings.TrimPrefix(auth, HeaderPrefix)
if after, ok := strings.CutPrefix(auth, HeaderPrefix); ok {
return after
}
return ""
}

View File

@ -85,8 +85,8 @@ func ParseSorting(sort string) []*Sort {
for sorting := range strings.SplitSeq(sort, ",") {
key := sorting
desc := false
if strings.HasPrefix(sorting, "-") {
key = strings.TrimPrefix(sorting, "-")
if after, ok := strings.CutPrefix(sorting, "-"); ok {
key = after
desc = true
}
sorts = append(sorts, &Sort{

View File

@ -111,8 +111,8 @@ func listOrderBy(query *q.Query) string {
}
prefixes := []string{"hard.", "used."}
for _, prefix := range prefixes {
if strings.HasPrefix(sortByItem.Key, prefix) {
resource := strings.TrimPrefix(sortByItem.Key, prefix)
if after, ok := strings.CutPrefix(sortByItem.Key, prefix); ok {
resource := after
if types.IsValidResource(types.ResourceName(resource)) {
field := fmt.Sprintf("%s->>%s", strings.TrimSuffix(prefix, "."), orm.QuoteLiteral(resource))
orderBy = fmt.Sprintf("(%s) %s", castQuantity(field), order)

View File

@ -41,8 +41,8 @@ func ParseProjectName(r *http.Request) string {
}
for _, prefix := range prefixes {
if strings.HasPrefix(path, prefix) {
parts := strings.Split(strings.TrimPrefix(path, prefix), "/")
if after, ok := strings.CutPrefix(path, prefix); ok {
parts := strings.Split(after, "/")
if len(parts) > 0 {
projectName = parts[0]
break