宏:##-合并操作符

原文出处:http://msdn.microsoft.com/en-us/library/09dwwt6y(VS.80).aspx

试着翻译了msdn上的一段说明文档,有好些地方译的自己也不满意,欢迎大家批评指正。

 

The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.

##,双数符号又称标志超出操作符(token-pasting" operator ),有时也叫做合并操作符,在定义作用类似对象和函数的宏时需要用到。它可以做到让分开的标志合并为单独的标志,因此,它不能是宏定义中的第一个或最后一个标志。

 

 

If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.

在宏定义中如果一个正式的参数的前面或后面有合并操作符,这个正式的参数立即会被当前还没展开的变量替代。在这个替换之前,不会进行宏展开。

 

 

Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.

然后,合并操作符在标志符串的每次出现都被去掉了,前后的标志符因此被连接起来了。形成的标志必须是一个有效的标志。如果是有效的标志,这个标志将会被扫描判断,如果它表示一个宏名,它将会被替换。这个识别符表示一个名字:通过这个名字,连接成的标志在程序中被替换前将会被识别,每个标志表示一个在程序中或在编译命令行中已经定义过的标志。标志前后的空格是随意的。

 

 

This example illustrates use of both the stringizing and token-pasting operators in specifying program output:

下面这个例子展示了字符串化操作符和合并操作符在规定程序输出上的用处:

#define paster( n ) printf_s( "token" #n " = %d", token##n )

int token9 = 9;

paster( 9 );

 

the macro yields:这个宏将替换为:

printf_s( "token" "9" " = %d", token9 );

接着变为:

printf_s( "token9 = %d", token9 );

 

一个完整的例子:

// preprocessor_token_pasting.cpp

#include <stdio.h>

#define paster( n ) printf_s( "token" #n " = %d", token##n )

int token9 = 9;

 

int main()

{

   paster(9);

}

 

输出:token9 = 9
# -*- coding: utf-8 -*- import re import pandas as pd from pathlib import Path def extract_rte_functions(c_file_path: Path) -> list[str]: """ 从C文件中提取所有Rte_Write函数名 :param c_file_path: C文件路径对象 :return: 去重后的函数名列表 """ function_pattern = re.compile(r'Rte_Write_(\w+)\s*\(') # 正则优化:使用原始字符串和编译对象 unique_functions = set() # 使用集合自动去重 try: with c_file_path.open('r', encoding='utf-8') as file: # 显式指定UTF-8编码 for line_num, line in enumerate(file, 1): # 带行号枚举 if match := function_pattern.search(line): # 海象运算符优化 func_name = match.group(1) unique_functions.add(func_name) print(f"行{line_num}: 发现函数 {func_name}") # 调试信息 except FileNotFoundError: print(f"错误: 文件 {c_file_path} 不存在") return [] except UnicodeDecodeError: print(f"错误: 文件 {c_file_path} 编码问题") return [] return sorted(unique_functions) # 返回排序后的列表 def create_function_dataframe(functions: list[str]) -> pd.DataFrame: """ 创建包含函数名的DataFrame :param functions: 函数名列表 :return: 带索引的DataFrame """ return pd.DataFrame( functions, columns=['Rte_Write函数名'], index=pd.RangeIndex(1, len(functions)+1, name='序号') ) if __name__ == "__main__": # 配置路径(优化:使用Path对象) input_file = Path("D:\mycode\MID_E2E.c") output_file = Path("rte_functions.xlsx") # 提取函数名(优化至保存前最后一步) rte_functions = extract_rte_functions(input_file) if rte_functions: print(f"共发现{len(rte_functions)}个唯一函数") # 创建DataFrame(保存前的最后一步) function_df = create_function_dataframe(rte_functions) else: print("未找到有效的Rte_Write函数") 这是我的Python代码,现在我希望将读取到的DataFrame数据前面添加# define 可以做到吗
最新发布
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值