mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
enable literal string for code search (#33590)
Close: #33588 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
@@ -35,7 +35,7 @@ func FilenameOfIndexerID(indexerID string) string {
|
||||
return indexerID[index+1:]
|
||||
}
|
||||
|
||||
// Given the contents of file, returns the boundaries of its first seven lines.
|
||||
// FilenameMatchIndexPos returns the boundaries of its first seven lines.
|
||||
func FilenameMatchIndexPos(content string) (int, int) {
|
||||
count := 1
|
||||
for i, c := range content {
|
||||
@@ -48,3 +48,11 @@ func FilenameMatchIndexPos(content string) (int, int) {
|
||||
}
|
||||
return 0, len(content)
|
||||
}
|
||||
|
||||
func ParseKeywordAsPhrase(keyword string) (string, bool) {
|
||||
if strings.HasPrefix(keyword, `"`) && strings.HasSuffix(keyword, `"`) && len(keyword) > 1 {
|
||||
// only remove the prefix and suffix quotes, no need to decode the content at the moment
|
||||
return keyword[1 : len(keyword)-1], true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
30
modules/indexer/code/internal/util_test.go
Normal file
30
modules/indexer/code/internal/util_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseKeywordAsPhrase(t *testing.T) {
|
||||
cases := []struct {
|
||||
keyword string
|
||||
phrase string
|
||||
isPhrase bool
|
||||
}{
|
||||
{``, "", false},
|
||||
{`a`, "", false},
|
||||
{`"`, "", false},
|
||||
{`"a`, "", false},
|
||||
{`"a"`, "a", true},
|
||||
{`""\"""`, `"\""`, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
phrase, isPhrase := ParseKeywordAsPhrase(c.keyword)
|
||||
assert.Equal(t, c.phrase, phrase, "keyword=%q", c.keyword)
|
||||
assert.Equal(t, c.isPhrase, isPhrase, "keyword=%q", c.keyword)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user