| 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 that customize behavior. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: All matching lines, formatted according to the flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert matching (return non-matching lines). |
| 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 | files_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 | results = [] |
| 29 | matched_files = set() |
| 30 | |
| 31 | # Edge Case: No files provided |
| 32 | if not files: |
| 33 | return "" |
| 34 | |
| 35 | # Edge Case: Empty pattern |
| 36 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 37 | |
| 38 | for file_path in files: |
| 39 | try: |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | except FileNotFoundError: |
| 43 | # Edge Case: File not found - skip the file |
| 44 | continue |
| 45 | |
| 46 | # Edge Case: Empty file |
| 47 | if not lines: |
| 48 | continue |
| 49 | |
| 50 | file_has_match = False |
| 51 | |
| 52 | for i, line in enumerate(lines, 1): # 1-indexed line numbers |
| 53 | # Prepare line for comparison |
| 54 | compare_line = line.rstrip('\n').lower() if case_insensitive else line.rstrip('\n') |
| 55 | |
| 56 | # Determine if line matches |
| 57 | if exact_match: |
| 58 | matches = (compare_line == search_pattern) |
| 59 | else: |
| 60 | matches = (search_pattern in compare_line) |
| 61 | |
| 62 | # Apply invert flag |
| 63 | if invert_match: |
| 64 | matches = not matches |
| 65 | |
| 66 | if matches: |
| 67 | file_has_match = True |
| 68 | |
| 69 | # Edge Case: Only file names requested |
| 70 | if files_only: |
| 71 | matched_files.add(file_path) |
| 72 | break # No need to check more lines in this file |
| 73 | |
| 74 | # Build output line |
| 75 | output_line = "" |
| 76 | |
| 77 | # Add filename prefix if multiple files |
| 78 | if len(files) > 1: |
| 79 | output_line += f"{file_path}:" |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | output_line += f"{i}:" |
| 84 | |
| 85 | # Add the actual line content |
| 86 | output_line += line |
| 87 | |
| 88 | results.append(output_line) |
| 89 | |
| 90 | # Edge Case: File has match and only file names requested |
| 91 | if file_has_match and files_only: |
| 92 | matched_files.add(file_path) |
| 93 | |
| 94 | # Edge Case: Only file names requested |
| 95 | if files_only: |
| 96 | # Return file names in the order they were encountered |
| 97 | ordered_results = [f for f in files if f in matched_files] |
| 98 | return "\n".join(ordered_results) + ("\n" if ordered_results else "") |
| 99 | |
| 100 | return "".join(results) |
| 101 | |
| 102 | # Handled Edge Cases: No files provided, Empty pattern, File not found, Empty file, Only file names requested |