| 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): Flags for customizing the command's behavior. |
| 8 | files (list[str]): One or more files to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers prepended. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert matching. |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags |
| 21 | line_numbers = '-n' in flags |
| 22 | files_only = '-l' in flags |
| 23 | case_insensitive = '-i' in flags |
| 24 | invert_match = '-v' in flags |
| 25 | exact_match = '-x' in flags |
| 26 | |
| 27 | # Prepare pattern for comparison |
| 28 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 29 | |
| 30 | # Initialize results |
| 31 | results = [] |
| 32 | matching_files = set() |
| 33 | |
| 34 | # Process each file |
| 35 | for filename in files: |
| 36 | try: |
| 37 | with open(filename, 'r', encoding='utf-8') as file: |
| 38 | # Edge Case: Handle files that cannot be read |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: Handle files that do not exist |
| 42 | continue |
| 43 | except IOError: |
| 44 | # Edge Case: Handle other I/O errors |
| 45 | continue |
| 46 | |
| 47 | # Process each line |
| 48 | for line_num, line in enumerate(lines, start=1): |
| 49 | # Prepare line for comparison |
| 50 | compare_line = line.lower() if case_insensitive else line |
| 51 | |
| 52 | # Determine if line matches |
| 53 | if exact_match: |
| 54 | matches = search_pattern == compare_line.rstrip('\n') |
| 55 | else: |
| 56 | matches = search_pattern in compare_line |
| 57 | |
| 58 | # Apply invert flag |
| 59 | if invert_match: |
| 60 | matches = not matches |
| 61 | |
| 62 | # If line matches, collect results |
| 63 | if matches: |
| 64 | matching_files.add(filename) |
| 65 | |
| 66 | # If only returning file names, we can break early |
| 67 | if files_only: |
| 68 | break |
| 69 | |
| 70 | # Build output line |
| 71 | output_line = "" |
| 72 | |
| 73 | # Add filename prefix if multiple files |
| 74 | if len(files) > 1: |
| 75 | output_line += f"{filename}:" |
| 76 | |
| 77 | # Add line number if requested |
| 78 | if line_numbers: |
| 79 | output_line += f"{line_num}:" |
| 80 | |
| 81 | # Add the actual line content |
| 82 | output_line += line |
| 83 | |
| 84 | results.append(output_line) |
| 85 | |
| 86 | # Edge Case: Handle -l flag by returning only filenames |
| 87 | if files_only: |
| 88 | # Return matching filenames in the order they were encountered |
| 89 | ordered_matching_files = [f for f in files if f in matching_files] |
| 90 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 91 | |
| 92 | # Edge Case: Handle empty results |
| 93 | if not results: |
| 94 | return "" |
| 95 | |
| 96 | # Join all results with newlines |
| 97 | return "".join(results) |
| 98 | |
| 99 | # Handled Edge Cases: Files that cannot be read, files that do not exist, other I/O errors, -l flag with early breaking, empty results, exact matching, invert matching, case insensitive matching, line numbering, multiple files with filename prefix |