github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/file/cataloger/secrets/newline_counter.go (about)

     1  package secrets
     2  
     3  import "io"
     4  
     5  type newlineCounter struct {
     6  	io.RuneReader
     7  	numBytes int64
     8  	newLines []int64
     9  }
    10  
    11  func (c *newlineCounter) ReadRune() (r rune, size int, err error) {
    12  	r, size, err = c.RuneReader.ReadRune()
    13  	c.numBytes += int64(size)
    14  	if r == '\n' {
    15  		c.newLines = append(c.newLines, c.numBytes)
    16  	}
    17  	return
    18  }
    19  
    20  func (c *newlineCounter) newlinesBefore(pos int64) int {
    21  	var result int
    22  	for _, nlPos := range c.newLines {
    23  		if nlPos <= pos {
    24  			result++
    25  		}
    26  	}
    27  	return result
    28  }
    29  
    30  func (c *newlineCounter) newlinePositionBefore(pos int64) int64 {
    31  	var last int64
    32  	for _, nlPos := range c.newLines {
    33  		if nlPos > pos {
    34  			break
    35  		}
    36  		last = nlPos
    37  	}
    38  	return last
    39  }