@ngel

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): 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 files_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: Empty pattern
29 if not pattern:
30 pattern_to_use = ''
31 else:
32 pattern_to_use = pattern.lower() if case_insensitive else pattern
33
34 results = []
35 matching_files = set()
36
37 # Edge Case: No files provided
38 if not files:
39 return ""
40
41 multiple_files = len(files) > 1
42
43 for file_path in files:
44 try:
45 with open(file_path, 'r', encoding='utf-8') as file:
46 lines = file.readlines()
47
48 # Edge Case: File is empty or unreadable
49 if not lines:
50 continue
51
52 for line_num, line in enumerate(lines, start=1):
53 # Prepare line for comparison
54 line_to_check = line.rstrip('\n')
55 comparison_line = line_to_check.lower() if case_insensitive else line_to_check
56
57 # Determine if line matches
58 if exact_match:
59 is_match = comparison_line == pattern_to_use
60 else:
61 is_match = pattern_to_use in comparison_line
62
63 # Apply invert flag
64 if invert_match:
65 is_match = not is_match
66
67 if is_match:
68 if files_only:
69 matching_files.add(file_path)
70 # No need to check more lines in this file
71 break
72 else:
73 output_line = ""
74 if multiple_files:
75 output_line += f"{file_path}:"
76 if line_numbers:
77 output_line += f"{line_num}:"
78 output_line += line
79 results.append(output_line)
80 except FileNotFoundError:
81 # Edge Case: File not found
82 continue
83 except Exception:
84 # Edge Case: Other file reading errors
85 continue
86
87 if files_only:
88 # Edge Case: No files matched
89 return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "")
90 else:
91 # Edge Case: No matches found
92 return "".join(results)
93
94# Handled Edge Cases: Empty pattern, no files provided, file is empty or unreadable, file not found, other file reading errors, no files matched, no matches found
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.