| 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): Flags for customizing the command's behavior. |
| 8 | files (list[str]): One or more files to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: All matching lines, possibly with file names and line numbers prepended. |
| 12 | """ |
| 13 | # Parse flags |
| 14 | line_numbers = '-n' in flags |
| 15 | filenames_only = '-l' in flags |
| 16 | case_insensitive = '-i' in flags |
| 17 | invert_match = '-v' in flags |
| 18 | exact_match = '-x' in flags |
| 19 | |
| 20 | # Edge Case: Empty pattern |
| 21 | if not pattern: |
| 22 | pattern_to_match = '' |
| 23 | else: |
| 24 | pattern_to_match = pattern.lower() if case_insensitive else pattern |
| 25 | |
| 26 | result_lines = [] |
| 27 | matched_files = set() |
| 28 | |
| 29 | # Edge Case: No files provided |
| 30 | if not files: |
| 31 | return "" |
| 32 | |
| 33 | for filename in files: |
| 34 | try: |
| 35 | # Edge Case: File not found or cannot be opened |
| 36 | with open(filename, 'r', encoding='utf-8') as file: |
| 37 | lines = file.readlines() |
| 38 | except FileNotFoundError: |
| 39 | # Edge Case: File not found - skip the file |
| 40 | continue |
| 41 | except IOError: |
| 42 | # Edge Case: Other I/O error - skip the file |
| 43 | continue |
| 44 | |
| 45 | # Edge Case: File is empty |
| 46 | if not lines: |
| 47 | continue |
| 48 | |
| 49 | for line_num, line in enumerate(lines, start=1): |
| 50 | # Prepare line for comparison |
| 51 | line_to_check = line |
| 52 | if case_insensitive: |
| 53 | line_to_check = line.lower() |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if exact_match: |
| 57 | matches = pattern_to_match == line_to_check.rstrip('\n\r') |
| 58 | else: |
| 59 | matches = pattern_to_match in line_to_check |
| 60 | |
| 61 | # Apply invert flag |
| 62 | if invert_match: |
| 63 | matches = not matches |
| 64 | |
| 65 | if matches: |
| 66 | matched_files.add(filename) |
| 67 | |
| 68 | # Edge Case: Only filenames requested |
| 69 | if filenames_only: |
| 70 | # We only need to know the file matches, no need to collect lines |
| 71 | break |
| 72 | |
| 73 | # Build output line |
| 74 | output_line = "" |
| 75 | |
| 76 | # Add filename if multiple files |
| 77 | # Edge Case: Single file vs multiple files |
| 78 | if len(files) > 1: |
| 79 | output_line += f"{filename}:" |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | output_line += f"{line_num}:" |
| 84 | |
| 85 | output_line += line |
| 86 | result_lines.append(output_line) |
| 87 | |
| 88 | # Edge Case: Filenames only requested |
| 89 | if filenames_only: |
| 90 | # Return filenames in the order they were encountered |
| 91 | output_files = [] |
| 92 | for filename in files: |
| 93 | if filename in matched_files: |
| 94 | output_files.append(filename) |
| 95 | return "\n".join(output_files) + ("\n" if output_files else "") |
| 96 | |
| 97 | return "".join(result_lines) + ("\n" if result_lines else "") |
| 98 | |
| 99 | # Handled Edge Cases: Empty pattern, No files provided, File not found or cannot be opened, File is empty, Only filenames requested, Single file vs multiple files, Filenames only requested |