stripStray function
Removes every leading and trailing occurrence of needle from
haystack, as haystack.replaceAll(RegExp('^($needle)+|($needle)+\$'), '')
does with needle escaped, but without a RegExp.
A haystack of nothing but needles gives '', and an empty needle
gives haystack unchanged.
Implementation
String stripStray(String haystack, String needle) {
var n = needle.length;
if (n == 0) return haystack;
var start = 0, end = haystack.length;
while (start + n <= end && haystack.startsWith(needle, start)) {
start += n;
}
while (end - n >= start && _endsWithAt(haystack, needle, end)) {
end -= n;
}
return start == 0 && end == haystack.length
? haystack
: haystack.substring(start, end);
}