hcl: NewRangeScannerFragment function

This is a variant of NewRangeScanner that allows the caller to specify the
start position, which is appropriate when this utility is being used with
a sub-slice of a file, rather than the whole file.
This commit is contained in:
Martin Atkins 2019-04-12 15:16:41 -07:00
parent 8b450a7d58
commit d268d87f93
2 changed files with 14 additions and 7 deletions

View File

@ -31,6 +31,9 @@ type Pos struct {
Byte int
}
// InitialPos is a suitable position to use to mark the start of a file.
var InitialPos = Pos{Byte: 0, Line: 1, Column: 1}
// Range represents a span of characters between two positions in a source
// file.
//

View File

@ -29,8 +29,8 @@ type RangeScanner struct {
err error // error from last scan, if any
}
// Create a new RangeScanner for the given buffer, producing ranges for the
// given filename.
// NewRangeScanner creates a new RangeScanner for the given buffer, producing
// ranges for the given filename.
//
// Since ranges have grapheme-cluster granularity rather than byte granularity,
// the scanner will produce incorrect results if the given SplitFunc creates
@ -39,15 +39,19 @@ type RangeScanner struct {
// around individual UTF-8 sequences, which will split any multi-sequence
// grapheme clusters.
func NewRangeScanner(b []byte, filename string, cb bufio.SplitFunc) *RangeScanner {
return NewRangeScannerFragment(b, filename, InitialPos, cb)
}
// NewRangeScannerFragment is like NewRangeScanner but the ranges it produces
// will be offset by the given starting position, which is appropriate for
// sub-slices of a file, whereas NewRangeScanner assumes it is scanning an
// entire file.
func NewRangeScannerFragment(b []byte, filename string, start Pos, cb bufio.SplitFunc) *RangeScanner {
return &RangeScanner{
filename: filename,
b: b,
cb: cb,
pos: Pos{
Byte: 0,
Line: 1,
Column: 1,
},
pos: start,
}
}