Debian Codegraph @

git_delete_merged_branches/_without.py

raw symbols SCIP
1 # Copyright (C) 2026 Sebastian Pipping <[email protected]>
2 # Licensed under GPL v3 or later
3
4 import copy
5 from typing import Any
6
7
8 def without(needle: list[Any], container: list[Any]) -> list[Any]:
9 """
10 Create a copy of a list with all occurences of sublist ``needle`` removed.
11 """
12 if not needle:
13 return copy.copy(container)
14
15 needle_first = needle[0]
16 needle_len = len(needle)
17
18 start_at = 0
19 result = []
20
21 while True:
22 # Find the potential start of a match
23 try:
24 found_at = container.index(needle_first, start_at)
25 except ValueError: # i.e. not found
26 # Copy the full remainder, and stop
27 result.extend(container[start_at:])
28 break
29
30 # Do we have an actual match?
31 if container[found_at : found_at + needle_len] == needle:
32 # Yes: skip the match, continue
33 result.extend(container[start_at:found_at])
34 start_at = found_at + needle_len
35 else:
36 # No: continue with next character
37 result.extend(container[start_at : found_at + 1])
38 start_at = found_at + 1
39
40 return result