Open the context
Choose the project folder and name the files or behavior involved.
Resolve one ASCII HTTP byte range against a resource size from zero through 10^12 using a header of at most 80 characters. Test open ends and suffixes, clip an excessive end and return no interval for unsatisfiable requests while rejecting malformed or multiple ranges.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
import re
def byte_range(header, size):
if type(header) is not str or len(header) > 80 or type(size) is not int or not 0 <= size <= 10**12:
raise ValueError("range.input")
match = re.fullmatch(r"bytes=([0-9]*)-([0-9]*)", header)
if match is None or not any(match.groups()):
raise ValueError("range.syntax")
left, right = match.groups()
if not left:
suffix = int(right)
if suffix == 0 or size == 0:
return None
return max(0, size-suffix), size-1
start = int(left)
end = int(right) if right else size-1
if start >= size or end < start:
return None
return start, min(end, size-1)
The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.