dartterm/lib/cp437.dart

312 lines
3.4 KiB
Dart
Raw Normal View History

2023-09-06 03:11:15 +00:00
typedef Cp437 = int;
bool initialized = false;
final List<String> _fromCp437 = [
"\u0000",
"\u0001",
"\u0002",
"\u0003",
"\u0004",
"\u0005",
"\u0006",
"\u0007",
"\b",
"\t",
"\n",
"\u000b",
"\f",
"\r",
"\u000e",
"\u000f",
"\u0010",
"\u0011",
"\u0012",
"\u0013",
"\u0014",
"\u0015",
"\u0016",
"\u0017",
"\u0018",
"\u0019",
"\u001a",
"\u001b",
"\u001c",
"\u001d",
"\u001e",
"\u001f",
" ",
"!",
"\"",
"#",
"\$",
"%",
"&",
"'",
"(",
")",
"*",
"+",
",",
"-",
".",
"/",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
":",
";",
"<",
"=",
">",
"?",
"@",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"[",
"\\",
"]",
"^",
"_",
"`",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"{",
"|",
"}",
"~",
"\u007f",
"\u00c7",
"\u00fc",
"\u00e9",
"\u00e2",
"\u00e4",
"\u00e0",
"\u00e5",
"\u00e7",
"\u00ea",
"\u00eb",
"\u00e8",
"\u00ef",
"\u00ee",
"\u00ec",
"\u00c4",
"\u00c5",
"\u00c9",
"\u00e6",
"\u00c6",
"\u00f4",
"\u00f6",
"\u00f2",
"\u00fb",
"\u00f9",
"\u00ff",
"\u00d6",
"\u00dc",
"\u00a2",
"\u00a3",
"\u00a5",
"\u20a7",
"\u0192",
"\u00e1",
"\u00ed",
"\u00f3",
"\u00fa",
"\u00f1",
"\u00d1",
"\u00aa",
"\u00ba",
"\u00bf",
"\u2310",
"\u00ac",
"\u00bd",
"\u00bc",
"\u00a1",
"\u00ab",
"\u00bb",
"\u2591",
"\u2592",
"\u2593",
"\u2502",
"\u2524",
"\u2561",
"\u2562",
"\u2556",
"\u2555",
"\u2563",
"\u2551",
"\u2557",
"\u255d",
"\u255c",
"\u255b",
"\u2510",
"\u2514",
"\u2534",
"\u252c",
"\u251c",
"\u2500",
"\u253c",
"\u255e",
"\u255f",
"\u255a",
"\u2554",
"\u2569",
"\u2566",
"\u2560",
"\u2550",
"\u256c",
"\u2567",
"\u2568",
"\u2564",
"\u2565",
"\u2559",
"\u2558",
"\u2552",
"\u2553",
"\u256b",
"\u256a",
"\u2518",
"\u250c",
"\u2588",
"\u2584",
"\u258c",
"\u2590",
"\u2580",
"\u03b1",
"\u00df",
"\u0393",
"\u03c0",
"\u03a3",
"\u03c3",
"\u00b5",
"\u03c4",
"\u03a6",
"\u0398",
"\u03a9",
"\u03b4",
"\u221e",
"\u03c6",
"\u03b5",
"\u2229",
"\u2261",
"\u00b1",
"\u2265",
"\u2264",
"\u2320",
"\u2321",
"\u00f7",
"\u2248",
"\u00b0",
"\u2219",
"\u00b7",
"\u221a",
"\u207f",
"\u00b2",
"\u25a0",
"\u00a0"
];
final Map<String, Cp437> _toCp437 = {};
2023-09-23 04:08:03 +00:00
final Map<int, Cp437> _toCp437I = {};
2023-09-06 03:11:15 +00:00
void _init() {
if (initialized) {
return;
}
for (final (i, c) in _fromCp437.indexed) {
_toCp437[c] = i;
2023-09-23 04:08:03 +00:00
_toCp437I[c.runes.first] = i;
2023-09-06 03:11:15 +00:00
}
initialized = true;
}
String fromCp437Char(Cp437 c) {
_init();
if (c < 0 || c > _fromCp437.length) {
return "?";
}
return _fromCp437[c];
}
Cp437 toCp437Char(String c) {
_init();
var cp = _toCp437[c];
if (cp == null) {
return toCp437Char("?");
}
return cp;
}
String fromCp437String(List<Cp437> s) {
2023-09-23 04:08:03 +00:00
_init();
2023-09-06 03:11:15 +00:00
var out = "";
for (final c in s) {
out += fromCp437Char(c);
}
return out;
}
List<Cp437> toCp437String(String s) {
2023-09-23 04:08:03 +00:00
_init();
2023-09-06 03:11:15 +00:00
List<Cp437> out = [];
for (final c in s.runes) {
2023-09-23 04:08:03 +00:00
out.add(_toCp437I[c] ?? toCp437Char("?"));
2023-09-06 03:11:15 +00:00
}
return out;
}