检查是否有tab, newline, space字符

int isSpace(int c){
    if(c == ' '){
        return 1;
    }

    return 0;
}

int isTab(int c){
    if(c == '\t'){
        return 1;
    }

    return 0;
}

int isNewline(int c){
    if(c == '\n'){
        return 1;
    }

    return 0;
}


这里开始记录自己一些函数啦,抽取出来以后自己可能会用到,也可以修修改改。

分析以下代码#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import argparse import re def normalize_indentation(file_path, tab_width=4): """高级缩进处理:精确保持注释块内部对齐一致性""" modified = False try: with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() except Exception as e: print(f"Error reading {file_path}: {e}") return False # 检查文件末尾是否有换行 ends_with_newline = content.endswith('\n') original_lines = content.splitlines() new_lines = [] # 解析状态 in_block_comment = False in_string = False string_char = None escape_next = False # 块注释数据结构 block_comment_data = { 'start_line': -1, # 注释块起始行号 'original_base_indent': '', # 原始基础缩进 'normalized_base_indent': '', # 规范化后的基础缩进 'modified': False, # 首行缩进是否被修改 'skip_all_modification': False, # 是否跳过整个注释块修改 'content_indents': {} # 存储每行的内容缩进(行号和缩进内容) } for line_num, line in enumerate(original_lines): # 保留原始行内容(包括行尾空格) raw_line = line.rstrip('\n') # 保留行尾空格,只移除换行符 # 获取前导空白 leading_whitespace = re.match(r'^\s*', raw_line).group() space_count = len(leading_whitespace.expandtabs(tab_width)) # 字符级分析 line_chars = list(raw_line) i = 0 n = len(line_chars) is_code_line = False is_line_comment = False is_block_comment_content = False found_block_comment_end = False # 重置转义状态 escape_next = False # 初始化状态 current_in_block_comment = in_block_comment # 处理块注释状态 if in_block_comment: is_block_comment_content = True # 检查块注释是否结束 if '*/' in raw_line: current_in_block_comment = False found_block_comment_end = True # 分析行内容 while i < n: char = line_chars[i] # 处理转义字符 if escape_next: escape_next = False i += 1 continue # 处理字符串 if in_string: if char == '\\': escape_next = True elif char == string_char: in_string = False string_char = None i += 1 continue # 处理块注释结束 if current_in_block_comment: if char == '*' and i + 1 < n and line_chars[i + 1] == '/': current_in_block_comment = False found_block_comment_end = True i += 1 # 跳过'/' i += 1 continue # 检查行注释 if char == '/' and i + 1 < n and line_chars[i + 1] == '/': is_line_comment = True current_in_block_comment = False break # 行注释后无需继续分析 # 检查块注释开始 if char == '/' and i + 1 < n and line_chars[i + 1] == '*': current_in_block_comment = True is_block_comment_content = True # 记录块注释起始位置 if not in_block_comment: block_comment_data = { 'start_line': line_num, 'original_base_indent': leading_whitespace, 'normalized_base_indent': '', 'modified': False, 'skip_all_modification': False, 'content_indents': {} } # 规范化首行缩进 tab_count = space_count // tab_width remaining = space_count % tab_width normalized_indent = '\t' * tab_count + ' ' * remaining block_comment_data['normalized_base_indent'] = normalized_indent # 检查首行缩进是否被修改 indent_modified = normalized_indent != leading_whitespace block_comment_data['modified'] = indent_modified # 关键优化:如果首行无需修改,跳过整个注释块 block_comment_data['skip_all_modification'] = not indent_modified i += 1 # 跳过'*' continue # 检查字符串开始 if char in ('"', "'"): in_string = True string_char = char i += 1 continue # 非空白字符是代码内容 if char != ' ' and char != '\t': is_code_line = True i += 1 # 更新块注释状态 in_block_comment = current_in_block_comment # 处理块注释内部行 if is_block_comment_content and 'start_line' in block_comment_data: # 关键1:记录内容缩进(用于对齐一致性) if line_num not in block_comment_data['content_indents']: block_comment_data['content_indents'][line_num] = raw_line[len(leading_whitespace):].lstrip() # 关键2:如果整个注释块需要跳过修改 if block_comment_data['skip_all_modification']: # 完全保留原始行内容(包括缩进和行尾空格) processed_line = raw_line else: # 关键3:严格保持对齐一致性 normalized_base = block_comment_data['normalized_base_indent'] # 获取内容部分(保留原始对齐空格) content_part = block_comment_data['content_indents'][line_num] # 应用规范化缩进(基础缩进)加上原始对齐内容 processed_line = normalized_base + content_part # 记录是否被修改 if processed_line != raw_line: modified = True # 非注释块内容处理 elif is_code_line: tab_count = space_count // tab_width remaining = space_count % tab_width new_indent = '\t' * tab_count + ' ' * remaining if remaining else '\t' * tab_count # 去除行尾空格(非注释块) content_part = raw_line[len(leading_whitespace):].rstrip() processed_line = new_indent + content_part if processed_line != raw_line: modified = True # 行注释处理 elif is_line_comment: tab_count = space_count // tab_width remaining = space_count % tab_width new_indent = '\t' * tab_count + ' ' * remaining # 去除行尾空格(非注释块) content_part = raw_line[len(leading_whitespace):].rstrip() processed_line = new_indent + content_part if processed_line != raw_line: modified = True # 空白行处理 else: processed_line = raw_line.rstrip() if processed_line != raw_line: modified = True new_lines.append(processed_line) # 重置块注释状态 if found_block_comment_end: block_comment_data = { 'start_line': -1, 'skip_all_modification': False } # 写入文件 if modified: try: with open(file_path, 'w', encoding='utf-8') as f: content = '\n'.join(new_lines) if ends_with_newline: content += '\n' f.write(content) return True except Exception as e: print(f"Error writing {file_path}: {e}") return False return False def process_directory(path, tab_width=4, extensions=('.c', '.h', '.cpp', '.hpp', '.java', '.js', '.ts', '.py')): count = 0 for root, _, files in os.walk(path): for file in files: if any(file.endswith(ext) for ext in extensions): file_path = os.path.join(root, file) if normalize_indentation(file_path, tab_width): print(f"Processed: {file_path}") count += 1 print(f"Total files modified: {count}") return count if __name__ == '__main__': parser = argparse.ArgumentParser(description='高级缩进规范化工具(严格保持注释块内部对齐)') parser.add_argument('path', help='文件或目录路径') parser.add_argument('--tab', type=int, default=4, help='TAB对应的空格数(默认4)') parser.add_argument('--ext', nargs='+', default=['.c', '.h', '.cpp', '.hpp', '.java', '.js', '.ts', '.py'], help='处理的文件扩展名(默认: .c .h .cpp .hpp .java .js .ts .py)') parser.add_argument('--dry-run', action='store_true', help='仅显示更改但不实际修改文件') args = parser.parse_args() # 实际处理逻辑 if os.path.isdir(args.path): process_directory(args.path, args.tab, tuple(args.ext)) elif os.path.isfile(args.path): if normalize_indentation(args.path, args.tab): print(f"Processed: {args.path}") else: print("Error: Invalid path provided")
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值