Debian Codegraph @

podofo/staging/BuildEncodings.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 string inputPath = @"D:\Temp\Enc\symbol.txt";
11 string outputPath = @"D:\symbol.txt";
12
13 // Dictionary: 1-byte code unit (0-255) -> list of (unicode code point, comment)
14 var map = new Dictionary<int, List<(int codePoint, string comment)>>();
15
16 foreach (string raw in File.ReadLines(inputPath))
17 {
18 string line = raw.Trim();
19 if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
20 continue;
21
22 string[] parts = line.Split(';');
23 if (parts.Length < 3)
24 continue;
25
26 if (!int.TryParse(parts[0].Trim(), System.Globalization.NumberStyles.HexNumber, null, out int codePoint))
27 continue;
28
29 if (!int.TryParse(parts[1].Trim(), System.Globalization.NumberStyles.HexNumber, null, out int byteUnit))
30 continue;
31
32 string comment = FormatComment(parts[2].Trim());
33
34 if (!map.ContainsKey(byteUnit))
35 map[byteUnit] = new List<(int, string)>();
36
37 map[byteUnit].Add((codePoint, comment));
38 }
39
40 using var writer = new StreamWriter(outputPath);
41
42 for (int b = 0; b <= 255; b++)
43 {
44 if (!map.TryGetValue(b, out var entries) || entries.Count == 0)
45 {
46 writer.WriteLine("0x0000,");
47 continue;
48 }
49
50 for (int i = 0; i < entries.Count; i++)
51 {
52 var (codePoint, comment) = entries[i];
53 string entry = $"0x{codePoint:X4}, // {b:X2} {comment}";
54
55 if (i % 2 == 0)
56 writer.WriteLine(entry);
57 else
58 writer.WriteLine($"// {entry}");
59 }
60 }
61
62 Console.WriteLine($"Done. Output written to: {outputPath}");
63 }
64
65 static string FormatComment(string comment)
66 {
67 // Normalize tabs to spaces
68 comment = comment.Replace('\t', ' ');
69
70 // Find the second '#' to split on
71 int firstHash = comment.IndexOf('#');
72 if (firstHash < 0)
73 return comment;
74
75 int secondHash = comment.IndexOf('#', firstHash + 1);
76 if (secondHash < 0)
77 return comment;
78
79 string firstPart = comment[..secondHash].TrimEnd();
80 string secondPart = comment[secondHash..];
81
82 return firstPart.PadRight(40) + " " + secondPart;
83 }
84 }