把#define宏转换成指定格式

之前在弄一个东西的,有一大堆的宏,需要把它转换成其它的形式。遇到这种大批量的东西,我特别没有耐心去一个一个的弄,于是写了一段代码。

估计大家平常比较难用得上,不过可以平常相似的情况用来参考。

复制代码
SortedDictionary<int, string> enumMap = new SortedDictionary<int, string>();
string fileName = "tmp.txt";
File.WriteAllText(fileName, tbSource.Text, Encoding.Default);
string[] items = File.ReadAllLines(fileName, Encoding.Default);
Regex reg = new Regex(@"#define\s+(\S+)\s+(\d+)");
foreach(string item in items)
{
    Match match = reg.Match(item);
    string value = match.Groups[1].Value;
    int key = Convert.ToInt32(match.Groups[2].Value);
    if(enumMap.ContainsKey(key))
    {
        Debug.WriteLine(item);
        Debug.Assert(false);
    }
    else
    {
        enumMap.Add(key, value);
    }
}
string result = "";
foreach(var item in enumMap)
{
    result += string.Format("{0} = {1},\n", item.Value, item.Key);
}
tbTarget.Text = result;
复制代码

 

posted @   wzwyc  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示