| 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: The string to search for |
| 7 | flags: String containing zero or more flags (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing all matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags |
| 14 | line_numbers = '-n' in flags |
| 15 | files_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 | matching_files = set() |
| 28 | |
| 29 | # Edge Case: No files provided |
| 30 | if not files: |
| 31 | return "" |
| 32 | |
| 33 | # Edge Case: Empty files list |
| 34 | if len(files) == 0: |
| 35 | return "" |
| 36 | |
| 37 | multiple_files = len(files) > 1 |
| 38 | |
| 39 | for file_path in files: |
| 40 | try: |
| 41 | with open(file_path, 'r', encoding='utf-8') as file: |
| 42 | lines = file.readlines() |
| 43 | |
| 44 | # Edge Case: File cannot be read |
| 45 | except IOError: |
| 46 | continue |
| 47 | |
| 48 | for line_num, line in enumerate(lines, 1): |
| 49 | # Remove newline for matching but keep it for output |
| 50 | line_content = line.rstrip('\n') |
| 51 | |
| 52 | # Determine if line matches |
| 53 | if case_insensitive: |
| 54 | line_to_check = line_content.lower() |
| 55 | else: |
| 56 | line_to_check = line_content |
| 57 | |
| 58 | if exact_match: |
| 59 | is_match = (line_to_check == pattern_to_match) |
| 60 | else: |
| 61 | is_match = (pattern_to_match in line_to_check) |
| 62 | |
| 63 | # Edge Case: Invert match flag |
| 64 | if invert_match: |
| 65 | is_match = not is_match |
| 66 | |
| 67 | if is_match: |
| 68 | # Edge Case: Files only flag |
| 69 | if files_only: |
| 70 | matching_files.add(file_path) |
| 71 | # No need to process more lines for this file |
| 72 | break |
| 73 | else: |
| 74 | output_line = "" |
| 75 | # Edge Case: Multiple files - prepend filename |
| 76 | if multiple_files: |
| 77 | output_line += f"{file_path}:" |
| 78 | # Edge Case: Line numbers flag |
| 79 | if line_numbers: |
| 80 | output_line += f"{line_num}:" |
| 81 | output_line += line |
| 82 | result_lines.append(output_line) |
| 83 | |
| 84 | # Edge Case: Files only flag - return file names |
| 85 | if files_only: |
| 86 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 87 | |
| 88 | # Edge Case: No matches found |
| 89 | if not result_lines: |
| 90 | return "" |
| 91 | |
| 92 | return "".join(result_lines) |
| 93 | |
| 94 | # Handled Edge Cases: Empty pattern, No files provided, Empty files list, File cannot be read, Invert match flag, Files only flag, Multiple files - prepend filename, Line numbers flag, No matches found |