location.go 911 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package detect
  2. // Location represents a location in a file
  3. type Location struct {
  4. startLine int
  5. endLine int
  6. startColumn int
  7. endColumn int
  8. startLineIndex int
  9. endLineIndex int
  10. }
  11. func getLocation(linePairs [][]int, start int, end int) Location {
  12. var (
  13. prevNewLine int
  14. location Location
  15. )
  16. for lineNum, pair := range linePairs {
  17. newLineByteIndex := pair[0]
  18. if prevNewLine <= start && start < newLineByteIndex {
  19. location.startLine = lineNum
  20. location.endLine = lineNum
  21. location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
  22. location.startLineIndex = prevNewLine
  23. location.endLineIndex = newLineByteIndex
  24. }
  25. if prevNewLine < end && end <= newLineByteIndex {
  26. location.endLine = lineNum
  27. location.endColumn = (end - prevNewLine)
  28. location.endLineIndex = newLineByteIndex
  29. }
  30. prevNewLine = pair[0]
  31. }
  32. return location
  33. }