Skip to content

Commit

Permalink
refactor indexPos
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Sep 12, 2020
1 parent 3591a93 commit a58e9ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
26 changes: 6 additions & 20 deletions modules/indexer/code/elastic_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,15 @@ func (b *ElasticSearchIndexer) Delete(repoID int64) error {
// first end following the start string.
// If not found any of the positions, it will return -1, -1.
func indexPos(content, start, end string) (int, int) {
if len(content) < len(start)+len(end) {
startIdx := strings.Index(content, start)
if startIdx == -1 {
return -1, -1
}
startIdx, endIdx := -1, -1
for i := 0; i < len(content); i++ {
if startIdx > -1 {
if i+len(end) > len(content) {
return -1, -1
}
if content[i:i+len(end)] == end {
endIdx = i
return startIdx, endIdx + len(end)
}
} else {
if i+len(start) > len(content) {
return -1, -1
}
if content[i:i+len(start)] == start {
startIdx = i
}
}
endIdx := strings.Index(content[startIdx:], end)
if endIdx == -1 {
return -1, -1
}
return -1, -1
return startIdx, startIdx + endIdx + len(end)
}

func convertResult(searchResult *elastic.SearchResult, kw string, pageSize int) (int64, []*SearchResult, []*SearchResultLanguages, error) {
Expand Down
6 changes: 6 additions & 0 deletions modules/indexer/code/elastic_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ func TestESIndexAndSearch(t *testing.T) {

testIndexer("elastic_search", t, indexer)
}

func TestIndexPos(t *testing.T) {
startIdx, endIdx := indexPos("test index start and end", "start", "end")
assert.EqualValues(t, 11, startIdx)
assert.EqualValues(t, 24, endIdx)
}

0 comments on commit a58e9ae

Please sign in to comment.