location.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. lineSet bool
  16. _lineNum int
  17. )
  18. for lineNum, pair := range linePairs {
  19. _lineNum = lineNum
  20. newLineByteIndex := pair[0]
  21. if prevNewLine <= start && start < newLineByteIndex {
  22. lineSet = true
  23. location.startLine = lineNum
  24. location.endLine = lineNum
  25. location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
  26. location.startLineIndex = prevNewLine
  27. location.endLineIndex = newLineByteIndex
  28. }
  29. if prevNewLine < end && end <= newLineByteIndex {
  30. location.endLine = lineNum
  31. location.endColumn = (end - prevNewLine)
  32. location.endLineIndex = newLineByteIndex
  33. }
  34. prevNewLine = pair[0]
  35. }
  36. if !lineSet {
  37. // if lines never get set then that means the secret is most likely
  38. // on the last line of the diff output and the diff output does not have
  39. // a newline
  40. location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1
  41. location.endColumn = (end - prevNewLine)
  42. location.startLine = _lineNum + 1
  43. location.endLine = _lineNum + 1
  44. }
  45. return location
  46. }