| 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. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers. |
| 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 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | filenames_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Edge Case: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | # Edge Case: Pattern is empty |
| 33 | if not pattern: |
| 34 | pattern = "" |
| 35 | |
| 36 | result_lines = [] |
| 37 | matching_files = set() |
| 38 | |
| 39 | # Prepare pattern for case-insensitive matching |
| 40 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 41 | |
| 42 | # Process each file |
| 43 | for file_path in files: |
| 44 | try: |
| 45 | with open(file_path, 'r', encoding='utf-8') as file: |
| 46 | lines = file.readlines() |
| 47 | |
| 48 | # Edge Case: File is empty or unreadable |
| 49 | if not lines: |
| 50 | # Still need to check for matches in empty files (for exact match with empty pattern) |
| 51 | if exact_match and pattern == "": |
| 52 | # An empty line in an empty file matches an empty pattern with -x |
| 53 | # But an empty file has no lines, so no match |
| 54 | pass |
| 55 | |
| 56 | file_has_match = False |
| 57 | |
| 58 | # Process each line |
| 59 | for line_num, line in enumerate(lines, 1): |
| 60 | # Prepare line for comparison |
| 61 | compare_line = line.lower() if case_insensitive else line |
| 62 | |
| 63 | # Determine if line matches |
| 64 | if exact_match: |
| 65 | matches = (compare_line.rstrip('\n\r') == search_pattern) |
| 66 | else: |
| 67 | matches = (search_pattern in compare_line) |
| 68 | |
| 69 | # Apply invert flag |
| 70 | if invert_match: |
| 71 | matches = not matches |
| 72 | |
| 73 | # Edge Case: Line ending handling - strip line ending for comparison but keep for output |
| 74 | original_line = line # Keep original line for output |
| 75 | |
| 76 | if matches: |
| 77 | file_has_match = True |
| 78 | |
| 79 | # If only filenames are needed, we can break early |
| 80 | if filenames_only: |
| 81 | matching_files.add(file_path) |
| 82 | break |
| 83 | |
| 84 | # Build output line |
| 85 | output_line = "" |
| 86 | |
| 87 | # Add filename prefix if multiple files |
| 88 | # Edge Case: Single file should not have filename prefix |
| 89 | if len(files) > 1: |
| 90 | output_line += f"{file_path}:" |
| 91 | |
| 92 | # Add line number if requested |
| 93 | if line_numbers: |
| 94 | output_line += f"{line_num}:" |
| 95 | |
| 96 | # Add the actual line content |
| 97 | output_line += original_line |
| 98 | |
| 99 | result_lines.append(output_line) |
| 100 | |
| 101 | # If filenames only and this file had a match |
| 102 | if filenames_only and file_has_match: |
| 103 | matching_files.add(file_path) |
| 104 | |
| 105 | except FileNotFoundError: |
| 106 | # Edge Case: File not found - according to specification we don't need to handle this specially |
| 107 | # But we should not crash, so we just continue |
| 108 | continue |
| 109 | except Exception: |
| 110 | # Edge Case: Other file reading errors - continue with next file |
| 111 | continue |
| 112 | |
| 113 | # Edge Case: When -l flag is used, return only filenames |
| 114 | if filenames_only: |
| 115 | # Return filenames in the order they appeared in the input |
| 116 | ordered_matching_files = [f for f in files if f in matching_files] |
| 117 | return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "") |
| 118 | |
| 119 | # Edge Case: No matches found |
| 120 | if not result_lines: |
| 121 | return "" |
| 122 | |
| 123 | # Edge Case: Remove trailing newline if it exists |
| 124 | return "".join(result_lines) |
| 125 | |
| 126 | # Handled Edge Cases: No files provided, empty pattern, file not found, file reading errors, empty files, line ending handling, single file (no filename prefix), no matches found, trailing newline removal |