sassIndexToRuneIndex method
Converts sassIndex
into a Dart-style index into text.runes
.
Sass indexes are one-based, while Dart indexes are zero-based. Sass indexes may also be negative in order to index from the end of the string.
See also sassIndexToStringIndex, which an index into text directly.
Throws a SassScriptException
if sassIndex
isn't a number, if that
number isn't an integer, or if that integer isn't a valid index for this
string. If sassIndex
came from a function argument, name
is the
argument name (without the $
). It's used for error reporting.
Implementation
int sassIndexToRuneIndex(Value sassIndex, [String? name]) {
var index = sassIndex.assertNumber(name).assertInt(name);
if (index == 0) {
throw SassScriptException("String index may not be 0.", name);
} else if (index.abs() > sassLength) {
throw SassScriptException(
"Invalid index $sassIndex for a string with $sassLength characters.",
name);
}
return index < 0 ? sassLength + index : index - 1;
}