| 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): Space-separated flags for customizing behavior. |
| 8 | files (list[str]): List of file paths 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 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | filenames_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Edge Case: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | # Edge Case: Pattern is empty |
| 33 | if not pattern: |
| 34 | pattern = "" |
| 35 | |
| 36 | results = [] |
| 37 | matched_files = set() |
| 38 | |
| 39 | # Determine if we're searching multiple files |
| 40 | multiple_files = len(files) > 1 |
| 41 | |
| 42 | # Process each file |
| 43 | for file_path in files: |
| 44 | try: |
| 45 | with open(file_path, 'r', encoding='utf-8') as f: |
| 46 | lines = f.readlines() |
| 47 | except FileNotFoundError: |
| 48 | # Edge Case: File not found - skip file |
| 49 | continue |
| 50 | except Exception: |
| 51 | # Edge Case: Other file reading errors - skip file |
| 52 | continue |
| 53 | |
| 54 | # Process each line in the file |
| 55 | for line_num, line in enumerate(lines, start=1): |
| 56 | # Prepare strings for comparison |
| 57 | search_pattern = pattern |
| 58 | search_line = line.rstrip('\n') |
| 59 | |
| 60 | if case_insensitive: |
| 61 | search_pattern = pattern.lower() |
| 62 | search_line = search_line.lower() |
| 63 | |
| 64 | # Determine if line matches |
| 65 | if exact_match: |
| 66 | is_match = search_line == search_pattern |
| 67 | else: |
| 68 | is_match = search_pattern in search_line |
| 69 | |
| 70 | # Apply invert flag |
| 71 | if invert_match: |
| 72 | is_match = not is_match |
| 73 | |
| 74 | # If line matches criteria |
| 75 | if is_match: |
| 76 | # Edge Case: Filenames only mode |
| 77 | if filenames_only: |
| 78 | matched_files.add(file_path) |
| 79 | # Break early since we only need to know the file matches |
| 80 | break |
| 81 | else: |
| 82 | output_line = line.rstrip('\n') |
| 83 | |
| 84 | # Build the output prefix |
| 85 | prefix_parts = [] |
| 86 | |
| 87 | # Add filename if multiple files |
| 88 | if multiple_files: |
| 89 | prefix_parts.append(file_path) |
| 90 | |
| 91 | # Add line number if requested |
| 92 | if line_numbers: |
| 93 | prefix_parts.append(str(line_num)) |
| 94 | |
| 95 | # Join prefix parts |
| 96 | if prefix_parts: |
| 97 | output_line = ':'.join(prefix_parts) + ':' + output_line |
| 98 | |
| 99 | results.append(output_line) |
| 100 | |
| 101 | # Edge Case: Filenames only mode - return file names |
| 102 | if filenames_only: |
| 103 | return '\n'.join(sorted(matched_files)) + ('\n' if matched_files else '') |
| 104 | |
| 105 | # Edge Case: No matches found |
| 106 | if not results: |
| 107 | return "" |
| 108 | |
| 109 | return '\n'.join(results) + '\n' |
| 110 | |
| 111 | # Handled Edge Cases: No files provided, Pattern is empty, File not found, Other file reading errors, Filenames only mode, No matches found |