| 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: No files provided |
| 22 | if not files: |
| 23 | return "" |
| 24 | |
| 25 | # Edge Case: Pattern is empty |
| 26 | if not pattern: |
| 27 | exact_match = False # Disable exact match if pattern is empty |
| 28 | |
| 29 | results = [] |
| 30 | matching_files = set() |
| 31 | |
| 32 | # Prepare pattern for case-insensitive comparison |
| 33 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 34 | |
| 35 | # Edge Case: File not found or cannot be read |
| 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, IOError): |
| 41 | # Skip files that cannot be opened |
| 42 | continue |
| 43 | |
| 44 | file_has_match = False |
| 45 | |
| 46 | for i, line in enumerate(lines, 1): # 1-indexed line numbers |
| 47 | # Prepare line for comparison |
| 48 | compare_line = line.lower() if case_insensitive else line |
| 49 | |
| 50 | # Determine if line matches |
| 51 | if exact_match: |
| 52 | matches = compare_line.rstrip('\n\r') == search_pattern |
| 53 | else: |
| 54 | matches = search_pattern in compare_line |
| 55 | |
| 56 | # Apply invert flag |
| 57 | if invert_match: |
| 58 | matches = not matches |
| 59 | |
| 60 | if matches: |
| 61 | file_has_match = True |
| 62 | |
| 63 | # Edge Case: Only file names requested |
| 64 | if files_only: |
| 65 | matching_files.add(file_path) |
| 66 | break # No need to check more lines in this file |
| 67 | |
| 68 | # Build output line |
| 69 | output_line = "" |
| 70 | |
| 71 | # Add filename prefix if multiple files |
| 72 | if len(files) > 1: |
| 73 | output_line += f"{file_path}:" |
| 74 | |
| 75 | # Add line number if requested |
| 76 | if line_numbers: |
| 77 | output_line += f"{i}:" |
| 78 | |
| 79 | # Add the actual line content |
| 80 | output_line += line |
| 81 | |
| 82 | results.append(output_line) |
| 83 | |
| 84 | # Edge Case: Only file names requested and this file had a match |
| 85 | if files_only and file_has_match: |
| 86 | matching_files.add(file_path) |
| 87 | |
| 88 | # Edge Case: Only file names requested |
| 89 | if files_only: |
| 90 | # Return file names in the order they were provided |
| 91 | ordered_results = [f for f in files if f in matching_files] |
| 92 | return "\n".join(ordered_results) + ("\n" if ordered_results else "") |
| 93 | |
| 94 | return "".join(results) |
| 95 | |
| 96 | # Handled Edge Cases: No files provided; Pattern is empty; File not found or cannot be read; Only file names requested |