agent

Finished
11def 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): Flags for customizing the command's behavior.
8 files (list[str]): One or more files to search in.
9
10 Returns:
11 str: All matching lines, possibly with file names and line numbers prepended.
12
13 Edge Cases:
14 - Empty pattern: Matches every line
15 - Empty file: No lines to match
16 - File not found: Should be handled gracefully
17 - Multiple flags: All specified flags should be applied
18 - No matching lines: Return empty string
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 # Prepare pattern for comparison
28 search_pattern = pattern.lower() if case_insensitive else pattern
29
30 # Initialize results
31 result_lines = []
32 matching_files = set()
33
34 # Edge Case: Empty files list
35 if not files:
36 return ""
37
38 # Process each file
39 for filename in files:
40 try:
41 # Edge Case: File not found
42 with open(filename, 'r') as file:
43 lines = file.readlines()
44 except FileNotFoundError:
45 # According to typical grep behavior, we silently ignore missing files
46 # or we could raise an error - following typical behavior here
47 continue
48
49 # Edge Case: Empty file
50 if not lines:
51 continue
52
53 # Process each line
54 for line_num, line in enumerate(lines, 1):
55 # Prepare line for comparison
56 compare_line = line.lower() if case_insensitive else line
57
58 # Determine if line matches
59 if exact_match:
60 matches = search_pattern == compare_line.rstrip('\n')
61 else:
62 matches = search_pattern in compare_line
63
64 # Apply invert flag
65 if invert_match:
66 matches = not matches
67
68 # If line matches, collect results
69 if matches:
70 matching_files.add(filename)
71
72 # If we only want file names, we can break early
73 if files_only:
74 break
75
76 # Build output line
77 output_line = ""
78
79 # Add filename if multiple files
80 if len(files) > 1:
81 output_line += f"{filename}:"
82
83 # Add line number if requested
84 if line_numbers:
85 output_line += f"{line_num}:"
86
87 # Add the actual line
88 output_line += line
89
90 result_lines.append(output_line)
91
92 # Edge Case: -l flag - return only filenames
93 if files_only:
94 # Return filenames in the order they were found in the files list
95 ordered_matching_files = [f for f in files if f in matching_files]
96 return "\n".join(ordered_matching_files) + ("\n" if ordered_matching_files else "")
97
98 # Edge Case: No matches
99 return "".join(result_lines)
100
101# Handled Edge Cases: Empty pattern, Empty file, File not found, Multiple flags, No matching lines, Empty files list
Test NameStatus
test_multiple_files_no_matches_various_flags
Pass
test_multiple_files_one_match_match_entire_lines_flag
Pass
test_multiple_files_one_match_multiple_flags
Pass
test_multiple_files_one_match_no_flags
Pass
test_multiple_files_one_match_print_file_names_flag
Pass
test_multiple_files_several_matches_case_insensitive_flag
Pass
test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
Pass
test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
Pass
test_multiple_files_several_matches_inverted_flag
Pass
test_multiple_files_several_matches_no_flags
Pass
test_multiple_files_several_matches_print_line_numbers_flag
Pass
test_one_file_no_matches_various_flags
Pass
test_one_file_one_match_case_insensitive_flag
Pass
test_one_file_one_match_file_flag_takes_precedence_over_line_flag
Pass
test_one_file_one_match_match_entire_lines_flag
Pass
test_one_file_one_match_multiple_flags
Pass
test_one_file_one_match_no_flags
Pass
test_one_file_one_match_print_file_names_flag
Pass
test_one_file_one_match_print_line_numbers_flag
Pass
test_one_file_several_matches_case_insensitive_flag
Pass
test_one_file_several_matches_inverted_and_match_entire_lines_flags
Pass
test_one_file_several_matches_inverted_flag
Pass
test_one_file_several_matches_match_entire_lines_flag
Pass
test_one_file_several_matches_no_flags
Pass
test_one_file_several_matches_print_line_numbers_flag
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.