| 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 # Cannot match entire line if pattern is empty |
| 35 | |
| 36 | # Prepare pattern for comparison |
| 37 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 38 | |
| 39 | results = [] |
| 40 | matching_files = set() |
| 41 | |
| 42 | multiple_files = len(files) > 1 |
| 43 | |
| 44 | for file_path in files: |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8') as file: |
| 47 | lines = file.readlines() |
| 48 | |
| 49 | # Edge Case: File is empty or unreadable |
| 50 | if not lines: |
| 51 | if file_names_only and False: # No match possible in empty file |
| 52 | pass |
| 53 | continue |
| 54 | |
| 55 | for i, line in enumerate(lines, start=1): |
| 56 | # Remove newline for processing but keep for output |
| 57 | line_content = line.rstrip('\n') |
| 58 | |
| 59 | # Prepare line for comparison |
| 60 | compare_line = line_content.lower() if case_insensitive else line_content |
| 61 | |
| 62 | # Determine if line matches |
| 63 | if exact_match: |
| 64 | matches = (compare_line == search_pattern) |
| 65 | else: |
| 66 | matches = (search_pattern in compare_line) |
| 67 | |
| 68 | # Apply invert flag |
| 69 | if invert_match: |
| 70 | matches = not matches |
| 71 | |
| 72 | if matches: |
| 73 | matching_files.add(file_path) |
| 74 | |
| 75 | if not file_names_only: |
| 76 | output_line = line_content |
| 77 | |
| 78 | # Build prefix |
| 79 | prefix_parts = [] |
| 80 | |
| 81 | # Edge Case: Multiple files - prepend filename |
| 82 | if multiple_files: |
| 83 | prefix_parts.append(file_path) |
| 84 | |
| 85 | # Edge Case: Line numbers requested |
| 86 | if line_numbers: |
| 87 | prefix_parts.append(str(i)) |
| 88 | |
| 89 | if prefix_parts: |
| 90 | output_line = ':'.join(prefix_parts) + ':' + output_line |
| 91 | |
| 92 | results.append(output_line) |
| 93 | |
| 94 | # Edge Case: File not found or permission denied |
| 95 | except (FileNotFoundError, PermissionError): |
| 96 | # According to typical grep behavior, we silently continue |
| 97 | continue |
| 98 | |
| 99 | # Edge Case: Only file names requested |
| 100 | if file_names_only: |
| 101 | # Return files in the order they were given, but only those with matches |
| 102 | output_files = [f for f in files if f in matching_files] |
| 103 | return '\n'.join(output_files) + ('\n' if output_files else '') |
| 104 | |
| 105 | return '\n'.join(results) + ('\n' if results else '') |
| 106 | |
| 107 | # Handled Edge Cases: No files provided, empty pattern, file is empty or unreadable, multiple files with filename prefix, line numbers requested, file not found or permission denied, only file names requested |