Files

64 lines
1.3 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class Cyr2Lat {
static Map<String, String> _charMap = {
'А': 'A',
'В': 'B',
'Е': 'E',
'Ё': 'E',
'З': '3',
'К': 'K',
'М': 'M',
'Н': 'H',
'О': 'O',
'Р': 'P',
'С': 'C',
'Т': 'T',
'Х': 'X',
'Ь': 'b',
'а': 'a',
'е': 'e',
'ё': 'e',
'о': 'o',
'р': 'p',
'с': 'c',
'у': 'y',
'х': 'x',
};
static final RegExp _prefixRegExp = RegExp(r'\@\[[\S\s]+\] ');
static void setCharMap(Map<String, String> charMap) {
_charMap = Map.from(charMap);
}
static String encode(String text) {
if (text.isEmpty) return text;
final buffer = StringBuffer();
final senderName = extractSenderName(text);
final msgText = removeSenderName(text);
for (final rune in msgText.runes) {
final char = String.fromCharCode(rune);
buffer.write(_charMap[char] ?? char);
}
return senderName + buffer.toString();
}
static String removeSenderName(String text) {
final match = _prefixRegExp.matchAsPrefix(text);
if (match != null) {
return text.substring(match.end);
}
return text;
}
static String extractSenderName(String text) {
final match = _prefixRegExp.matchAsPrefix(text);
if (match != null) {
return match.group(0) ?? '';
}
return '';
}
}