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:
parent
8b450a7d58
commit
d268d87f93
@ -31,6 +31,9 @@ type Pos struct {
|
|||||||
Byte int
|
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
|
// Range represents a span of characters between two positions in a source
|
||||||
// file.
|
// file.
|
||||||
//
|
//
|
||||||
|
@ -29,8 +29,8 @@ type RangeScanner struct {
|
|||||||
err error // error from last scan, if any
|
err error // error from last scan, if any
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new RangeScanner for the given buffer, producing ranges for the
|
// NewRangeScanner creates a new RangeScanner for the given buffer, producing
|
||||||
// given filename.
|
// ranges for the given filename.
|
||||||
//
|
//
|
||||||
// Since ranges have grapheme-cluster granularity rather than byte granularity,
|
// Since ranges have grapheme-cluster granularity rather than byte granularity,
|
||||||
// the scanner will produce incorrect results if the given SplitFunc creates
|
// 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
|
// around individual UTF-8 sequences, which will split any multi-sequence
|
||||||
// grapheme clusters.
|
// grapheme clusters.
|
||||||
func NewRangeScanner(b []byte, filename string, cb bufio.SplitFunc) *RangeScanner {
|
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{
|
return &RangeScanner{
|
||||||
filename: filename,
|
filename: filename,
|
||||||
b: b,
|
b: b,
|
||||||
cb: cb,
|
cb: cb,
|
||||||
pos: Pos{
|
pos: start,
|
||||||
Byte: 0,
|
|
||||||
Line: 1,
|
|
||||||
Column: 1,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user