a0ms1n/AI-Code-Detector_for-Competitive-Programming
7
1 2import re3import subprocess4import shutil5preprocessor_pattern = re.compile(r'^\s*#.*$', re.MULTILINE)6block_comment_pattern = re.compile(r'/\*.*?\*/', re.DOTALL)7using_pattern = re.compile(r'^\s*using\s+[^\n;]+;', re.MULTILINE)8typedef_pattern = re.compile(r'^\s*typedef\s+[^\n;]+;', re.MULTILINE)9line_comment_pattern = re.compile(r'//.*')10 11def remove_comments(code):12 code = block_comment_pattern.sub('', code)13 code = line_comment_pattern.sub('', code)14 return code15 16def replace_preprocessor(code):17 code = preprocessor_pattern.sub('<PREPROCESSOR>', code)18 code = using_pattern.sub('<PREPROCESSOR>', code)19 code = typedef_pattern.sub('<PREPROCESSOR>',code)20 return code21 22def strip_lines(text, max_blank_lines=0):23 text += '\n'24 lines = text.splitlines()25 kept = []26 consec = 027 for line in lines:28 if line.strip() == "":29 consec +=130 else:31 consec = 032 if consec <= max_blank_lines:33 kept.append(line)34 return '\n'.join(kept) 35 36space_braces_function_pattern = re.compile(r'(\([^\)]*\))\s*\{')37multiline_function_pattern = re.compile(r'(\([^\)]*\))\s*\n\s*\{')38 39def normalize_braces(code):40 code = multiline_function_pattern.sub(r'\1{', code)41 code = space_braces_function_pattern.sub(r'\1{',code)42 return code43 44def format_cpp(code: str, style: str = "Google") -> str:45 if not shutil.which("clang-format"):46 raise EnvironmentError("clang-format is not installed or not in PATH.")47 48 result = subprocess.run(49 ["clang-format", f"--style={style}"],50 input=code.encode(),51 stdout=subprocess.PIPE,52 stderr=subprocess.PIPE,53 check=True54 )55 56 return result.stdout.decode()