class ChannelNameFormatter { static final Map _cache = {}; static const Set _qualityTokens = { 'SD', 'HD', 'FHD', 'UHD', '4K', '8K', 'HDR', 'HEVC', 'H264', 'H265', 'FULLHD', }; static String forDisplay(String rawName) { if (rawName.isEmpty) return rawName; final cached = _cache[rawName]; if (cached != null) return cached; var display = rawName.trim(); final pipeIndex = display.indexOf('|'); if (pipeIndex >= 0 && pipeIndex < display.length - 1) { display = display.substring(pipeIndex + 1).trim(); } display = display.replaceFirst(RegExp(r'^[\-\–\—:]+'), '').trim(); if (display.isNotEmpty) { final parts = display.split(RegExp(r'\s+')).toList(growable: true); while (parts.isNotEmpty && _isQualityToken(parts.last)) { parts.removeLast(); } display = parts.join(' ').trim(); } if (display.isEmpty) { display = rawName.trim(); } if (_cache.length > 50000) { _cache.clear(); } _cache[rawName] = display; return display; } static bool _isQualityToken(String token) { final normalized = token.toUpperCase().replaceAll(RegExp(r'[^A-Z0-9]'), ''); return _qualityTokens.contains(normalized); } }