| 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 | multiple_files = len(files) > 1 |
| 34 | |
| 35 | for file_path in files: |
| 36 | try: |
| 37 | # Edge Case: File not found or cannot be read |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # According to specification, we should just skip files that can't be opened |
| 42 | continue |
| 43 | except Exception: |
| 44 | # Handle any other file reading errors |
| 45 | continue |
| 46 | |
| 47 | file_has_match = False |
| 48 | |
| 49 | for line_num, line in enumerate(lines, 1): |
| 50 | # Process line for comparison |
| 51 | line_to_check = line.rstrip('\n') |
| 52 | if case_insensitive: |
| 53 | line_to_check = line_to_check.lower() |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if exact_match: |
| 57 | is_match = pattern_to_match == line_to_check |
| 58 | else: |
| 59 | is_match = pattern_to_match in line_to_check |
| 60 | |
| 61 | # Apply invert flag |
| 62 | if invert_match: |
| 63 | is_match = not is_match |
| 64 | |
| 65 | if is_match: |
| 66 | file_has_match = True |
| 67 | |
| 68 | # Edge Case: Only file names requested |
| 69 | if files_only: |
| 70 | matching_files.add(file_path) |
| 71 | # No need to process more lines for this file |
| 72 | break |
| 73 | |
| 74 | # Build output line |
| 75 | output_parts = [] |
| 76 | |
| 77 | # Add filename if multiple files |
| 78 | if multiple_files: |
| 79 | output_parts.append(file_path) |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | if multiple_files: |
| 84 | output_parts[-1] += ':' + str(line_num) |
| 85 | else: |
| 86 | output_parts.append(str(line_num)) |
| 87 | |
| 88 | # Add the actual line content |
| 89 | if output_parts: |
| 90 | result_lines.append(':'.join(output_parts) + ':' + line.rstrip('\n')) |
| 91 | else: |
| 92 | result_lines.append(line.rstrip('\n')) |
| 93 | |
| 94 | # Edge Case: File has matches and we're in files-only mode |
| 95 | if files_only and file_has_match: |
| 96 | matching_files.add(file_path) |
| 97 | |
| 98 | # Edge Case: Files-only mode - return file names |
| 99 | if files_only: |
| 100 | return '\n'.join(sorted(list(matching_files))) |
| 101 | |
| 102 | return '\n'.join(result_lines) |
| 103 | |
| 104 | # Handled Edge Cases: Empty pattern, No files provided, File not found or cannot be read, Only file names requested, File has matches and we're in files-only mode |