Document.movePositionBytes

Returns the position at "end" starting from the given "src" position which is assumed to be at byte "start" Faster to quickly calculate nearby positions of known byte positions. Falls back to bytesToPosition if end is before start.

struct Document
const
movePositionBytes
(,
size_t start
,
size_t end
)

Examples

import std.regex;

auto intRegex = regex(`\bint\b`);

Document d;
d.setContent("int foo(int x, uint y)\n{\n    return cast(int)(x + y);\n}\n");

// either use size_t.max or 0, both work as starting points for different reasons:
// - 0 always matches Position.init, so the offset can be calculated
// - size_t.max is larger than the checked index match, so position is recomputed
size_t lastIndex = size_t.max;
Position lastPosition;

Position[] matches;

foreach (match; d.rawText.matchAll(intRegex))
{
	size_t index = match.pre.length;
	// to reduce boilerplate, use d.nextPositionBytes instead!
	auto pos = d.movePositionBytes(lastPosition, lastIndex, index);
	lastIndex = index;
	lastPosition = pos;
	matches ~= pos;
}

assert(matches == [
	Position(0, 0),
	Position(0, 8),
	Position(2, 16)
]);

Meta