把#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;

 

转载于:https://www.cnblogs.com/wzwyc/p/6811096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值