| 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 | 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 | # Even with empty pattern, we follow the logic |
| 35 | pass |
| 36 | |
| 37 | results = [] |
| 38 | matching_files = set() |
| 39 | |
| 40 | # Prepare pattern for comparison |
| 41 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 42 | |
| 43 | # Edge Case: File not found or cannot be read |
| 44 | for file_path in files: |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8') as file: |
| 47 | lines = file.readlines() |
| 48 | except (FileNotFoundError, IOError): |
| 49 | # Skip files that cannot be opened |
| 50 | continue |
| 51 | |
| 52 | file_has_match = False |
| 53 | |
| 54 | for line_num, line in enumerate(lines, 1): |
| 55 | # Prepare line for comparison |
| 56 | compare_line = line.lower() if case_insensitive else line |
| 57 | |
| 58 | # Determine if line matches |
| 59 | if exact_match: |
| 60 | matches = (compare_line.rstrip('\n\r') == search_pattern) |
| 61 | else: |
| 62 | matches = (search_pattern in compare_line) |
| 63 | |
| 64 | # Apply invert flag |
| 65 | if invert_match: |
| 66 | matches = not matches |
| 67 | |
| 68 | if matches: |
| 69 | file_has_match = True |
| 70 | |
| 71 | # Edge Case: Only filenames requested |
| 72 | if filenames_only: |
| 73 | matching_files.add(file_path) |
| 74 | # No need to process more lines for this file once we know it matches |
| 75 | break |
| 76 | else: |
| 77 | output_line = "" |
| 78 | # Edge Case: Multiple files - prepend filename |
| 79 | if len(files) > 1: |
| 80 | output_line += f"{file_path}:" |
| 81 | # Edge Case: Line numbers requested |
| 82 | if line_numbers: |
| 83 | output_line += f"{line_num}:" |
| 84 | output_line += line |
| 85 | results.append(output_line) |
| 86 | |
| 87 | # Edge Case: Filenames only and this file had matches |
| 88 | if filenames_only and file_has_match: |
| 89 | matching_files.add(file_path) |
| 90 | |
| 91 | # Edge Case: Filenames only mode |
| 92 | if filenames_only: |
| 93 | # Return filenames in the order they were encountered |
| 94 | ordered_results = [f for f in files if f in matching_files] |
| 95 | return "\n".join(ordered_results) + ("\n" if ordered_results else "") |
| 96 | |
| 97 | return "".join(results) |
| 98 | |
| 99 | # Handled Edge Cases: No files provided, Pattern is empty, File not found or cannot be read, Only filenames requested, Multiple files - prepend filename, Line numbers requested, Filenames only mode |