| 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 command line 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 | results = [] |
| 21 | matched_files = set() |
| 22 | |
| 23 | # Edge Case: Empty file list |
| 24 | if not files: |
| 25 | return "" |
| 26 | |
| 27 | multiple_files = len(files) > 1 |
| 28 | |
| 29 | # Process each file |
| 30 | for file_path in files: |
| 31 | try: |
| 32 | # Edge Case: File not found or inaccessible |
| 33 | with open(file_path, 'r', encoding='utf-8') as file: |
| 34 | lines = file.readlines() |
| 35 | except FileNotFoundError: |
| 36 | # Edge Case: Continue processing other files if one is not found |
| 37 | continue |
| 38 | except IOError: |
| 39 | # Edge Case: Handle other I/O errors |
| 40 | continue |
| 41 | |
| 42 | # Process each line in the file |
| 43 | for line_num, line in enumerate(lines, 1): |
| 44 | # Prepare strings for comparison based on case sensitivity |
| 45 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 46 | search_line = line.lower() if case_insensitive else line |
| 47 | |
| 48 | # Determine if line matches based on exact match flag |
| 49 | if exact_match: |
| 50 | is_match = search_pattern == search_line.rstrip('\n') |
| 51 | else: |
| 52 | is_match = search_pattern in search_line |
| 53 | |
| 54 | # Apply invert flag |
| 55 | if invert_match: |
| 56 | is_match = not is_match |
| 57 | |
| 58 | # Edge Case: Handle matching logic with all flag combinations |
| 59 | if is_match: |
| 60 | if files_only: |
| 61 | matched_files.add(file_path) |
| 62 | # No need to process more lines in this file |
| 63 | break |
| 64 | else: |
| 65 | output_line = "" |
| 66 | # Add filename prefix if multiple files |
| 67 | if multiple_files: |
| 68 | output_line += f"{file_path}:" |
| 69 | # Add line number if requested |
| 70 | if line_numbers: |
| 71 | output_line += f"{line_num}:" |
| 72 | # Add the actual line |
| 73 | output_line += line |
| 74 | results.append(output_line) |
| 75 | |
| 76 | # Edge Case: Handle -l flag which only returns filenames |
| 77 | if files_only: |
| 78 | return "\n".join(sorted(matched_files)) + ("\n" if matched_files else "") |
| 79 | |
| 80 | # Edge Case: Handle empty results |
| 81 | if not results: |
| 82 | return "" |
| 83 | |
| 84 | # Edge Case: Remove trailing newline only if results exist |
| 85 | result = "".join(results) |
| 86 | # Ensure trailing newline if result exists and doesn't already end with one |
| 87 | if result and not result.endswith('\n'): |
| 88 | result += '\n' |
| 89 | return result |
| 90 | |
| 91 | # Handled Edge Cases: Empty file list, File not found or inaccessible, Other I/O errors, Matching logic with all flag combinations, -l flag which only returns filenames, Empty results, Remove trailing newline only if results exist |