| 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 | file_names_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 | exact_match = False # Disable exact match if pattern is empty |
| 35 | |
| 36 | results = [] |
| 37 | matching_files = set() |
| 38 | |
| 39 | # Prepare pattern for case-insensitive matching |
| 40 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 41 | |
| 42 | # Edge Case: File not found or inaccessible |
| 43 | for file_path in files: |
| 44 | try: |
| 45 | with open(file_path, 'r', encoding='utf-8') as file: |
| 46 | lines = file.readlines() |
| 47 | except FileNotFoundError: |
| 48 | # Skip files that don't exist |
| 49 | continue |
| 50 | except IOError: |
| 51 | # Skip files that can't be read |
| 52 | continue |
| 53 | |
| 54 | file_has_match = False |
| 55 | |
| 56 | # Edge Case: Empty file |
| 57 | if not lines: |
| 58 | # Process as empty list, no matches possible |
| 59 | pass |
| 60 | |
| 61 | for line_num, line in enumerate(lines, start=1): |
| 62 | # Prepare line for comparison |
| 63 | compare_line = line.lower() if case_insensitive else line |
| 64 | |
| 65 | # Determine if line matches |
| 66 | if exact_match: |
| 67 | matches = (compare_line.rstrip('\n\r') == search_pattern) |
| 68 | else: |
| 69 | matches = (search_pattern in compare_line) |
| 70 | |
| 71 | # Apply invert flag |
| 72 | if invert_match: |
| 73 | matches = not matches |
| 74 | |
| 75 | if matches: |
| 76 | file_has_match = True |
| 77 | |
| 78 | # Edge Case: File names only requested |
| 79 | if file_names_only: |
| 80 | matching_files.add(file_path) |
| 81 | else: |
| 82 | output_line = line # Preserve original line including newline |
| 83 | |
| 84 | # Build output with appropriate prefixes |
| 85 | prefix_parts = [] |
| 86 | |
| 87 | # Edge Case: Multiple files - prepend filename |
| 88 | if len(files) > 1: |
| 89 | prefix_parts.append(file_path) |
| 90 | |
| 91 | # Edge Case: Line numbers requested |
| 92 | if line_numbers: |
| 93 | prefix_parts.append(str(line_num)) |
| 94 | |
| 95 | if prefix_parts: |
| 96 | output_line = ':'.join(prefix_parts) + ':' + output_line |
| 97 | |
| 98 | results.append(output_line) |
| 99 | |
| 100 | # Edge Case: File names only and this file had a match |
| 101 | if file_names_only and file_has_match: |
| 102 | matching_files.add(file_path) |
| 103 | |
| 104 | # Edge Case: File names only requested |
| 105 | if file_names_only: |
| 106 | # Return files in the order they were provided |
| 107 | ordered_results = [f for f in files if f in matching_files] |
| 108 | return '\n'.join(ordered_results) + ('\n' if ordered_results else '') |
| 109 | |
| 110 | return ''.join(results) |
| 111 | |
| 112 | # Handled Edge Cases: No files provided, Pattern is empty, File not found or inaccessible, Empty file, File names only requested, Multiple files - prepend filename, Line numbers requested |