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

对mozjpeg中的函数名进行替换

获取到mozjpeg中的所有函数名

import os
import re

def process_files_in_directory(directory, pattern, output_file, valid_extensions):
    matches = []

    for root, _, files in os.walk(directory):
        for file_name in files:
            # 检查文件后缀
            if not any(file_name.endswith(ext) for ext in valid_extensions):
                continue

            file_path = os.path.join(root, file_name)
            try:
                # 读取文件内容
                with open(file_path, 'r', encoding='utf-8') as file:
                    content = file.read()
                
                # 查找匹配项,提取捕获组中的字符串
                file_matches = re.findall(pattern, content)
                matches.extend(file_matches)
                
                print(f"Processed {file_path}")
                    
            except Exception as e:
                print(f"Error processing {file_path}: {e}")

    # 将所有匹配到的字符串写入到文件中
    if matches:
        with open(output_file, 'w', encoding='utf-8') as out_file:
            for match in matches:
                out_file.write(match + '\n')
    else:
        print("No matches found")

# 示例调用
directory_to_search = r'Z:\\Variablereplace\\mozjpeg-master'
# 修改正则表达式,使用捕获组提取字符串
search_pattern = r"EXTERN\([^)]*\)\s+(\w+)"
output_file_path = 'output_file.txt'
# 指定需要处理的文件扩展名
valid_extensions = ['.c', '.h']

process_files_in_directory(directory_to_search, search_pattern, output_file_path, valid_extensions)

获取到函数列表到指定文件后,删去simd相关的内容。

进行第一次替换,替换所有的函数名称。
第二次替换是替换def目录导出的函数

 import os
import re

def replace_in_files(replace_dict, directory, extensions):
    """
    :param replace_dict: 进行替换的字典
    :param directory: 要搜索的目录
    :param extensions: 需要处理的文件后缀列表,例如 ['.txt', '.md']
    """
    for root, _, files in os.walk(directory):
        for file_name in files:
            if any(file_name.endswith(ext) for ext in extensions):
                file_path = os.path.join(root, file_name)
                try:
                    # 读取文件内容
                    with open(file_path, 'r', encoding='utf-8') as file:
                        content = file.read()
                    
                    # 进行替换
                    for old_string, new_string in replace_dict.items():
                        content = re.sub(r'\b' + re.escape(old_string) + r'\b', new_string, content)
                    
                    # 写回文件
                    with open(file_path, 'w', encoding='utf-8') as file:
                        file.write(content)
                    
                    print(f"Processed {file_path}")
                        
                except Exception as e:
                    print(f"Error processing {file_path}: {e}")

def get_replacement_dict(file_path):
    replace_dict = {}
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            for line in file:
                line = line.strip()
                if line:  # 确保非空行
                    replace_dict[line] = line + '_cjpeg'
    except Exception as e:
        print(f"Error reading replacement file {file_path}: {e}")
    return replace_dict

# 示例调用
replacement_file_path = 'output_file.txt'
# directory_to_search = r'Z:\\Variablereplace\\mozjpeg-master'
# file_extensions = ['.c', '.h']  # 需要处理的文件后缀
directory_to_search = r"Z:\\Variablereplace\\mozjpeg-master\\win"
file_extensions = ['.def']  # 需要处理的文件后缀

replace_dict = get_replacement_dict(replacement_file_path)
replace_in_files(replace_dict, directory_to_search, file_extensions) 


http://www.kler.cn/news/283623.html

相关文章:

  • 详解Spring AOP
  • 简单的Tcp服务器
  • 【香橙派系列教程】(十六) 语音模块与阿里云结合
  • Kafka日志及常见问题
  • x-cmd mod | x scoop - Windows 开源包管理工具
  • Java、python、php版 美发美甲预约服务平台 美容院管理系统(源码、调试、LW、开题、PPT)
  • 安卓15发布日期确定,安卓15 谷歌GMS认证截止日期有重大变化!安卓版本GMS认证截止时间更新,谷歌GMS认证之MADA/EDLA设备认证截止时间介绍
  • CSS 的文字平滑属性font-smooth
  • C++研发笔记1——github注册文档
  • C++类和对象(5)——运算符重载(以日期类为例)
  • 数据库,SQL和 MySql的三者关系
  • 智能听诊器:开启宠物健康管理新维度
  • 【网络安全】打开这份“开学礼” 谨防骗子“冲业绩”
  • 【Spring Boot 3】【Web】同时启用 HTTP 和 HTTPS
  • vue3+ts+vite项目代码检查报错(vue-tsc)
  • 解决Nginx负载均衡中的慢启动问题:策略与实践
  • k8s-pod 实战八 (gRPC 探测详细分析)
  • Cpp学习手册-基础学习
  • Python 处理 PDF 文件(PyPDF2, ReportLab)
  • 云轴科技ZStack与鼎甲科技共创数据保护新篇章
  • 显示中文字体问题解决:ImportError: The _imagingft C module is not installed
  • 最简洁!四步完成C#——opencv环境配置
  • 大模型企业应用落地系列八》基于大模型的对话式推荐系统》用户交互层
  • Python编码系列—Python CI/CD 实战:构建高效的自动化流程
  • MySQL集群基本概率
  • MySQL 延迟从库介绍
  • 基于OpenCV+MFC的KCF测速软件
  • Java面试题--1基础篇-01 __八股文 备战春招,秋招
  • 餐饮_零售_麻辣烫_水果店_零食店_生鲜店等收银系统
  • 代码随想录算法训练营day55:图论05:并查集