I was playing with algorithms using Dart and as I actually followed TDD, I realized that my code has some limitations.
I was trying to reverse strings as part of an interview problem, but I couldn't get the surrogate pairs correctly reversed.
const simple = 'abc';
const emoji = '๐๐๐';
const surrogate = '๐ฎ๐ฝโโ๏ธ๐ฉ๐ฟโ๐ป';
String rev(String s) {
return String.fromCharCodes(s.runes.toList().reversed);
}
void main() {
print(simple);
print(rev(simple));
print(emoji);
print(rev(emoji));
print(surrogate);
print(rev(surrogate));
}
The output:
abc
cba
๐๐๐
๐๐๐
๐ฎ๐ฝโโ๏ธ๐ฉ๐ฟโ๐ป
๐ปโ๐ฟ๐ฉ๏ธโโ๐ฝ๐ฎ
You can see that the simple emojis are correctly reversed as I'm using the runes
instead of just simply executing s.split('').toList().reversed.join('');
but the surrogate pairs are reversed incorrectly.
How can I reverse a string that might contain surrogate pairs using the Dart programming language?
When reversing strings, you must operate on graphemes, not characters nor code units. Use grapheme_splitter.