当前位置: 首页 > news >正文

乐山做网站装信通装修网

乐山做网站,装信通装修网,网站建设公司咨询,嵌入式软件开发面试题执行效果 python3 find_large_functions.py ./stage_test.cpp: StageTest::process has 106 lines. ./stage_test.cpp: StageTest::do_test_ has 172 lines.实现方法 在检测到函数开始的“可能”标志后#xff08;比如返回类型和作用域运算符::#xff09;#xff0c;开始…执行效果 python3 find_large_functions.py ./stage_test.cpp: StageTest::process has 106 lines. ./stage_test.cpp: StageTest::do_test_ has 172 lines.实现方法 在检测到函数开始的“可能”标志后比如返回类型和作用域运算符::开始累积接下来的行直到遇到函数体开始的左大括号{为止从而捕获完整的函数声明。 关键点 使用re.MULTILINE | re.DOTALL标志允许正则表达式跨越多行并正确匹配换行符。func_pattern试图匹配整个函数声明包括可能分布在多行的参数列表直到声明结束处的左大括号{。在找到一个匹配后代码将继续搜索匹配的右大括号}以确定函数体的实际长度并计算行数。 import os import redef find_large_functions_in_file(file_path, min_lines100):with open(file_path, r, encodingutf-8) as file:lines file.read()# 模式匹配类成员函数可能跨越多行func_pattern re.compile(r\w[\w\s:]*\w::\w\s*\([^)]*\)\s*[{],re.MULTILINE | re.DOTALL)functions []for match in func_pattern.finditer(lines):func_sig match.group()start_index match.start()# 计算左右大括号以平衡找出函数体的结束brace_count 1i match.end()while i len(lines) and brace_count ! 0:if lines[i] {:brace_count 1elif lines[i] }:brace_count - 1i 1func_body lines[start_index:i]line_count func_body.count(\n)if line_count min_lines:# 提取简化的函数名func_name_match re.search(r(\w)::(\w)\s*\(, func_sig)if func_name_match:class_name func_name_match.group(1)func_name func_name_match.group(2)display_name f{class_name}::{func_name}else:display_name Unknown Functionfunctions.append((display_name, line_count))return functionsdef find_large_functions(directory, line_threshold100):for root, dirs, files in os.walk(directory):for file_name in files:if file_name.endswith(.cpp):file_path os.path.join(root, file_name)large_functions find_large_functions_in_file(file_path, line_threshold)for func_name, line_count in large_functions:print(f{file_path}: {func_name} has {line_count} lines.)if __name__ __main__:find_large_functions(., 50)改进实现 读取整个文件内容考虑到需要使用正则表达式跨多行匹配函数声明我们维持了读取整个文件内容的方式但这在处理巨大的源文件时可能不够高效。正则表达式改进更好地匹配可能跨越多行的函数参数列表。异常处理: 当文件非常大或者编码问题导致无法打开时应该添加适当的异常处理来增强代码的健壮性这在示例中未展示。性能注意事项对于特别大的项目或文件完整地读取每个文件进内存可能导致性能瓶颈。对于超大文件可能需要采用更高效的逐行分析策略但这可能会复杂化正则表达式匹配跨越多行的情况。 import os import redef find_large_functions_in_file(file_path, min_lines100):# 编译正则表达式以匹配C类成员函数声明考虑跨多行func_pattern re.compile(r\w[\w\s:]*\w::\w\s*\((?:[^)]|\n)*\)\s*{,re.MULTILINE | re.DOTALL)functions [] # 存储符合条件的函数信息with open(file_path, r, encodingutf-8) as file:content file.read()for match in func_pattern.finditer(content):start_index match.end() # 函数体开始位置# 确定函数体结束的位置brace_level 1 # 已匹配一个左花括号for i in range(start_index, len(content)):if content[i] {:brace_level 1elif content[i] }:brace_level - 1if brace_level 0: # 所有括号均已匹配end_index i 1breakelse:# 如果找不到匹配的右括号则跳过此函数continue# 计算函数体行数line_count content[start_index:end_index].count(\n)if line_count min_lines:# 提取和格式化函数名func_name_match re.search(r(\w)::(\w)\s*\(, match.group(0))class_name, func_name func_name_match.groups() if func_name_match else (Unknown, Unknown)display_name f{class_name}::{func_name}functions.append((display_name, line_count))return functionsdef find_large_functions(directory, line_threshold100):for root, _, files in os.walk(directory):for file_name in files:if file_name.endswith(.cpp):file_path os.path.join(root, file_name)large_functions find_large_functions_in_file(file_path, line_threshold)for func_name, line_count in large_functions:print(f{file_path}: {func_name} has {line_count} lines.)if __name__ __main__:find_large_functions(., 100)
http://www.pierceye.com/news/380074/

相关文章:

  • 实训小结网站建设国内外最新新闻
  • 最新网站排名优化方法云龙徐州网站开发
  • 扬州做网站多少钱免费拿货的代理商
  • html做校园网站服装设计图片
  • 做三网站推广一般给多少钱
  • 网站关键词的写法牛肉煲的做法
  • 网站权限怎么设置吉林电商网站建设报价
  • wordpress修改站点名wordpress 插件 调用
  • vs2015做的网站广东省白云区属于哪个市
  • 微信群投票网站怎么做佳木斯做网站公司
  • 建设网站用哪个主机好阳西哪里有做网站
  • 沈阳市有做网站的公司中文企业网站html模板
  • 破解织梦做的网站有什么页游传奇平台好
  • 临安网站开发网站建设做什么费用
  • 辽宁建设工程信息网网站python 网站开发
  • 企业网站.net免费做ppt的网站
  • 浦城 做网站wordpress下载页面
  • 广西住房城乡建设部网站网站优化怎么看
  • 网站建设负责人证明网络营销的10个特点
  • 泉州市服务好的网站设计塘沽网吧开门了吗
  • 商城网站建设哪家公司好wordpress输出到模板
  • 建站报价网站建设培训学校
  • 杭州高端网站定制手机网站开发应注意
  • 深圳网站建设选云聚达做二手元器件那个网站查价格
  • 网站建设公司企业模板微网站开发制作
  • 北京网站制作计划合理的网站结构
  • 网站建设如何搭建框架兰州seo排名
  • 网站作为医院形象建设cms搭建网站
  • 如何做个购物网站网站开发好不好
  • wordpress国内打开速度慢东莞搜索seo关键词