| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search files for lines matching a search string and return all matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern (str): The string to search for |
| 7 | flags (str): Space-separated flags for customizing behavior (-n, -l, -i, -v, -x) |
| 8 | files (list[str]): List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags |
| 14 | flag_list = flags.split() if flags else [] |
| 15 | line_numbers = '-n' in flag_list |
| 16 | files_only = '-l' in flag_list |
| 17 | case_insensitive = '-i' in flag_list |
| 18 | invert_match = '-v' in flag_list |
| 19 | exact_match = '-x' in flag_list |
| 20 | |
| 21 | # Edge Case: Empty pattern |
| 22 | if not pattern: |
| 23 | pattern = '' |
| 24 | |
| 25 | # Edge Case: No files provided |
| 26 | if not files: |
| 27 | return '' |
| 28 | |
| 29 | # Prepare pattern for comparison |
| 30 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 31 | |
| 32 | results = [] |
| 33 | matched_files = set() |
| 34 | |
| 35 | # Edge Case: File not found |
| 36 | for file_path in files: |
| 37 | try: |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Skip files that don't exist |
| 42 | continue |
| 43 | |
| 44 | file_has_match = False |
| 45 | |
| 46 | # Edge Case: Empty file |
| 47 | if not lines: |
| 48 | continue |
| 49 | |
| 50 | for line_num, line in enumerate(lines, 1): |
| 51 | # Remove newline character for processing |
| 52 | line_content = line.rstrip('\n') |
| 53 | |
| 54 | # Determine if line matches |
| 55 | if case_insensitive: |
| 56 | line_to_check = line_content.lower() |
| 57 | pattern_to_check = search_pattern # Already lowercased |
| 58 | else: |
| 59 | line_to_check = line_content |
| 60 | pattern_to_check = pattern # Use original case pattern |
| 61 | |
| 62 | if exact_match: |
| 63 | is_match = (line_to_check == pattern_to_check) |
| 64 | else: |
| 65 | is_match = (pattern_to_check in line_to_check) |
| 66 | |
| 67 | # Apply invert flag |
| 68 | if invert_match: |
| 69 | is_match = not is_match |
| 70 | |
| 71 | if is_match: |
| 72 | file_has_match = True |
| 73 | |
| 74 | # If we only need file names, we can break early |
| 75 | if files_only: |
| 76 | matched_files.add(file_path) |
| 77 | break |
| 78 | |
| 79 | # Build output line |
| 80 | output_line = '' |
| 81 | |
| 82 | # Add filename prefix if multiple files |
| 83 | if len(files) > 1: |
| 84 | output_line += f'{file_path}:' |
| 85 | |
| 86 | # Add line number if requested |
| 87 | if line_numbers: |
| 88 | output_line += f'{line_num}:' |
| 89 | |
| 90 | # Add the actual line content |
| 91 | output_line += line_content |
| 92 | |
| 93 | results.append(output_line) |
| 94 | |
| 95 | # If we're only outputting filenames and this file had a match |
| 96 | if files_only and file_has_match: |
| 97 | matched_files.add(file_path) |
| 98 | |
| 99 | # Edge Case: -l flag with no matches |
| 100 | if files_only: |
| 101 | # Return filenames in the order they were provided |
| 102 | output_files = [f for f in files if f in matched_files] |
| 103 | result = '\n'.join(output_files) |
| 104 | # Add trailing newline if there are matches |
| 105 | if result: |
| 106 | result += '\n' |
| 107 | return result |
| 108 | |
| 109 | # Edge Case: No matches found |
| 110 | result = '\n'.join(results) |
| 111 | # Add trailing newline if there are matches |
| 112 | if result: |
| 113 | result += '\n' |
| 114 | return result |
| 115 | |
| 116 | # Handled Edge Cases: Empty pattern, no files provided, file not found, empty file, -l flag with no matches, no matches found |