It's not that hard if you understand how to work with Unicode characters properly.
I won't say that this is the right way, but it works.
import 'package:sequence_processor/sequence_processor.dart';
import 'package:unicode/decomposers/canonical.dart';
import 'package:unicode/emoji/emoji.dart';
void main(List<String> args) {
var str = 'hello🇵🇬你们😀😀👨👩👦';
print('"$str"');
while (str.isNotEmpty) {
str = _removeChar(str, 1);
print('"$str"');
}
str = 'I 💗 you! ❤️🔥 ';
print('"$str"');
while (str.isNotEmpty) {
str = _removeChar(str, 1);
print('"$str"');
}
str = 'Amélie';
print('"$str"');
while (str.isNotEmpty) {
str = _removeChar(str, 1);
print('"$str"');
}
str = "Hello 😀 World";
print('"$str"');
while (str.isNotEmpty) {
str = _removeChar(str, 1);
print('"$str"');
}
}
// Should be stored in a static member for performance reasons.
final _processor = () {
final emojis = getUnicodeEmojiList();
final processor = SequenceProcessor<int, Object>();
for (final emoji in emojis) {
processor.addSequence(emoji.codePoints, emoji);
}
const decomposer = CanonicalDecomposer();
final mappingList = decomposer.getMappingList();
for (var i = 0; i < mappingList.length; i++) {
final mapping = mappingList[i];
final sequence = mapping.$2;
if (sequence.length > 1) {
if (!processor.hasSequence(sequence)) {
processor.addSequence(mapping.$2, mapping.$1);
}
}
}
return processor;
}();
String _removeChar(String text, int count) {
if (text.isEmpty) {
return '';
}
final result = _processor.process(text.runes.toList());
if (result.length < count) {
return '';
}
return result
.take(result.length - count)
.map((e) => e.data == null
? String.fromCharCode(e.element!)
: String.fromCharCodes(e.sequence!))
.join();
}
Output:
"hello🇵🇬你们😀😀👨👩👦"
"hello🇵🇬你们😀😀"
"hello🇵🇬你们😀"
"hello🇵🇬你们"
"hello🇵🇬你"
"hello🇵🇬"
"hello"
"hell"
"hel"
"he"
"h"
""
"I 💗 you! ❤️🔥 "
"I 💗 you! ❤️🔥"
"I 💗 you! "
"I 💗 you!"
"I 💗 you"
"I 💗 yo"
"I 💗 y"
"I 💗 "
"I 💗"
"I "
"I"
""
"Amélie"
"Améli"
"Amél"
"Amé"
"Am"
"A"
""
"Hello 😀 World"
"Hello 😀 Worl"
"Hello 😀 Wor"
"Hello 😀 Wo"
"Hello 😀 W"
"Hello 😀 "
"Hello 😀"
"Hello "
"Hello"
"Hell"
"Hel"
"He"
"H"
""
""