peek method
Get a substring from the document. If count is negative, this function returns count.abs() characters
leading up to the current position. If count is positive, this function returns count characters after
the current position. If count is 0, this function returns an empty string. The calculated start and end
positions of peek are bounds-checked (meaning they bottom out at 0 and top out at length.)
Implementation
String peek([int count = 1]) {
int start, end;
switch (count) {
// easy cases that don't require substring
case 1:
// return next character
return document[clampPos(position)];
case -1:
// return previous character
return document[clampPos(position - 1)];
// everything else
default:
// calculate start/stop pos
start = clampPos(position + (count < 0 ? count : 0));
end = clampPos(position + (count > 0 ? count : 0));
// return substring
return document.substring(start, end);
}
}