| 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: 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 | line_numbers = '-n' in flags |
| 22 | files_only = '-l' in flags |
| 23 | case_insensitive = '-i' in flags |
| 24 | invert_match = '-v' in flags |
| 25 | exact_match = '-x' in flags |
| 26 | |
| 27 | # Edge Case: Empty pattern |
| 28 | if not pattern: |
| 29 | # An empty pattern matches every line |
| 30 | pass |
| 31 | |
| 32 | # Edge Case: Empty files list |
| 33 | if not files: |
| 34 | return "" |
| 35 | |
| 36 | results = [] |
| 37 | matched_files = set() |
| 38 | |
| 39 | multiple_files = len(files) > 1 |
| 40 | |
| 41 | # Prepare pattern for case-insensitive matching |
| 42 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 43 | |
| 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: |
| 49 | # Edge Case: File not found - according to problem description, we should just skip |
| 50 | continue |
| 51 | |
| 52 | file_has_match = False |
| 53 | |
| 54 | for line_num, line in enumerate(lines, 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: When -l flag is used, we only need to know that file has a match |
| 72 | if files_only: |
| 73 | matched_files.add(file_path) |
| 74 | break # No need to check more lines in this file |
| 75 | |
| 76 | output_line = line # Preserve original line including newline |
| 77 | |
| 78 | # Build output |
| 79 | if multiple_files: |
| 80 | output_line = f"{file_path}:{output_line}" |
| 81 | |
| 82 | if line_numbers: |
| 83 | if multiple_files: |
| 84 | # Insert line number after the filename |
| 85 | parts = output_line.split(':', 1) |
| 86 | output_line = f"{parts[0]}:{line_num}:{parts[1]}" |
| 87 | else: |
| 88 | output_line = f"{line_num}:{output_line}" |
| 89 | |
| 90 | results.append(output_line) |
| 91 | |
| 92 | # Edge Case: When -l flag is used and we've found a match in this file |
| 93 | if files_only and file_has_match: |
| 94 | continue # Move to next file |
| 95 | |
| 96 | # Edge Case: When -l flag is used, return only file names |
| 97 | if files_only: |
| 98 | return '\n'.join(sorted(matched_files)) + ('\n' if matched_files else '') |
| 99 | |
| 100 | # Edge Case: No matches found |
| 101 | if not results: |
| 102 | return "" |
| 103 | |
| 104 | return ''.join(results) |
| 105 | |
| 106 | # Handled Edge Cases: Empty pattern, empty files list, file not found, no matches found, -l flag with matches |