Skip to content

Commit

Permalink
remove lineEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
camdencheek committed Apr 18, 2024
1 parent d8c99b1 commit a91d71f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
40 changes: 10 additions & 30 deletions contentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin
m := ms[0]
num := p.newlines().atOffset(m.byteOffset)
lineStart := int(p.newlines().lineStart(num))
lineEnd := int(p.newlines().lineEnd(num))
nextLineStart := int(p.newlines().lineStart(num + 1))

var lineCands []*candidateMatch

endMatch := m.byteOffset + m.byteMatchSz

for len(ms) > 0 {
m := ms[0]
if int(m.byteOffset) < lineEnd {
if int(m.byteOffset) < nextLineStart {
endMatch = m.byteOffset + m.byteMatchSz
lineCands = append(lineCands, m)
ms = ms[1:]
Expand All @@ -266,7 +266,7 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin
log.Panicf(
"%s %v infinite loop: num %d start,end %d,%d, offset %d",
p.id.fileName(p.idx), p.id.metaData,
num, lineStart, lineEnd,
num, lineStart, nextLineStart,
m.byteOffset)
}

Expand All @@ -275,22 +275,22 @@ func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLin
// Due to merging matches, we may have a match that
// crosses a line boundary. Prevent confusion by
// taking lines until we pass the last match
for lineEnd < len(data) && endMatch > uint32(lineEnd) {
next := bytes.IndexByte(data[lineEnd:], '\n')
for nextLineStart < len(data) && endMatch > uint32(nextLineStart) {
next := bytes.IndexByte(data[nextLineStart:], '\n')
if next == -1 {
lineEnd = len(data)
nextLineStart = len(data)
} else {
// TODO(hanwen): test that checks "+1" part here.
lineEnd += next + 1
nextLineStart += next + 1
}
}

finalMatch := LineMatch{
LineStart: lineStart,
LineEnd: lineEnd,
LineEnd: nextLineStart,
LineNumber: num,
}
finalMatch.Line = data[lineStart:lineEnd]
finalMatch.Line = data[lineStart:nextLineStart]

if numContextLines > 0 {
finalMatch.Before = p.newlines().getLines(data, num-numContextLines, num)
Expand Down Expand Up @@ -502,34 +502,14 @@ func (nls newlines) lineStart(lineNumber int) uint32 {
}
}

// lineEnd returns the (exclusive) byte offset pointing immediately after the
// last byte of the line. lineNumber is 1-based. If lineNumber is out
// of range of the lines in the file, the return value will be clamped to
// [0,fileSize].
func (nls newlines) lineEnd(lineNumber int) uint32 {
// nls.locs[0] + 1 is the start of the 2nd line of data.
endIdx := lineNumber - 1

if endIdx < 0 {
return 0
} else if endIdx >= len(nls.locs) {
return nls.fileSize
} else {
return nls.locs[endIdx] + 1
}
}

// getLines returns a slice of data containing the lines [low, high).
// low is 1-based and inclusive. high is 1-based and exclusive.
func (nls newlines) getLines(data []byte, low, high int) []byte {
if low >= high {
return nil
}

lowStart := nls.lineStart(low)
highEnd := nls.lineEnd(high - 1)

return data[lowStart:highEnd]
return data[nls.lineStart(low):nls.lineStart(high)]
}

const (
Expand Down
4 changes: 2 additions & 2 deletions contentprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestAtOffset(t *testing.T) {
if gotLineStart := nls.lineStart(gotLineNumber); gotLineStart != tt.lineStart {
t.Fatalf("expected line start %d, got %d", tt.lineStart, gotLineStart)
}
if gotLineEnd := nls.lineEnd(gotLineNumber); gotLineEnd != tt.lineEnd {
if gotLineEnd := nls.lineStart(gotLineNumber + 1); gotLineEnd != tt.lineEnd {
t.Fatalf("expected line end %d, got %d", tt.lineEnd, gotLineEnd)
}
})
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestLineBounds(t *testing.T) {
if gotStart != tt.start {
t.Fatalf("expected line start %d, got %d", tt.start, gotStart)
}
gotEnd := nls.lineEnd(tt.lineNumber)
gotEnd := nls.lineStart(tt.lineNumber + 1)
if gotEnd != tt.end {
t.Fatalf("expected line end %d, got %d", tt.end, gotEnd)
}
Expand Down
2 changes: 1 addition & 1 deletion matchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc
}
prev = line
byteStart := int(cp.newlines().lineStart(line))
byteEnd := int(cp.newlines().lineEnd(line))
byteEnd := int(cp.newlines().lineStart(line + 1))
lines = append(lines, lineRange{byteStart, byteEnd})
}

Expand Down

0 comments on commit a91d71f

Please sign in to comment.