| 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 | The function supports the following flags: |
| 14 | -n: Prepend line numbers |
| 15 | -l: Output only filenames with matches |
| 16 | -i: Case-insensitive matching |
| 17 | -v: Invert matching (return non-matching lines) |
| 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 | result_lines = [] |
| 28 | matched_files = set() |
| 29 | |
| 30 | # Edge Case: Empty file list |
| 31 | if not files: |
| 32 | return "" |
| 33 | |
| 34 | # Edge Case: Empty pattern |
| 35 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 36 | |
| 37 | for file_path in files: |
| 38 | try: |
| 39 | # Edge Case: File not found or inaccessible |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | except FileNotFoundError: |
| 43 | # Edge Case: Continue processing other files if one is not found |
| 44 | continue |
| 45 | except Exception: |
| 46 | # Edge Case: Handle other file reading errors |
| 47 | continue |
| 48 | |
| 49 | file_has_match = False |
| 50 | |
| 51 | for line_num, line in enumerate(lines, 1): |
| 52 | # Process line for comparison |
| 53 | compare_line = line.rstrip('\n') |
| 54 | compare_text = compare_line.lower() if case_insensitive else compare_line |
| 55 | |
| 56 | # Determine if line matches |
| 57 | if exact_match: |
| 58 | is_match = (search_pattern == compare_text) |
| 59 | else: |
| 60 | is_match = (search_pattern in compare_text) |
| 61 | |
| 62 | # Apply invert flag |
| 63 | if invert_match: |
| 64 | is_match = not is_match |
| 65 | |
| 66 | if is_match: |
| 67 | file_has_match = True |
| 68 | |
| 69 | # Edge Case: When -l flag is used, we only need to know file has a match |
| 70 | if files_only: |
| 71 | matched_files.add(file_path) |
| 72 | break # No need to check more lines in this file |
| 73 | |
| 74 | # Build output line |
| 75 | output_parts = [] |
| 76 | |
| 77 | # Add filename prefix if multiple files |
| 78 | if len(files) > 1: |
| 79 | output_parts.append(file_path) |
| 80 | |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | # If we have filename, add colon, otherwise just the number |
| 84 | if output_parts: |
| 85 | output_parts.append(f":{line_num}") |
| 86 | else: |
| 87 | output_parts.append(str(line_num)) |
| 88 | |
| 89 | # Add colon separator if we have filename or line number |
| 90 | if output_parts: |
| 91 | output_parts.append(":") |
| 92 | |
| 93 | # Add the actual line |
| 94 | output_parts.append(line.rstrip('\n')) |
| 95 | |
| 96 | result_lines.append(''.join(output_parts)) |
| 97 | |
| 98 | # Edge Case: Handle -l flag after processing file |
| 99 | if files_only and file_has_match: |
| 100 | matched_files.add(file_path) |
| 101 | |
| 102 | # Edge Case: Return only file names when -l flag is used |
| 103 | if files_only: |
| 104 | return '\n'.join(sorted(list(matched_files))) |
| 105 | |
| 106 | # Edge Case: No matches found |
| 107 | return '\n'.join(result_lines) |
| 108 | # Handled Edge Cases: Empty file list, Empty pattern, File not found or inaccessible, Continue processing other files if one is not found, Handle other file reading errors, When -l flag is used, we only need to know file has a match, Return only file names when -l flag is used, No matches found |