24 lines
768 B
Dart
24 lines
768 B
Dart
/// Maps real ticker symbols to opaque tokens shown in question text.
|
|
class SymbolObfuscator {
|
|
SymbolObfuscator({this.prefix = 'ASSET_'});
|
|
|
|
final String prefix;
|
|
|
|
/// Stable token for [symbol] among [universe] sorted lexicographically.
|
|
String tokenFor(String symbol, List<String> universe) {
|
|
final List<String> sorted = List<String>.from(universe)..sort();
|
|
final int index = sorted.indexOf(symbol);
|
|
if (index < 0) {
|
|
throw ArgumentError.value(symbol, 'symbol', 'not in universe');
|
|
}
|
|
return '$prefix${_letterForIndex(index)}';
|
|
}
|
|
|
|
static String _letterForIndex(int index) {
|
|
if (index < 26) {
|
|
return String.fromCharCode(65 + index);
|
|
}
|
|
return '${_letterForIndex(index ~/ 26 - 1)}${_letterForIndex(index % 26)}';
|
|
}
|
|
}
|