commonPrefix method
Returns the longest common prefix of all strings in this list.
Returns empty string if list is empty or any element is empty.
Example:
['flower', 'flow', 'flight'].commonPrefix(); // 'fl'
['a', 'b'].commonPrefix(); // ''
Implementation
@useResult
String commonPrefix() {
if (isEmpty) return '';
String prefix = this[0];
for (int i = 1; i < length; i++) {
final String s = this[i];
final int maxLen = prefix.length < s.length ? prefix.length : s.length;
int j = 0;
while (j < maxLen && prefix[j] == s[j]) {
j++;
}
prefix = prefix.replaceRange(j, prefix.length, '');
if (prefix.isEmpty) return '';
}
return prefix;
}