| 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 | # Edge Case: Empty pattern |
| 28 | if not pattern: |
| 29 | pattern_to_match = "" |
| 30 | else: |
| 31 | pattern_to_match = pattern.lower() if case_insensitive else pattern |
| 32 | |
| 33 | multiple_files = len(files) > 1 |
| 34 | |
| 35 | for file_path in files: |
| 36 | try: |
| 37 | # Edge Case: File not found or inaccessible |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | for line_num, line in enumerate(file, 1): |
| 40 | # Process line for matching |
| 41 | line_to_check = line.lower() if case_insensitive else line |
| 42 | |
| 43 | # Determine if line matches |
| 44 | if exact_match: |
| 45 | matches = pattern_to_match == line_to_check.rstrip('\n\r') |
| 46 | else: |
| 47 | matches = pattern_to_match in line_to_check |
| 48 | |
| 49 | # Apply invert flag |
| 50 | if invert_match: |
| 51 | matches = not matches |
| 52 | |
| 53 | if matches: |
| 54 | # Edge Case: Only file names requested |
| 55 | if files_only: |
| 56 | matched_files.add(file_path) |
| 57 | # No need to process more lines in this file |
| 58 | break |
| 59 | else: |
| 60 | output_line = line.rstrip('\n\r') |
| 61 | |
| 62 | # Build output with appropriate prefixes |
| 63 | if multiple_files: |
| 64 | output_line = f"{file_path}:{output_line}" |
| 65 | |
| 66 | if line_numbers: |
| 67 | if multiple_files: |
| 68 | # Insert line number after filename |
| 69 | parts = output_line.split(':', 1) |
| 70 | output_line = f"{parts[0]}:{line_num}:{parts[1] if len(parts) > 1 else ''}" |
| 71 | else: |
| 72 | output_line = f"{line_num}:{output_line}" |
| 73 | |
| 74 | results.append(output_line) |
| 75 | except FileNotFoundError: |
| 76 | # Edge Case: File not found - continue with other files |
| 77 | continue |
| 78 | except IOError: |
| 79 | # Edge Case: Other I/O error - continue with other files |
| 80 | continue |
| 81 | |
| 82 | # Edge Case: Only file names requested |
| 83 | if files_only: |
| 84 | return '\n'.join(sorted(matched_files)) |
| 85 | |
| 86 | return '\n'.join(results) |
| 87 | |
| 88 | # Handled Edge Cases: Empty file list, Empty pattern, File not found or inaccessible, Only file names requested |