| 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 | # 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 f: |
| 47 | lines = f.readlines() |
| 48 | except (FileNotFoundError, IOError): |
| 49 | # Skip unreadable files |
| 50 | continue |
| 51 | |
| 52 | file_has_match = False |
| 53 | |
| 54 | for i, line in enumerate(lines, start=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 collect lines |
| 75 | break |
| 76 | else: |
| 77 | output_line = line # Keep original line for output |
| 78 | |
| 79 | # Build output |
| 80 | prefix_parts = [] |
| 81 | |
| 82 | # Edge Case: Multiple files - prepend filename |
| 83 | if len(files) > 1: |
| 84 | prefix_parts.append(file_path) |
| 85 | |
| 86 | # Edge Case: Line numbers requested |
| 87 | if line_numbers: |
| 88 | prefix_parts.append(str(i)) |
| 89 | |
| 90 | prefix = ':'.join(prefix_parts) |
| 91 | |
| 92 | # Edge Case: Prefix exists |
| 93 | if prefix: |
| 94 | results.append(f"{prefix}:{output_line}") |
| 95 | else: |
| 96 | results.append(output_line) |
| 97 | |
| 98 | # Edge Case: Filenames only and this file had a match |
| 99 | if filenames_only and file_has_match: |
| 100 | matching_files.add(file_path) |
| 101 | |
| 102 | # Edge Case: Only filenames requested |
| 103 | if filenames_only: |
| 104 | # Return files in the order they were provided |
| 105 | ordered_results = [f for f in files if f in matching_files] |
| 106 | return '\n'.join(ordered_results) + ('\n' if ordered_results else '') |
| 107 | |
| 108 | return ''.join(results) |
| 109 | |
| 110 | # 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, Prefix exists |